Forum Discussion
elads
7 years agoHelper III
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 "...
- 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
Anonymous
7 years agoNot 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
- elads7 years agoHelper 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
- elads7 years agoHelper III
Hi,
Sorry, I answered you..
- Anonymous7 years agoNot 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- elads7 years agoHelper III
Hi Anonymous,
Do you have an example of such a calculation ..?
- Anonymous3 years agoNot applicable
This was a great solution. Worked like a charm!