Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Previous rows value based on date (Lag functionality from SQL in Power BI logic)

Hi, I am confused about how to achieve the values from previous row in Power BI (which means the lag functionality in SQL) I have a table where the cumulative_inventory is already calculated based ...
  • Henrykong_'s avatar
    4 years ago

    Hi Anonymous ,

     

    You can try formula like below:

    M_Pre = 
    VAR pre_ =
        CALCULATE (
            MAX ( 'Table'[Date] ),
            FILTER ( ALL ( 'Table' ), 'Table'[Date] <= MAX ( 'Table'[Date] ) - 1 )
        )
    RETURN
        CALCULATE (
            MAX ( 'Table'[Volumn] ),
            FILTER ( ALL ( 'Table' ), 'Table'[Date] = pre_ )
        )

     

    If converted into a calculated column formula, you can use the top function to get the value of any of the first few lines (or summation). Specific reference to the following formula:

    Column =
    VAR pre_row =
        TOPN (
            1,
            FILTER (
                'Table',
                'Table'[Month] < EARLIER ( 'Table'[Month] )
                    && 'Table'[Employee] = EARLIER ( 'Table'[Employee] )
            ),
            [Month], DESC
        )
    VAR pre_val =
        MINX ( pre_row, [Sales] )
    RETURN
        'Table'[Sales] + pre_val

     

    If the problem is still not resolved, please provide detailed error information and let me know immediately. Looking forward to your reply.


    Best Regards,
    Henry


    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.