How to convert seconds into hh:mm format in Power Bi?
Asked Answered
B

8

6

In Power Bi, I have a table that contains Name and TimeSpent by user in seconds. I want to convert total seconds spent by all users into duration format (hh:mm)

When I am getting seconds in hh:mm format for each user from database query, the values are coming up like these 12:63 etc. After importing these values into power bi, I tried to set its datatype to DateTime format but power bi shows an error saying that it is not a valid value. If I set the datatype of the column as string then strings dont add up.

What can be the ideal way to do it?

Brannen answered 9/8, 2017 at 7:31 Comment(1)
#1262997Arquebus
A
4

You can try the following DAX:

HHMMSS = 
INT(Table[TimeSpent] / 3600) & ":" &
RIGHT("0" & INT((Table[TimeSpent] - INT(Table[TimeSpent] / 3600) * 3600) / 60), 2) & ":" &
RIGHT("0" & MOD(Table[TimeSpent], 3600), 2)

Source

Attitudinarian answered 9/8, 2017 at 16:4 Comment(5)
suppose my total seconds are 1625038 which are 151 hours and 23 minutes. Using this formula, the hours are coming 51:23, 1 is getting truncated.Brannen
@Brannen I have updated the DAX. Please check if it works.Attitudinarian
Hi @Brannen Does this help solving the issue after all? If so, you can accept this answer to positively close the question.Attitudinarian
I changed it a little IF(INT(Query1[Total Time] / 3600) = 0,"00",INT(Query1[Total Time] / 3600)) & ":" & RIGHT("0" & INT((Query1[Total Time] - INT(Query1[Total Time] / 3600) * 3600) / 60), 2)Brannen
can anyone help in adding datepart as well in above query!Facility
N
9

you can solve this in one line:

measure = FORMAT(TIME(0, 0, tableNAME[your_column]), "HH:mm:ss")
Nee answered 14/6, 2019 at 18:55 Comment(2)
For this to work on a new calculated column, you need to replace ; with ,Thrombophlebitis
if you only want minutes and seconds its "nn:ss"Trichite
A
4

You can try the following DAX:

HHMMSS = 
INT(Table[TimeSpent] / 3600) & ":" &
RIGHT("0" & INT((Table[TimeSpent] - INT(Table[TimeSpent] / 3600) * 3600) / 60), 2) & ":" &
RIGHT("0" & MOD(Table[TimeSpent], 3600), 2)

Source

Attitudinarian answered 9/8, 2017 at 16:4 Comment(5)
suppose my total seconds are 1625038 which are 151 hours and 23 minutes. Using this formula, the hours are coming 51:23, 1 is getting truncated.Brannen
@Brannen I have updated the DAX. Please check if it works.Attitudinarian
Hi @Brannen Does this help solving the issue after all? If so, you can accept this answer to positively close the question.Attitudinarian
I changed it a little IF(INT(Query1[Total Time] / 3600) = 0,"00",INT(Query1[Total Time] / 3600)) & ":" & RIGHT("0" & INT((Query1[Total Time] - INT(Query1[Total Time] / 3600) * 3600) / 60), 2)Brannen
can anyone help in adding datepart as well in above query!Facility
T
2

DAX code:

 = TIME(0,0,SUM('Table'[Timespent]))

Then click the modelling tab and choose Format - Date Time and choose the appropriate format.

Tergal answered 23/4, 2019 at 12:37 Comment(0)
E
1

Had a similar question but for D:HH:MM:SS, code below if it's of use.

DurTime (meas) = 
VAR vDur = <<<duration in CALCULATE(SUM(seconds)) >>>
  RETURN INT(vDur/86400) & ":" &                     //Days
    RIGHT("0" & INT(MOD(vDur/3600,24)),2) & ":" &    //Hours 
    RIGHT("0" & INT(MOD(vDur/60,60)),2) & ":" &      //Minutes
    RIGHT("0" & INT(MOD(vDur,60)),2)                 //Seconds
Ectomere answered 15/3, 2018 at 6:4 Comment(0)
O
0

That's a better formula, which I'm using in PBI: HHMMSS = FORMAT(TIME(int(Table[TimeSpent] / 3600); int(mod(Table[TimeSpent]; 3600) / 60);int(mod(mod(Table[TimeSpent]; 3600); 60))); "HH:mm:ss")

Owens answered 21/3, 2018 at 15:19 Comment(0)
M
0

I wanted a Power BI Measure wich is easy to read for this problem, code below if it's of use.

HH:MM = 
VAR TotalDuration = SUM(tableNAME[your_column] ) //if you use a measure just leave the SUM part out
VAR TotalHours = TRUNC (TotalDuration/3600)
VAR Min_ =  FORMAT(TRUNC(TotalDuration - TotalHours * 3600),"00")
RETURN
    TotalHours & ":" & Min_

The solution is adopted from the top answer of this question PowerBi Duration calculation in hh:mm:ss

Mercuri answered 11/1, 2022 at 11:42 Comment(0)
O
0
ROUNDDOWN(TableName[ColumnName]/3600,0)&":"&ROUNDDOWN(TableName[ColumnName]/60,0)-ROUNDDOWN(TableName[ColumnName]/3600,0)*60&":"&TableName[ColumnName]-ROUNDDOWN(TableName[ColumnName]/3600,0)*3600-(ROUNDDOWN(TableName[ColumnName]/60,0)-ROUNDDOWN(TableName[ColumnName]/3600,0)*60)*60
Osteogenesis answered 17/3, 2023 at 12:25 Comment(1)
This will give appropriate result, test it.Platitudinize
W
-1

I have solved a template for this:

This will enable to convert seconds into D:HH:MM:SS format

D:HH:MM:SS = 

VAR D = int([AVG CBT]/86400)

VAR A_D = [AVG CBT]- D * 86400

VAR HH = RIGHT(0 & INT(A_D/3600),2)

VAR A_HH = A_D - HH * 3600

VAR  MM = RIGHT(0 & INT(A_HH/60),2)

VAR A_MM = A_HH - MM*60

VAR SS = RIGHT(0 & INT(A_MM),2)

RETURN
D & ":" & HH & ":" & MM & ":" & SS

I think it will resolve most needs.

Kishan.

Walleyed answered 11/3, 2023 at 11:31 Comment(1)
While pointless to begin with (see Jonas Correa's answer), I find timo's 2018 rendition more readable.Shealy

© 2022 - 2025 — McMap. All rights reserved.