Forum Discussion

peterg0417's avatar
peterg0417
Icon for Helper III rankHelper III
2 years ago
Solved

Using Dynamic Formatting to convert seconds to hours, minutes and seconds

I wrote a format string using ChatGPT but it's not working 100% correctly in certain situations. Below is the code I'm using for dynamic formatting:   VAR TotalSeconds = [Latest Execution Time (hh...
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi, peterg0417 

    Based on your information, I create a sample table:

     

    Then create a calculated column, try the following DAX:

    Column = 
    VAR TotalSeconds = [Latest Execution Time(ss)]
    VAR Hours = INT(TotalSeconds / 3600)
    VAR Minutes = INT(MOD(TotalSeconds, 3600) / 60)
    VAR Seconds = INT(MOD(TotalSeconds, 60))
    VAR HoursText = IF(Hours > 0, Hours & "h ", "")
    VAR MinutesText = IF(Minutes > 0, Minutes & "m ", "")
    VAR SecondsText = IF(Seconds > 0, Seconds & "s", "")
    RETURN
     IF(TotalSeconds = 0, "0", TRIM(HoursText & MinutesText & SecondsText))

    Here is my preview:

     

    How to Get Your Question Answered Quickly

    Best Regards

    Yongkang Hua

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