Forum Discussion
datediff in HH:MM format
Hi,
I have two columns:
1. Start time
2. End time.
I want to know the duration of time between the two columns in hour and minute format (HH:MM)
Then I want to display their summary on the "CARD" in the same format.
Attached is a sample example.
Thank you so much for helping.
BR,
Alon
- Anonymous7 years agoCreate a calculated column that for each row will store the number of seconds between the two dates. Then take the measure I've shown you and operate on the SUM of the column. So, you should create a measure like [Total Seconds] = SUM ( T[Seconds] ) and then use its value in my measure replacing the part which is responsible for calculating the seconds...
Best
Darek
14 Replies
- AnonymousNot applicable
Duration HH:MM:SS = // We start with a duration in number of seconds VAR Duration = DATEDIFF( 'Table'[StartDate], 'Table'[EndDate], SECOND ) // There are 3,600 seconds in an hour VAR Hours = INT ( Duration / 3600) // There are 60 seconds in a minute VAR Minutes = INT ( MOD( Duration - ( Hours * 3600 ),3600 ) / 60) // Remaining seconds are the remainder of the seconds divided by 60 after subtracting out the hours VAR Seconds = ROUNDUP(MOD ( MOD( Duration - ( Hours * 3600 ),3600 ), 60 ),0) // We round up here to get a whole number // These intermediate variables ensure that we have leading zero's concatenated onto single digits // Hours with leading zeros VAR H = IF ( LEN ( Hours ) = 1, CONCATENATE ( "0", Hours ), CONCATENATE ( "", Hours ) ) // Minutes with leading zeros VAR M = IF ( LEN ( Minutes ) = 1, CONCATENATE ( "0", Minutes ), CONCATENATE ( "", Minutes ) ) // Seconds with leading zeros VAR S = IF ( LEN ( Seconds ) = 1, CONCATENATE ( "0", Seconds ), CONCATENATE ( "", Seconds ) ) // Now return hours, minutes and seconds with leading zeros in the proper format "hh:mm:ss" RETURN CONCATENATE ( H, CONCATENATE ( ":", CONCATENATE ( M, CONCATENATE ( ":", S ) ) ) )This is code from:
https://community.powerbi.com/t5/Community-Blog/Aggregating-Duration-Time/ba-p/22486
Best
Darek
- AnonymousNot applicable
This was a great solution. Worked like a charm!
- eladsHelper III
Hi,
Thank you for your answer,
The result is good, but I have to summarize the hours and minutes.
I don't seem to be able to do that because this field is text.BR,
Alon
- AnonymousNot applicableMate, a little bit of thinking goes a long way... If you have to summarize, then the measure should first add up all the seconds and then apply the above formula to this sum. Is this not obvious?
Best
Darek
- MariuszCommunity Champion
Hi elads
You can do something like below, you can adjust the code to show only hh:mm if you wish, but the below will address the issue of days when the sum goes over 24.
Measure = VAR _all = SUMX( 'Table', 'Table'[End] - 'Table'[Start] ) * 1 VAR _days = INT( _all ) VAR _time = _all - _days RETURN FORMAT( _days, "#0D-" ) & FORMAT( _time, "hh:mm:ss")
Best Regards,
Mariusz
If this post helps, then please consider Accepting it as the solution.
Please feel free to connect with me.- AnonymousNot applicable
Thnaks for information