Search code examples
powerbidaxpowerbi-desktopmeasure

Decrement/increment rank automatically after condition is met in DAX Power BI


I have a measure written in Power BI as below, where I have ranked the dates based on the recency where 1 will be the ranking of most recent date. When there are 2 dates this measure gives correct result but when there are 3 dates, it keeps the reference of date always as minimum and does the calculation.Example below

Snapshot Date   Overdue Total   Diff
5/09/2020       $88,786         $88,786
7/09/2020       $87,434        -$1,352
9/09/2020       $90,123         $86,097 

Is there a way to move the rank and keep increasing the min dates by 1 as the condition is met. So that I get Rank 1- Rank 2 , then rank 2- rank 3 and so on.

Diff = 
VAR MAX_DATE=min('Account and SA'[Date Rank])

VAR PREV_VAL=
CALCULATE(
    sum('Account and SA'[Overdue(Total)]),
    FILTER(
        ALLSELECTED('Account and SA'),
        'Account and SA'[Date Rank] > MAX_DATE
    )
)

return
sum('Account and SA'[Overdue(Total)])-PREV_VAL

Solution

  • If you are looking for the Difference of current row overdue with previous available date, you can use this below Measure-

    Diff = 
    
    VAR previous_date =
    CALCULATE(
        MAX('Account and SA'[Snapshot Date]),
        FILTER(
            ALL('Account and SA'),
            'Account and SA'[Snapshot Date] < MIN('Account and SA'[Snapshot Date])
        )
    )
    
    VAR previous_date_overdue =
    CALCULATE(
        MAX('Account and SA'[Overdue Total]),
        FILTER(
            ALL('Account and SA'),
            'Account and SA'[Snapshot Date] = previous_date 
        )
    )
    
    RETURN MIN('Account and SA'[Overdue Total]) - previous_date_overdue