Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

diff from previous row (group by type)

Hi all, need your help. 

I got this table:

typedaynum
A10
A21
A36
A48
A515
A620
B12
B210
B315
B417
B517
B6

20

 

and want to add a column (diff) like this:

typedaynumdiff
A100
A211
A365
A483
A5157
A6205
B120
B2108
B3155
B4172
B5170
B6203

 

The column need to calculate the difference from previous row within the group (column "type").

I tried to use EARLIER function, but didn't succeed.

  • Anonymous If you don't have "Day" field then add an Index field in Power Query Editor and then add a RANK for each group (which will be logically your "Day" field as mentioned above)

     

    RankIndex = RANKX(FILTER(Test62RunningDiff,Test62RunningDiff[Type]=EARLIER(Test62RunningDiff[Type])),Test62RunningDiff[Index],,ASC)

    Now you can use this "RankIndex" instead of "Day" field in the above logic for calculating "Diff" posted by TomMartens

     

    Finally your table looks like this.. 

     

     

  • Hey Anonymous,

     

    for your 1st example you can use this DAX statement to create the diff, the statement does not rely on consecutive days. Basically it looks for the max day that is smaller as the current day and uses this day in the LOOKUPVALUE(...) function:

    diff non consecutive = 
    var currenttype = 'Table1'[type]
    var currentday = 'Table1'[day]
    var maxPriorDay = 
        CALCULATE(
            MAX('Table1'[day])
            ,FILTER(
                ALL('Table1')
                ,'Table1'[type] = currenttype && 'Table1'[day] < currentday
            )
        )
    return
    'Table1'[num] - LOOKUPVALUE('Table1'[num],'Table1'[type], currenttype, Table1[day], maxPriorDay)

    If your data does not contain something that can be used as an index (somehow you have to determine the previous row) you have to create one using PowerQuery as PattemManohar already mentioned. This can become somewhat challenging if you have to create an index for groups. Maybe this blogpost provides you with some ideas how this can be achieved using PowerQuery and some little M tweeking:

    https://www.minceddata.info/2018/04/30/navigating-tabular-tables-previous-row/

     

    Hopefully this will help you to tackle your challenges.

     

    Regards,

    Tom

     

      

5 Replies

  • Hey,

     

    you can use this DAX statement to create a calculated column:

    diff calc = 
    'Table1'[num]-
    LOOKUPVALUE('Table1'[num],'Table1'[type], 'Table1'[type],'Table1'[day],'Table1'[day]-1)
      

    This creates these values

     

    Please be aware that this only works if the values in the "day" column are consecutive. If this will not be the case than it will become a little more complex.

     

    Please tell us if the values in the day column are consecutive or not.

     

    Regards,

    Tom

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Acctually the "day" column are not consecutive :/

      There is an option to calculate the new column without using the "day" column at all ?(in some tables  i dont have this column) 

      • PattemManohar's avatar
        PattemManohar
        Community Champion

        Anonymous If you don't have "Day" field then add an Index field in Power Query Editor and then add a RANK for each group (which will be logically your "Day" field as mentioned above)

         

        RankIndex = RANKX(FILTER(Test62RunningDiff,Test62RunningDiff[Type]=EARLIER(Test62RunningDiff[Type])),Test62RunningDiff[Index],,ASC)

        Now you can use this "RankIndex" instead of "Day" field in the above logic for calculating "Diff" posted by TomMartens

         

        Finally your table looks like this..