Forum Discussion
DAX
- 1 year ago
Hi leejq1
I have two solutions for it, please try and let me know if it helped you in any way. If MAX is not working for you, you can try the below given methods:1. DAX
Opening ODO = VAR CurrentMonthIndex = MAXX(ALLSELECTED('Calendar table'), 'Calendar table'[Index]) VAR LastMonthIndex = CurrentMonthIndex - 1 VAR LatestDateInLastMonth = CALCULATE( LASTDATE('Shell Report'[Date]), FILTER( ALL('Calendar table'), 'Calendar table'[Index] = LastMonthIndex ), FILTER( ALL('Shell Report'), 'Shell Report'[Vehicle License Number] = MAX('Shell Report'[Vehicle License Number]) ) ) VAR OdometerReadingOnLatestDate = CALCULATE( MAX('Shell Report'[Odometer Reading]), 'Shell Report'[Date] = LatestDateInLastMonth, FILTER( ALL('Shell Report'), 'Shell Report'[Vehicle License Number] = MAX('Shell Report'[Vehicle License Number]) ) ) RETURN OdometerReadingOnLatestDateThis should work correctly even with multiple months selected in the filter.
2. DAX
Opening ODO = VAR CurrentMonthIndex = MAXX(ALLSELECTED('Calendar table'), 'Calendar table'[Index]) VAR LastMonthIndex = CurrentMonthIndex - 1 VAR LatestDateInLastMonth = CALCULATETABLE( TOPN( 1, FILTER( ALL('Shell Report'), 'Shell Report'[Vehicle License Number] = MAX('Shell Report'[Vehicle License Number]) && RELATED('Calendar table'[Index]) = LastMonthIndex ), 'Shell Report'[Date], DESC ) ) VAR OdometerReadingOnLatestDate = CALCULATE( MAX('Shell Report'[Odometer Reading]), FILTER( LatestDateInLastMonth, 'Shell Report'[Vehicle License Number] = MAX('Shell Report'[Vehicle License Number]) ) ) RETURN OdometerReadingOnLatestDateThis version with TOPN can be more flexible in situations where MAX or LASTDATE do not provide the intended result across multiple months. Give this a try and let me know how it goes!
Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂
Kind Regards,
Poojara
Data Analyst | MSBI Developer | Power BI Consultant
YouTube: https://youtube.com/@biconcepts?si=04iw9SYI2HN80HKS
Thanks for youe advice. It doesnt work with MAX
- 123abc1 year agoCommunity Champion
let’s try a different approach. Instead of using MAX, we can use TOPN to get the latest date and corresponding odometer reading. Here’s an updated version of your measure:
Opening ODO =
VAR CurrentIndex = SELECTEDVALUE('Calendar table'[Index])
VAR LastMonthIndex = CurrentIndex - 1VAR LatestDateInLastMonth =
CALCULATE(
MAX('Shell Report'[Date]),
FILTER(
ALL('Calendar table'),
'Calendar table'[Index] = LastMonthIndex
),
FILTER(
ALL('Shell Report'),
'Shell Report'[Vehicle License Number] = MAX('Shell Report'[Vehicle License Number])
)
)VAR OdometerReadingOnLatestDate =
CALCULATE(
MAXX(
TOPN(
1,
FILTER(
ALL('Shell Report'),
'Shell Report'[Date] = LatestDateInLastMonth &&
'Shell Report'[Vehicle License Number] = MAX('Shell Report'[Vehicle License Number])
),
'Shell Report'[Date], DESC
),
'Shell Report'[Odometer Reading]
)
)RETURN
OdometerReadingOnLatestDateThis should help in getting the correct odometer reading even when multiple months are selected. Give this a try and let me know if it works!