Forum Discussion

DreamToGet's avatar
DreamToGet
Frequent Visitor
5 years ago
Solved

Subtract values from same column based on multiple filters

Hi all,   I am trying to find a way to subtract values from the same column ('Output') based on filters.   E.g., for device serial number 'A123BX90', I would like to find the difference of 'Outpu...
  • v-alq-msft's avatar
    5 years ago

    Hi, DreamToGet 

     

    Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

    Table:

     

    In Power Query, you may create a new query with the following m codes in 'Advanced Editor'.

    let
        Source = Table.Group(Table, {"DeviceSerialNo", "TestStep"}, {{"Data", each 
    let 
    x=try Number.From([Output]{0}) otherwise null,
    y=try Number.From([Output]{1}) otherwise null
    in x-y , type number}})
    
    in
        Source

     

    Result:

     

    If you want to use DAX, you need to create an index column in Power Query.

     

    You may create a measure as below.

    Result Measure = 
    SUMX(
        SUMMARIZE(
            'Table',
            [DeviceSerialNo],
            [TestStep],
            "Result",
            var minindex = MIN('Table'[Index])
            var maxindex = MAX('Table'[Index])
            var val1 = 
            IFERROR(
                VALUE(
                    MAXX(
                        FILTER(
                            'Table',
                            [Index]=minindex
                        ),
                        [Output]
                    )
                ),
                BLANK()
            )
            var val2 = 
            IFERROR(
                VALUE(
                    MAXX(
                        FILTER(
                            'Table',
                            [Index]=maxindex
                        ),
                        [Output]
                    )
                ),
                BLANK()
            )
            return
            val1-val2
        ),
        [Result]
    )

     

    Result:

     

    Best Regards

    Allan

     

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

  • v-alq-msft's avatar
    v-alq-msft
    5 years ago

    Hi, DreamToGet 

     

    In Power Query, you may add a new step with the following m codes. The pbix file is attached in the end.

    = Table.Group(#"Changed Type", {"DeviceSerialNo", "TestStep"}, {{"Data", each 
    let 
    x= Table.Max( Table.SelectRows(_,each [TestProcess]="Process-Pre"),"StartDate")[Output],
    y= Table.Max( Table.SelectRows(_,each [TestProcess]="Process-Post"),"StartDate")[Output],
    m= try Number.From(x) otherwise null,
    n= try Number.From(y) otherwise null
    in m-n
    }})

     

    Result:

     

    Best Regards

    Allan

     

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