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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
nerokasai
Frequent Visitor

Difference between 2 rows of a column (elapsed distance)

Hi all!

 

I would like to create either a column or measure which returns the difference of distance run on a recorded event date

Here's a sample source table:

Company NameEmployeeRecorded DateDifference (distance run on recorded date)Total distance run (km)
ABob2019-12-0122
ABob2019-12-0257
AJack2019-12-03310
BHannah2019-12-0244
BJohn2019-12-0537

 

Ideally, I would like to take data from the difference column to make a visual that represents the average distance ran on recorded date (average of the difference between two rows values) for each company or employee.

 

What would be the best way to approach this? Would I need Power Query for this?

 

Thank you!

2 ACCEPTED SOLUTIONS
Greg_Deckler
Super User
Super User

OK, you can create the column you requested using EARLIER to filter ALL of your table and then using MAXX on that to grab the date (this gets you your previous row). Then you could use that date to filter out to the value for the last run. See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395.... Let me know if you need further clarification but the technique is pretty much there. Although you might have an extra step to grab the previous run's distance.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

Some like this as mentioned by @Greg_Deckler 

 difference of distance run on a recorded event date = 
 var _last = maxx(filter(table,table[Company Name]=earlier(table[Company Name]) && table[Recorded Date]<earlier(table[Recorded Date]) ),table[Total distance run (km)])
 return
 if(is(_last),table[Total distance run (km)], _last-table[Total distance run (km)])

View solution in original post

6 REPLIES 6
amitchandak
Super User
Super User

@nerokasai , There are couple of solutions provided. Hope something has worked out for you

Works great! Thank you @Greg_Deckler  @amitchandak for your help!

Greg_Deckler
Super User
Super User

OK, you can create the column you requested using EARLIER to filter ALL of your table and then using MAXX on that to grab the date (this gets you your previous row). Then you could use that date to filter out to the value for the last run. See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395.... Let me know if you need further clarification but the technique is pretty much there. Although you might have an extra step to grab the previous run's distance.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Greg_Deckler
Super User
Super User

I am not 100% following this. Does the data displayed represent sample source data? Or is it what you would like to achieve? If it is sample source data, what is your expected output?


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler My apologies, I should have better clarified! 

 

The data shown displays the overall expected output I want to have.

 

To be more precise, I would like the column "Difference (distance run on recorded date") added to the sample source data, such that the overall data table would be like what I showed.

 

 

Some like this as mentioned by @Greg_Deckler 

 difference of distance run on a recorded event date = 
 var _last = maxx(filter(table,table[Company Name]=earlier(table[Company Name]) && table[Recorded Date]<earlier(table[Recorded Date]) ),table[Total distance run (km)])
 return
 if(is(_last),table[Total distance run (km)], _last-table[Total distance run (km)])

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.