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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

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
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.