Forum Discussion
Get last known value
Hi all,
I'm having problems with getting the last known value with my DAX. I now have:
MeasureName =
VAR maxdate = LASTDATE ( Date[Date] )
var result =
CALCULATE (
SUM ( Transactons[Amount] ) + 0,
Transacties[Action]="Action 23",
Transacties[Date] <= maxdate,
ALL(Datum)
)
RETURN result
But this is doing exactly what I ask in the code; it gives me the SUM of amount for al specific dates BEFORE the max date. But that's not what I want; I want to have the latest available value, not a sum. But I'm stuck at fixing this.
Example dataset:
1 jan '20 - 100
3 jan '20 - 80
Desired output:
1 jan '20 - 100
2 jan '20 - 100
3 jan '20 - 80
Current output:
1 jan '20 - 100
2 jan '20 - 100
3 jan '20 - 180
Does anybody have an idea?
10 Replies
- amitchandak
Super User
MiKeZZa , With a date table try something like this example
Cumm Sales = CALCULATE(SUM(Sales[Sales Amount]),filter(all(date),date[date] <=maxx(date,date[date]))) - CALCULATE(SUM(Sales[Sales Amount]),filter(all(date),date[date] <=maxx(date,date[date])-1))To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/
See if my webinar on Time Intelligence can help: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/PowerBI-Time-Intelligence-Calendar-WTD-YTD-LYTD-Week-Over-Week/m-p/1051626#M184
Appreciate your Kudos.- MiKeZZa
Post Patron
This doesn't work, it gives strange values. Maybe because of gaps larger than 1 day??
i'll check your documentation later.
- AnonymousNot applicable
Hi MiKeZZa ,
MeasureName = VAR maxdate = MAX ( Date[Date] ) var result = CALCULATE ( MAX ( Transactons[Amount] ), FILTER (ALL(Transacties), Transacties[Action]="Action 23", Transacties[Date] = maxdate)) RETURN result
- MiKeZZa
Post Patron
I've now added an example PBIX: https://gofile.io/d/qMLJiL
I've made this of your solution:
Table 2 =
ADDCOLUMNS(
CALENDAR(MIN('Transactions'[Date]), MAX('Transactions'[Date])),
"Amount",
CALCULATE(SUM('Transactions'[Total]), FILTER('Transactions', 'Transactions'[Date] <= EARLIER([Date]) ), Transactions[Action]="Action 23")
)
What am I doing wrong? Because of 500 and later 1000 do give me 1500. That's not ok... See example PBIX.
Is there also anyone with an other option? Adding a second table isn't the best option I guess.- MiKeZZa
Post Patron
A little kick. somebody???