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 August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Calculating time difference between rows with slicers

I have a DAX formula that is calculating the time difference between each consecutive row in a table visulaization. It works when I have only one process selected, but when I select multiple processes the Time Diff measure is incorrect. See images below.

 

DAX formula: 

Time Diff = VAR Previous = CALCULATE ( MAX ('Main Table'[Date.Time] ), FILTER ( ALLSELECTED ( 'Main Table' ), 'Main Table'[Date.Time] < MAX ( 'Main Table'[Date.Time] ) ) ) RETURN DATEDIFF ( Previous, MAX ( 'Main Table'[Date.Time] ), MINUTE )

 

One selectionOne selectionTwo selectionTwo selection

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

It looks like it is correctly calculating the difference between rows (regardless of the Process).  If you'd like to keep the Process in context, you need to add VALUES('Main Table'[Process]) to your Calculate().

 

If this works for you, please mark it as solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

5 REPLIES 5
harshnathani
Community Champion
Community Champion

 

 

Hi @Anonymous ,
 
Incase you want difference between each row
 

Time Difference =

var _a = MAX('Table'[Date/Time])
var _previousdate = CALCULATE(MAX('Table'[Date/Time]), FILTER(ALLSELECTED('Table'[Date/Time]),'Table'[Date/Time] < _a) )

RETURN
CALCULATE(DATEDIFF(_previousdate,_a,MINUTE))

1.jpg
 
 
This measure too gives the same results.
 
Time Difference =
VAR _a =
MAX ( 'Table'[Date/Time] )
VAR _previousdate =
CALCULATE (
MAX ( 'Table'[Date/Time] ),
FILTER (
ALLEXCEPT('Table', 'Table'[Process])
,
'Table'[Date/Time] < _a
)
)
RETURN
DATEDIFF (
_previousdate,
_a,
MINUTE
)
 
Regards,
Harsh Nathani
Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)


Greg_Deckler
Community Champion
Community Champion

I would start by dropping your ALLSELECTED and not using CALCULATE.

 

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

 

I have a great version of that in my book, DAX Cookbook. If you can post sample data, I'll try to adapt it for your situation. Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.

 

 



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...
Greg_Deckler
Community Champion
Community Champion

@Anonymous Seems like the issue is that when you are getting the MAX, it is pulling for Line 11 or basically the wrong thing and this is likely because of your ALLSELECTED which I try not to use if at all possible and not sure why you need it. I would start by dropping it.

 

This post may help. 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/339586

 

I have a great version of that in my book, DAX Cookbook.

 

Also, if you can paste some of your source data or sample source data I can probably adapt the formula from my book for you. Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.



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...
mahoneypat
Microsoft Employee
Microsoft Employee

It looks like it is correctly calculating the difference between rows (regardless of the Process).  If you'd like to keep the Process in context, you need to add VALUES('Main Table'[Process]) to your Calculate().

 

If this works for you, please mark it as solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

That works! Thank you

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.