Forum Discussion

Sultanista's avatar
Sultanista
Frequent Visitor
3 years ago
Solved

Calculate difference between current and previous row with a condition

I have a table and need to calculate a difference between 2 rows with a condition that it only applies to Agregation Type = All Branches - YTD and it is a diff between fiscal month and fiscal month-1...
  • v-yinliw-msft's avatar
    3 years ago

    Hi Sultanista ,

     

    You can try this method:

    New column:

     

    Diff =
    VAR _curr =
        CALCULATE (
            SUM ( 'Table'[Applicants] ),
            'Table'[Agregation Type] = "All Branches - YTD"
        )
    RETURN
        IF (
            [Fiscal Month] = 1
                || 'Table'[Agregation Type] <> "All Branches - YTD"
                || ISBLANK ( 'Table'[Applicants] ),
            BLANK (),
            _curr
                - CALCULATE (
                    SUM ( 'Table'[Applicants] ),
                    FILTER (
                        'Table',
                        [Agregation Type] = "All Branches - YTD"
                            && [Fiscal Year] = EARLIER ( 'Table'[Fiscal Year] )
                            && [Fiscal Month]
                                = EARLIER ( 'Table'[Fiscal Month] ) - 1
                    )
                )
        )
    

     

    The result is:

     

     

    Hope this helps you. Here is my PBIX file.

     

    Best Regards,

    Community Support Team _Yinliw

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