Forum Discussion

Jimcint's avatar
Jimcint
New Member
2 years ago
Solved

Calculating difference between rows based on multiple columns

I have a large table that contains mass measurements from multiple tires at multiple mileages, as shown below:

 

I would like to create two new columns (MassLossKG and MileDiff) that contain the difference in mass and miles from consecutive measurements of the same tire (same TyreSpec and TyreNum), So, I'm looking for somethine like this:

MilesTyreSpecTyreNumTyreWheelMassKGMassLossKGMileDiff
8000C321.25.04800
0C422.38nullnull
800C422.36.02800
1600C422.34.02800
2400C422.28.06800
3200C422.24.04800
4000C422.18.06800
4800C422.14.04800
5600C422.11.03800
6400C422.1.01800
7200C422.03.07800
8000C422.01.02800
0D123.12nullnull

 

Is it possible to do something like this in DAX? How?

If it's not possible to use null or blank values for the 0 mile cases, then 0 is okay. 

 

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi,

    PijushRoy , thanks for the concern about the question, and i want to offer some more infotmation for user to refer to.

    hello Jimcint , based on your description, you can refer to the following caucluated columns.

     

    MassLossKG =
    VAR a =
        CALCULATE (
            SUM ( 'Table'[TyreWheelMassKG] ),
            ALLEXCEPT ( 'Table', 'Table'[TyreSpec], 'Table'[TyreNum] ),
            'Table'[StopId]
                = EARLIER ( 'Table'[StopId] ) - 1
        )
    RETURN
        IF ( [Miles] <> 0, ABS ( [TyreWheelMassKG] - a ) )
    
    MileDiff =
    VAR a =
        CALCULATE (
            SUM ( 'Table'[Miles] ),
            ALLEXCEPT ( 'Table', 'Table'[TyreSpec], 'Table'[TyreNum] ),
            'Table'[StopId]
                = EARLIER ( 'Table'[StopId] ) - 1
        )
    RETURN
        IF ( [Miles] <> 0, ABS ( [Miles] - a ) )
    

     

    Output

    Best Regards!

    Yolo Zhu

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

     

4 Replies

  • PijushRoy's avatar
    PijushRoy
    Community Champion

    Hi Jimcint 

    Your requirements and examples are not clear to me. Can you add more details?
    In MassLossKG column 1st value 0.04, what is the calculation?

    • Jimcint's avatar
      Jimcint
      New Member

      For the fourth row of the table, tire C 4 is at 1600 miles. the mass is 22.34 kg. The previous measurement is 800 miles with a mass of 22.36 kg. so mass loss is 22.36 - 22.34 = 0.02 measurement distance is 1600 - 800 = 800. 

      for the first (0 mile) measurement, there is nothing to subtract from, so the results would be null, 0, or blank. 

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi,

        PijushRoy , thanks for the concern about the question, and i want to offer some more infotmation for user to refer to.

        hello Jimcint , based on your description, you can refer to the following caucluated columns.

         

        MassLossKG =
        VAR a =
            CALCULATE (
                SUM ( 'Table'[TyreWheelMassKG] ),
                ALLEXCEPT ( 'Table', 'Table'[TyreSpec], 'Table'[TyreNum] ),
                'Table'[StopId]
                    = EARLIER ( 'Table'[StopId] ) - 1
            )
        RETURN
            IF ( [Miles] <> 0, ABS ( [TyreWheelMassKG] - a ) )
        
        MileDiff =
        VAR a =
            CALCULATE (
                SUM ( 'Table'[Miles] ),
                ALLEXCEPT ( 'Table', 'Table'[TyreSpec], 'Table'[TyreNum] ),
                'Table'[StopId]
                    = EARLIER ( 'Table'[StopId] ) - 1
            )
        RETURN
            IF ( [Miles] <> 0, ABS ( [Miles] - a ) )
        

         

        Output

        Best Regards!

        Yolo Zhu

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