powerbidax

In DAX generate a measure that creates daily snapshot data from non-snapshot data


I would love if someone could help me with something I am struggling with in DAX.

I have a fact table something like the following:

Date Product Customer Value
2024/1/15 Prod 1 Cust A 1
2024/3/21 Prod 1 Cust A 2
2024/5/11 Prod 1 Cust A 3
2024/9/29 Prod 1 Cust A 2
2024/11/11 Prod 1 Cust A 1

And similar data for Prod 2 and Cust B etc

I want to create a DAX measure that will give me the most recent value for every date in the calendar year, including dates not in the fact table. Something that will also allow aggregation of the most recent values from all the Products and Customers on that date.

I can get something working when using only Date and Value in the fact table but it gets too complicated when I add in Product and Customer.

I would be very grateful for any help.

I tried the following that works for date and value only but not for aggregating different products and customers

LatestValue =
var _latestDate = max(tblCalendar[Date])
 var _latestDataDate = CALCULATE(max(tblData[Date]),  tblCalendar[Date] <= _latestDate)
 var _latestData = CALCULATE(sum(tblData[Value]), tblCalendar[Date] = _latestDataDate)
 RETURN _latestData

Solution

  • I assume that you are asking to display the sales from the most recent day with sales, even on days where no sales are recorded. It would help if you could show the expected output in your question. Anyway, here is one way you can achieve this result:

        Latest Sales = 
        VAR CurrentDate = MAX('Date'[Date])
        VAR LatestSalesDate =
            CALCULATE(
                MAX(Sales[Date]),
                Sales[Date] <= CurrentDate, 
                REMOVEFILTERS(Sales[Date])
            )
        RETURN
            CALCULATE(
                SUM(Sales[Value]),
                Sales[Date] = LatestSalesDate
            )