2

I have the following DAX in Power BI to calculate duration. How can I include the seconds?

Total Duration = sumx('Sheet1'; hour('Sheet1'[Duration])) +
TRUNC(sumx('Sheet1'; MINUTE('Sheet1'[Duration]))/60) & ":" &
mod(sumx('Sheet1'; MINUTE('Sheet1'[Duration])); 60)& ":00"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nathi Mazibuko
  • 23
  • 1
  • 1
  • 3

4 Answers4

6

Try this instead:

= FORMAT(SUM('Sheet1'[Duration]), "hh:mm:ss")

If the duration can cover more than 24 hours, then you may need to handle days separately. Something like this:

Format Duration (d.hh:mm:ss) =
VAR TotalDuration = SUM ( 'Sheet1'[Duration] )
VAR TotalDays = TRUNC ( TotalDuration )
VAR HrMinSec = FORMAT ( TotalDuration - TotalDays, "hh:mm:ss" )
RETURN
    TotalDays & "." & HrMinSec

To keep everything in hours:

Format Duration =
VAR TotalDuration = SUM ( 'Sheet1'[Duration] )
VAR TotalHours = TRUNC ( 24 * TotalDuration )
VAR MinSec = FORMAT ( TotalDuration - TotalHours / 24, "nn:ss" )
RETURN
    TotalHours & ":" & MinSec
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alexis Olson
  • 38,724
  • 7
  • 42
  • 64
0

If the data is in seconds you can use:

=FORMAT((DATAINSECONDS/3600)/24; "HH:mm:ss")
Lucia Clifford
  • 136
  • 1
  • 6
0
=FORMAT((DATAINSECONDS/3600)/24; "HH:mm:ss")

This solution works with time, but it doesn't work with duration. If your duration less than 24 hours, time and duration are the same, and the solution works. But if your duration exceeds 24 hours this solution doesn't work. For example, try it for 87000 seconds.

A better solution in Power BI is to transform to duration in a power query.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-1
Total_ = sumx('dados'; hour('dados'[Duration])) + TRUNC(sumx('dados';
         MINUTE('dados'[Duration]))/60) & ":" & mod(sumx('dados'; 
         MINUTE('dados'[Duration])); 60) + 
         TRUNC(SUMX('dados';SECOND('dados'[Duração]))/60) & ":" & 
         mod(sumx('dados'; SECOND('dados'[Duration])); 60)
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Juliano
  • 1
  • 1
  • 1
    try to enclose your code part of the solution within backtick quotes. See the formatting help page of stackoverflow : https://stackoverflow.com/editing-help – ggulgulia Aug 13 '18 at 21:34
  • 2
    Welcome to Stack Overflow! Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation would greatly improve its long-term value](//meta.stackexchange.com/q/114762/206345) by showing _why_ this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Blue Aug 13 '18 at 23:32