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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
pmeyp
Frequent Visitor

Calculate time between 2 dates in different rows

Hi guys, I need help with this:

given the following set of data, calculate time between the 2 dates in different rows. (Please note that I am converting time and date into number format for dates and times, this way it is easier to calculate date with time accurately, however if this is the wrong approach let me know). Thank you!

 

EVENTEVENT FROM DATE&TIMEEVENT TO DATE&TIME
(1) A44256.333333333344265.66768845

(2) B

44267.9546523445

44268.000000000
(3) C44280.654651232444500.654852256

 

The objective would be to calculate longest time between events, for example:

Event A happened from 44256.3... until 44265.6..., therefore, uneventful time was from 44265.6... until Event B at 44267.9...

 

Anyone has any ideas on how best to approach this? (first event would theoretically return an error but that's fine).

1 ACCEPTED SOLUTION
v-jayw-msft
Community Support
Community Support

Hi @pmeyp ,

 

Create an index column and use the following formula.

Column = 
var last_event = CALCULATE(MAX('Table'[EVENT FROM DATE&TIME]),FILTER('Table','Table'[Index]=EARLIER('Table'[Index])-1))
return
IF(ISBLANK(last_event),BLANK(),'Table'[EVENT FROM DATE&TIME]-last_event)

3.PNG

In addition, you could use DATEDIFF() function so that you don't have to convert time value to number value.

https://docs.microsoft.com/en-us/dax/datediff-function-dax 

 

Best Regards,

Jay

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

View solution in original post

3 REPLIES 3
v-jayw-msft
Community Support
Community Support

Hi @pmeyp ,

 

Create an index column and use the following formula.

Column = 
var last_event = CALCULATE(MAX('Table'[EVENT FROM DATE&TIME]),FILTER('Table','Table'[Index]=EARLIER('Table'[Index])-1))
return
IF(ISBLANK(last_event),BLANK(),'Table'[EVENT FROM DATE&TIME]-last_event)

3.PNG

In addition, you could use DATEDIFF() function so that you don't have to convert time value to number value.

https://docs.microsoft.com/en-us/dax/datediff-function-dax 

 

Best Regards,

Jay

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.
selimovd
Super User
Super User

Hello @pmeyp ,

 

add a new calculated column and calculate the difference:

Difference = myTable[EVENT TO DATE&TIME] - myTable[EVENT FROM DATE&TIME]

 

Then you can search or filter for the MAX and you should have the row with the longest duration.

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍

Best regards
Denis

Blog: WhatTheFact.bi

Hey @selimovd ,

 

This is not what I am looking for -- I want to calculate FROM time in Event B minus TO time in Event A (as this calculation yields the time in between events, not duration of event).

 

Any other ideas? 🤔

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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