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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
Anonymous
Not applicable

Subtracting LAST ID in same column

IDAgeAtEventEventTypeDifferenrce 
101101st Tag-
102101st Tag-
103101st Tag-
101162nd tag6
104101st Tag-
105111st Tag 
103142nd tag3
101203rd Tag4

 

Best way to achieve this output in Power BI? I have the 1st 3 columns, looking to calculate the 4th

 

 

Essentially, each row represents an event trigger, and I would like to find the difference of event age. So 2nd Event minus 1st event, but if no other event comes before it, then prdouce a blank. 

 

ID 101 for example, has 3 events, so I would like the difference from 1st event to 2nd, 2nd to 3rd, etc. 

 

Thank you in advance!

2 ACCEPTED SOLUTIONS
Nathaniel_C
Community Champion
Community Champion

Hi @Anonymous ,

Like this?

 

Nathaniel_C_1-1659896106186.png

 

 


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel

 





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

Proud to be a Super User!




View solution in original post

Hi @Anonymous ,

Please try this:

mydifcolumn = 

var _ID = (TagTable[ID])
var _ageAtEvent = (TagTable[AgeAtEvent])
var _EventTypeOnly = (TagTable[EventType # only])

var _previousEventTypeOnly = _EventTypeOnly-1 //get the previous Event Type

var _calc = CALCULATE(MAX(TagTable[AgeAtEvent]),FILTER(all(TagTable),TagTable[EventType # only]=_previousEventTypeOnly && TagTable[ID]=_ID)) // Get the Age At Event for the previous row of the same ID

Return if (_previousEventTypeOnly>0, _ageAtEvent- _calc) 

 

Nathaniel_C_0-1659909768672.png

Basically the same thing, but take out the MAX() surrounding all the column names that are brought in as variables (var)

Good Luck! And as this is a second solution, would you please mark this as well?


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel





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

Proud to be a Super User!




View solution in original post

8 REPLIES 8
Anonymous
Not applicable

Hi @Nathaniel_C  this works great as a measure, thank you so much

 

Is there a way to do it as a calculated column, though?


Let me know if you can!

Hi @Anonymous ,

Please try this:

mydifcolumn = 

var _ID = (TagTable[ID])
var _ageAtEvent = (TagTable[AgeAtEvent])
var _EventTypeOnly = (TagTable[EventType # only])

var _previousEventTypeOnly = _EventTypeOnly-1 //get the previous Event Type

var _calc = CALCULATE(MAX(TagTable[AgeAtEvent]),FILTER(all(TagTable),TagTable[EventType # only]=_previousEventTypeOnly && TagTable[ID]=_ID)) // Get the Age At Event for the previous row of the same ID

Return if (_previousEventTypeOnly>0, _ageAtEvent- _calc) 

 

Nathaniel_C_0-1659909768672.png

Basically the same thing, but take out the MAX() surrounding all the column names that are brought in as variables (var)

Good Luck! And as this is a second solution, would you please mark this as well?


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel





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

Proud to be a Super User!




Anonymous
Not applicable

Yes! Thank you so much. Appreciate the explanation as well. Have a good one

Hi @Anonymous ,

You are very welcome!

And the reason that the MAX() is not needed, is that when creating a calculated column, Power BI "knows" which row it is on when it does the calculation.

Cheers,

Nathaniel





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

Proud to be a Super User!




Anonymous
Not applicable

Yes! Thank you @Nathaniel_C .

 

I have an EventType #Only column as well! How did you make this work? 

 

Best,

 Senad

You are welcome! Did my second post answer your question @Anonymous ? Nathaniel

 

 





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

Proud to be a Super User!




Nathaniel_C
Community Champion
Community Champion

Hi @Anonymous ,

Like this?

 

Nathaniel_C_1-1659896106186.png

 

 


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel

 





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

Proud to be a Super User!




Hi @Anonymous ,
I would note that the value you have for 103 Difference should be 14-10=4.

mydif = 

var _ID = max(TagTable[ID])
var _ageAtEvent = MAX(TagTable[AgeAtEvent])
var _EventTypeOnly = MAX(TagTable[EventType # only])

var _previousEventTypeOnly = _EventTypeOnly-1 //get the previous Event Type

var _calc = CALCULATE(MAX(TagTable[AgeAtEvent]),FILTER(all(TagTable),TagTable[EventType # only]=_previousEventTypeOnly && TagTable[ID]=_ID)) // Get the Age At Event for the previous row of the same ID

Return if (_previousEventTypeOnly>0, _ageAtEvent- _calc) 

 

Previous to creating this measure, I extracted the numeric value in Power Query from the column EventType to a new column EventType # Only and made it a whole number.

 


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel





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

Proud to be a Super User!




Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.