2

I have the following DAX in PowerBi to calculate duration, anyone had an idea how to 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"
DaFois
  • 2,166
  • 8
  • 22
  • 38
Nathi Mazibuko
  • 23
  • 1
  • 1
  • 3

4 Answers4

6

Try this instead:

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

Edit: 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
Alexis Olson
  • 36,898
  • 7
  • 37
  • 62
0

If the data is in seconds you can use:

=FORMAT((DATAINSECONDS/3600)/24; "HH:mm:ss")
0
=FORMAT((DATAINSECONDS/3600)/24; "HH:mm:ss")

This solution works with time but 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. Better solution in Power BI is to trasform to duration in power query.

EvilDr
  • 8,131
  • 11
  • 66
  • 121
0
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
  • 7,766
  • 8
  • 46
  • 61
Juliano
  • 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
  • 1
    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