Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Anonymous
Not applicable

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.

2 ACCEPTED SOLUTIONS

@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.. 

 

image.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




View solution in original post

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

 

  



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

5 REPLIES 5
TomMartens
Super User
Super User

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

image.png

 

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

 

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
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) 

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

 

  



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
Anonymous
Not applicable

Thanks @TomMartens and @PattemManohar !!

works great!

@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.. 

 

image.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




Helpful resources

Announcements
October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.