Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin 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.
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.
Solved! Go to Solution.
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:
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:
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!
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
Hi @Creative_tree88,
If you use all date with hours you can solve your problem.
Take a look at my example:
I'm using this dAX measure:
Proud to be a 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)
)
Proud to be a Super User! |
|
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:
@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!
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:
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")
Proud to be a Super User!
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
78 | |
76 | |
59 | |
35 | |
33 |
User | Count |
---|---|
100 | |
62 | |
56 | |
47 | |
41 |