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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply

Difference between two times when time goes past midnight

Hi all - I have a dataset, and I'm trying to calculate the time between two dates.  The basic 'DATEDIFF' in minutes DAX works fine until I get to a line of data which is looking at a time which starts before midnight and ends after midnight, as in the example below.  I get minus numbers.  What's the bext way to deal with this?  Looking for a solution - thanks all.

 

Test_example.png

2 ACCEPTED SOLUTIONS

DAX formula you can use to create a calculated column that displays the time difference in the format "X hours Y minutes":

 

Time Diff Formatted =
VAR TotalMinutes = [Time Diff Req to Event]
VAR Hours = INT(TotalMinutes / 60)
VAR Minutes = MOD(TotalMinutes, 60)
RETURN
Hours & " hours " & Minutes & " minutes"

 

For example:

  • If the difference is 56 minutes, it will show as "0 hours 56 minutes".
  • If the difference is 125 minutes, it will show as "2 hours 5 minutes".

View solution in original post

Anonymous
Not applicable

Hi @Creative_tree88 

 

Thanks for the reply from 123abc , bhanu_gautam , _AAndrade  and Kedar_Pande .

 

If you need to display the average time in the card in the format of HH:MM, the following test is for your reference:

 

Create a measure as follows

average = 
VAR _sum = SUM('Table'[Time Diff Req to Event])
VAR _count = COUNTROWS('Table')
VAR _averageSecond = DIVIDE(_sum, _count) * 60
VAR _NumberOfHours = QUOTIENT(_averageSecond, 3600)
VAR _NumberOfMinutes = QUOTIENT( MOD(_averageSecond, 3600), 60)
RETURN
FORMAT(_NumberOfHours, "[hh]") & ":" & FORMAT(_NumberOfMinutes, "[mm]")

 

Output:

vxuxinyimsft_0-1728370592785.png

 

Best Regards,
Yulia Xu

 

If our answers helps, then please consider Accept them as the solution to help the other members find it more quickly. Thank you!

View solution in original post

10 REPLIES 10
Kedar_Pande
Super User
Super User

@Creative_tree88 ,

DAX formula:
Time Difference (Minutes) =
VAR ReqTime = MAX('Table'[Req Time Formatted])
VAR EventTime = MAX('Table'[Event Time Formatted])
RETURN
IF(
ReqTime > EventTime,
DATEDIFF(EventTime, ReqTime, MINUTE),
DATEDIFF(EventTime, ReqTime + TIME(24, 0, 0), MINUTE)
)

If this helped, a Kudos 👍 or Solution mark would be great!
Cheers,
Kedar Pande
www.linkedin.com/in/kedar-pande

_AAndrade
Super User
Super User

Hi @Creative_tree88,

If you use all date with hours you can solve your problem.
Take a look at my example:

_AAndrade_0-1727955678723.png



I'm using this dAX measure:

_AAndrade_1-1727955703787.png

 





Did I answer your question? Mark my post as a solution! Kudos are welcome.

Proud to be a Super User!




bhanu_gautam
Super User
Super User

@Creative_tree88 , Try using 

TimeDifference =
VAR StartTime = [StartTimeColumn]
VAR EndTime = [EndTimeColumn]
RETURN
IF (
EndTime < StartTime,
DATEDIFF(StartTime, EndTime + 1, MINUTE),
DATEDIFF(StartTime, EndTime, MINUTE)
)




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

Proud to be a Super User!




LinkedIn






123abc
Community Champion
Community Champion

in your sample negitive value a occure bcz " event time" corsses midnitght time, and BI is define it as a negative difference btwn two time.

 

first of all you need to ensur both time REQ TIME and EVENT TIME are in valid time format then to sovle the issue taht where the event time is past midnight we can change the logic by adding  24 hours to the event time if its samllar then the request time.

 

pleaase try this dax:

 

Time Diff Req to Event =
VAR ReqTime = VALUE(HOUR([Req Time Formatted]) * 60 + MINUTE([Req Time Formatted]))
VAR EventTime = VALUE(HOUR([Event Time Formatted]) * 60 + MINUTE([Event Time Formatted]))
VAR AdjustedEventTime = IF(EventTime < ReqTime, EventTime + 1440, EventTime) -- 1440 minutes in 24 hours
RETURN AdjustedEventTime - ReqTime

 

 

hopefully this solve the issue.

 

that you don’t get negative values when the event crosses midnight.

 

@123abc that works really well.  How do I convert the resulting calculated column into the format hours and minutes.  So, if it was 56 minutes...it shows 0 hours 56 minutes etc etc.  Thank you!

If this solved your prob please accept it as a solution so that it will help other community members

 

Regards:

Your Kudoed

Ali Abbas

DAX formula you can use to create a calculated column that displays the time difference in the format "X hours Y minutes":

 

Time Diff Formatted =
VAR TotalMinutes = [Time Diff Req to Event]
VAR Hours = INT(TotalMinutes / 60)
VAR Minutes = MOD(TotalMinutes, 60)
RETURN
Hours & " hours " & Minutes & " minutes"

 

For example:

  • If the difference is 56 minutes, it will show as "0 hours 56 minutes".
  • If the difference is 125 minutes, it will show as "2 hours 5 minutes".

@123abc that's great - many thanks.  I'd like to show the average on a card, but obviously power BI can't show this as it's a string.  How can I show the average in format, as example '1h:50m' - on a card??  I could show this in minutes, as per original calculation, but for the user I really think they'd prefer to see it in hours and minutes.  Many thanks!  

Anonymous
Not applicable

Hi @Creative_tree88 

 

Thanks for the reply from 123abc , bhanu_gautam , _AAndrade  and Kedar_Pande .

 

If you need to display the average time in the card in the format of HH:MM, the following test is for your reference:

 

Create a measure as follows

average = 
VAR _sum = SUM('Table'[Time Diff Req to Event])
VAR _count = COUNTROWS('Table')
VAR _averageSecond = DIVIDE(_sum, _count) * 60
VAR _NumberOfHours = QUOTIENT(_averageSecond, 3600)
VAR _NumberOfMinutes = QUOTIENT( MOD(_averageSecond, 3600), 60)
RETURN
FORMAT(_NumberOfHours, "[hh]") & ":" & FORMAT(_NumberOfMinutes, "[mm]")

 

Output:

vxuxinyimsft_0-1728370592785.png

 

Best Regards,
Yulia Xu

 

If our answers helps, then please consider Accept them as the solution to help the other members find it more quickly. Thank you!

Use the FORMAT function https://learn.microsoft.com/en-us/dax/format-function-dax

Example: FORMAT( 'Your Measure', "hh:nn")





Did I answer your question? Mark my post as a solution! Kudos are welcome.

Proud to be a Super User!




Helpful resources

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

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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