Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
michael_knight
Post Prodigy
Post Prodigy

Convert Seconds to DD:HH:MM:SS

Hi,

 

I'm trying to convert Seconds to DD:HH:MM:SS. I used this article to get close, however this doesn't work with both date slicers (Look at the average for the month, for example)

New Time = 
var vSeconds=AVERAGE(Sheet1[Seconds])
var vMinutes=int( vSeconds/60)
var vRemainingSeconds=MOD(vSeconds, 60)
var vHours=INT(vMinutes/60)
var vRemainingMinutes=MOD(vMinutes,60)
var vDays=INT(vHours/24)
var vRemainingHours=MOD(vHours,24)
return
  vDays&" Days & "&
  vRemainingHours&" Hours & "&
  vRemainingMinutes&" Minutes & "& 
  ROUND(vRemainingSeconds,0)& " Seconds"

 

This is the outcome from the measure

help2.PNG

 

When I try to use a Calculate formula with USERELATIONSHIP, it just straight up doesn't work

 

Does anyone have alternatives, or ways I can work around this issue?

 

He's the PBIX: https://www.dropbox.com/s/raye0i6xfvr0h9y/dates.pbix?dl=0

 

Cheers,

Mike

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

You can use measures to coax the data into this (admittedly weird) format.

 

d = FORMAT(int(sum('Duration'[Duration])),"0") & FORMAT(sum('Duration'[Duration]),":HH:NN")
 
Are you sure you want to do that?
 
lbendlin_0-1619456331174.png

but no, you can't have both.

View solution in original post

7 REPLIES 7
lbendlin
Super User
Super User

You can use measures to coax the data into this (admittedly weird) format.

 

d = FORMAT(int(sum('Duration'[Duration])),"0") & FORMAT(sum('Duration'[Duration]),":HH:NN")
 
Are you sure you want to do that?
 
lbendlin_0-1619456331174.png

but no, you can't have both.

lbendlin
Super User
Super User

Here is an eample using #duration

 

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJQ0lEyNAABpVidaCUjJFEjuKgxkqghXNQEIWpqDhaMBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, Seconds = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Month", type date}, {"Seconds", type number}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Duration", each #duration(0,0,0,[Seconds])),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Duration", type duration}})
in
    #"Changed Type1"

 

which you can then plot as needed

lbendlin_0-1619439790988.png

 

That's very promising, my only query is that the I have no way of knowing what 1.16 is in Duration terms. Do you know if there anyway of coverting it into DD:HH:MM while still keeping it in a datatype which allows for visualisation (Not Text)?

ERD
Super User
Super User

Hi @michael_knight ,

You can use this measure if you need an average value:

#d:h:m:s = 
VAR avgNum = AVERAGE(T[Sec])
VAR res = 
INT(avgNum/86400) & ":" &                         //Days
RIGHT("0" & INT(MOD(avgNum/3600,24)),2) & ":" &    //Hours 
RIGHT("0" & INT(MOD(avgNum/60,60)),2) & ":" &       //Minutes
RIGHT("0" & INT(MOD(avgNum,60)),2)                //Seconds
RETURN res

ERD_0-1619434893026.png

If this post helps, then please consider Accept it as the solution ✔️to help the other members find it more quickly.

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. Appreciate your Kudos.

Check out my latest demo report in the data story gallery.

Stand with Ukraine!


Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/

Thank you!

Hi,

 

That measure kinda works. My issue is that it is a text datatype so it's no possible to visualise it. 

 

I'd want to be able to display the average over Months on Months 

lbendlin
Super User
Super User

Remember that datetime values are actually numbers with the integer part being the days and the fraction part being , well the fractions of days.

 

You can thus take your seconds value, multiply by 86400 and you got your datetime value. 

 

Alternatively you can use #duration()  and specify only the seconds value.  It will convert to days/hours/minutes/seconds automatically.

Hi @lbendlin 

I tried using the duration and I had no luck. Are you able to elaborate on what I can do with the seconds? 

 

Cheers

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.