Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Calculating difference between two values per category

I have this issue. One (groupBy table):   week   costs   orderID 1           10        001 1           12        002 2           15        001 2           18        002   One order table:  ...
  • Icey's avatar
    6 years ago

    Hi Anonymous ,

    You can create your measure like so:

    Costs Measure =
    VAR Order_ID =
        MAX ( 'order table'[orderID] )
    RETURN
        IF (
            HASONEVALUE ( 'order table'[supplier] ),
            MAX ( 'groupBy table'[costs] ),
            MAX ( 'groupBy table'[costs] )
                - CALCULATE (
                    MAX ( 'groupBy table'[costs] ),
                    'order table'[orderID] = Order_ID - 1
                )
        )

     

    And if you have more than 2 suppliers, you can ceate measures like so:

    Diff =
    VAR Order_ID =
        MAX ( 'groupBy table'[orderID] ) - 1
    VAR Previous_Costs =
        CALCULATE (
            MAX ( 'groupBy table'[costs] ),
            FILTER (
                ALLEXCEPT ( 'groupBy table', 'groupBy table'[week] ),
                'groupBy table'[orderID] = Order_ID
            )
        )
    RETURN
        MAX ( 'groupBy table'[costs] ) - Previous_Costs

    PBIX file attached.

     

    Best Regards,
    Icey

     

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