cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Helper I
Helper I

Sum of duration

I have time values in format HH:MM:SS and I would like to be able to summarize values in that format. I found code below which is working, but the format is DD:HH:MM:SS, for example 34days02:59:27.

But I dont want to show the summary in days, I want to show total hours, minutes and seconds. Instead of  34days02:59:27 it should show 818:59:27

 

Anyone who can help me solve this?

 

newDuration =

VAR TotalSeconds=SUMX('Tiddata';HOUR('Tiddata'[CopyDuration])*3600+MINUTE('Tiddata'[CopyDuration])*60+SECOND('Tiddata'[CopyDuration]))

VAR Days =TRUNC(TotalSeconds/3600/24)

VAR Hors = TRUNC((TotalSeconds-Days*3600*24)/3600)

VAR Mins =TRUNC(MOD(TotalSeconds;3600)/60)

VAR Secs = MOD(TotalSeconds;60)

return IF(DAYS=0;"";IF(DAYS>1;DAYS&"days";Days&"day"))&IF(Hors<10;"0"&Hors;Hors)&":"&IF(Mins<10;"0"&Mins;Mins)&":"&IF(Secs<10;"0"&Secs;Secs)

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Anonymous
Not applicable

Re: Sum of duration

Try this:

newDuration =
VAR TotalSeconds =
    SUMX (
        'Tiddata';
        HOUR ( 'Tiddata'[CopyDuration] ) * 3600
            + MINUTE ( 'Tiddata'[CopyDuration] ) * 60
            + SECOND ( 'Tiddata'[CopyDuration] )
    )
VAR Hors =
    TRUNC ( TotalSeconds / 3600 )
VAR Mins =
    TRUNC ( MOD ( TotalSeconds; 3600 ) / 60 )
VAR Secs =
    MOD ( TotalSeconds; 60 )
RETURN
    IF ( Hors < 10; "0" & Hors; Hors )
        & ":"
        & IF ( Mins < 10; "0" & Mins; Mins )
        & ":"
        & IF ( Secs < 10; "0" & Secs; Secs )

 

I changed the Hors variable to 

VAR Hors =
TRUNC ( TotalSeconds / 3600 )

and then removed any code having to do with the Days variable.

View solution in original post

3 REPLIES 3
Highlighted
Anonymous
Not applicable

Re: Sum of duration

Try this:

newDuration =
VAR TotalSeconds =
    SUMX (
        'Tiddata';
        HOUR ( 'Tiddata'[CopyDuration] ) * 3600
            + MINUTE ( 'Tiddata'[CopyDuration] ) * 60
            + SECOND ( 'Tiddata'[CopyDuration] )
    )
VAR Hors =
    TRUNC ( TotalSeconds / 3600 )
VAR Mins =
    TRUNC ( MOD ( TotalSeconds; 3600 ) / 60 )
VAR Secs =
    MOD ( TotalSeconds; 60 )
RETURN
    IF ( Hors < 10; "0" & Hors; Hors )
        & ":"
        & IF ( Mins < 10; "0" & Mins; Mins )
        & ":"
        & IF ( Secs < 10; "0" & Secs; Secs )

 

I changed the Hors variable to 

VAR Hors =
TRUNC ( TotalSeconds / 3600 )

and then removed any code having to do with the Days variable.

View solution in original post

Highlighted
Helper I
Helper I

Re: Sum of duration

Hi ChrisHaas,

 

It's working 🙂

 

This really made my day, I really needed this to start off the week right 😄

 

 

BR

Nazeem

Highlighted
Helper I
Helper I

Re: Sum of duration

Hi again,

 

The problem I have now is that the new measure is not working with the date table I have. The total value for a specific date is correct, but in the table I can see all the all values for all dates and not only the one I selected in a filter. This is the case only for the created measu newDuration, for other measures in table Tiddata the date table is working fine. See issue in table below:

 

Skärmklipp.PNG

 

Helpful resources

Announcements
Meet the 2020 Season 2 Power BI Super Users!

Meet the 2020 Season 2 Power BI Super Users!

Find out who's part of the program this season, and welcome the new Super Users.

August 2020 Community Challenge: Can You Solve These?

August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

July 2020 Community Highlights

July 2020 Community Highlights

Learn about the exciting things that happened in July.

Featured Data Story of The Month

Featured Data Story of The Month

All Data Stories Gallery contributions are reviewed for each month. We select a contribution and feature the community member the following month.

Power BI Dev Camp - Developing with .NET Core

Power BI Dev Camp - Developing with .NET Core

Learn how to develop custom web applications for Power BI using .NET Core 3.1 and .NET 5.

Top Solution Authors
Top Kudoed Authors