简体   繁体   中英

Power BI: Use unrelated date table to create a measure

Hi I have been struggling with this for a bit now and I hope I didn't miss a previous question. I am trying to get a count of the vehicles we have based on an EOMONTH date. We are buying and selling cars on a regular basis and for reporting we need to know how many we had at the end of each month and the report is a rolling 12 months.

I've tried creating the relationship with the purchasedate of the vehicle to the date of my date table but when I create the measure (Used to calculate the number of vehicles purchased but haven't been sold):

SalesBlank = CALCULATE( COUNT(Vehicles[MVANumber]), FILTER(Vehicles, Vehicles[purchasedate] <= RELATED('Date'[EOMONTH]) && ISBLANK(Vehicles[saledate])))

I only get a count of vehicles purchased that month and don't have a sale date - I'm not surprised because my relationship with the date table is the purchase date.

How can I set up a measure to look at the date table and filter the vehicles table with this logic: purchasedate <= date[EOMONTH] && ISBLANK(salesdate)

Any help would be greatly appreciated,! Thanks, Matt

Sample Data and Desired Results

Relationships

If I understand you correctly, you want to get a count of the vehicles on hand at the end of each month. That could be calculated by counting the vehicles with a purchase date less than or equal to the selected end of month and subtracting the count of vehicles with a sale date less than or equal to the selected end of month.

You can create an active relationship between Vehicle[PurchaseDate] and Date[Date]. Then create an inactive relationship based upon Vehicles[SaleDate] and Date[Date].

You could use a measure that is something like this:

Inventory Count =
VAR MaxDate =
    MAX ( 'Date'[Date] )
VAR MinDate =
    CALCULATE ( MIN ( 'Date'[Date] ), ALL ( 'Date' ) )
VAR Purch =
    CALCULATE (
        COUNT ( 'Vehicles'[VehicleID] ),
        DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
    )
VAR Sales =
    CALCULATE (
        COUNT ( 'Vehicles'[VehicleID] ),
        USERELATIONSHIP ( 'Date'[Date], Vehicles[Sale Date] ),
        DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
    )
VAR MaxPurDt =
    CALCULATE ( MAX ( 'Vehicles'[Purchase Date] ), ALL ( 'Vehicles' ) )
VAR MaxSlDt =
    CALCULATE ( MAX ( 'Vehicles'[Sale Date] ), ALL ( 'Vehicles' ) )
RETURN
    IF (
        MIN ( 'Date'[Date] ) <= MaxPurDt
            || MIN ( 'Date'[Date] ) <= MaxSlDt,
        Purch - Sales
    )

This measure gets a cumulative count of purchases and a cumulative count of sales and then subtracts them. The IF check is to avoid propagation of cumulative totals beyond the maximum date in the Vehicle table.

I'm not sure how to interpret your showing of just 3 months in the desired results. This will produce the same answers as what you have, but without a filter applied to the table, it starts at 3/31/2016 (the date of the first sale).

Edit: There's probably a more efficient way along the lines you were thinking, but it is escaping me at the moment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM