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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
cheid_4838
Helper IV
Helper IV

Drive Duration Calculation

I am probably making this harder than it needs to be, but I am trying to calculate the driving time between each stop sequence for each order.  For example for stop 2 for order 6005581 I need to know how long the drive was from departure time from stop sequence #1 to the arrival time at Stop 2.  I thought about using the OFFSET function, but not sure that is the best way to do it?  Any help or suggestion would be appreciated?  Thanks.  

 

cheid_4838_2-1710343628353.png

 

 

 

 

7 REPLIES 7
Greg_Deckler
Community Champion
Community Champion

@cheid_4838 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....
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
 ( __Current - __Previous ) * 1.



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Thanks for the quick response and your help.  I think I understand what you provided, but have a couple of questions.

 

1.  What value would I put between the brackets in this statement?   VAR __Current = [Value]

2.  Since I have multiple orders, how do I make sure that the driver duration is only calculated within each order and that the time won't be calculated across orders like the example I provided where there were two orders?   

@cheid_4838 Since you want to subtract the previous departure time from the current arrival time, then you would put VAR __Current = [Arrival Date & Time].

 

You will want to capture the current driver and perhaps order similar to the __Current variable. You then need to use those when filtering to find your previous row.



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

I must be missing something because I keep getting a syntax error message.  What am I missing?  

 

Table = stops

 

Drive Duration =

VAR CURRENT = stops[stp_arrivaldate]

VAR PreviousDate = MAXX(FILTER(stops,stops[stp_departuredate] < EARLIER(stops[stp_departuredate]

VAR Previous = MAXX(FILTER(stops,stops[stp_departuredate]= PreviousDate,stops[stp_arrivaldate])

RETURN
(_Current - _Previous)*1

@cheid_4838 

 

Drive Duration =

VAR CURRENT = stops[stp_arrivaldate]

VAR PreviousDate = MAXX(FILTER(stops,stops[stp_departuredate] < CURRENT), [stp_departuredate]) 


RETURN
(_Current - _PreviousDate)*1.

 



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

I appreciate the help with this, but for some reason it's still not working.  From the looks of it, it should work.  Do you know what could be driving this error message?  Thanks.

 

 

cheid_4838_0-1710370818187.png

 

 

@cheid_4838 For whatever reason and to my knowledge undocumented, CURRENT seems to be some sort of reserved word, which is why it is always a good idea to prefix variable names with __ or _ or some other character. So just do that.



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

Fabric Community Sticker Design Challenge Barcelona Carousel

Fabric Community Sticker Challenge - Barcelona 2026

If you love stickers, then you will definitely want to check out our community sticker challenge, Barcelona edition!

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors