Forum Discussion

leejq1's avatar
leejq1
Regular Visitor
1 year ago
Solved

DAX

Hello, I need to create a measure to get the last month's latest date Odometer reading as Opening ODO (measure name). This is the current formula I have and it works well when my filter selection is ...
  • Poojara_D12's avatar
    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
        OdometerReadingOnLatestDate

      This 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
        OdometerReadingOnLatestDate

     

    This 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