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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
senad
Frequent Visitor

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
Super User
Super User

Hi @senad ,

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 @senad ,

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
senad
Frequent Visitor

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 @senad ,

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!




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

Hi @senad ,

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!




senad
Frequent Visitor

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 @senad ? Nathaniel

 

 





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

Proud to be a Super User!




Nathaniel_C
Super User
Super User

Hi @senad ,

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 @senad ,
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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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