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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
peterg0417
Helper III
Helper III

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 (hhmmss)]
VAR Hours = INT(TotalSeconds / 3600)
VAR Minutes = INT(MOD(TotalSeconds, 3600) / 60)
VAR Seconds = INT(MOD(TotalSeconds, 60))  -- Round seconds to remove extra digits
VAR HoursText = IF(Hours > 0, Hours & "h ", "")
VAR MinutesText = IF(Minutes > 0, Minutes & "m ", "")
VAR SecondsText = IF(Seconds > 0, Seconds & "s", "")
RETURN
    TRIM(HoursText & MinutesText & SecondsText)
 
And this is what it returns: (notice the one line marked with an x)
peterg0417_0-1725980475435.png

 

Does anyone have any ideas on how to fix this? I want to use dynamic formatting so that I can utilize the bars in the cell, so the actual measure needs to stay as an integer instead of text (like the "TimeFormattedMeasure" to the left.)


Edit I want to note that depending on the day selected, different model refresh times display the value incorrectly. My refresh times from yesterday are like this:
peterg0417_0-1725980875985.png

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi, @peterg0417 

Based on your information, I create a sample table:

vyohuamsft_0-1726021949070.png

 

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:

vyohuamsft_1-1726022073831.png

 

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.

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

Hi, @peterg0417 

Based on your information, I create a sample table:

vyohuamsft_0-1726021949070.png

 

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:

vyohuamsft_1-1726022073831.png

 

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.

shafiz_p
Super User
Super User

hI @peterg0417 Considering you want to convert total seconds to hour, minutes, and seconds. Try below Codes:

shafiz_p_0-1725982647162.png

 

 

Hope this helps!!
If this solved your problem, please accept it as a soltuion!!


Best Regards,
Shahariar Hafiz

I tried putting your code in as dynamic formatting but the error is still there. It looks like it's entering 2  digits (74 and 13, for the 2 lines) in between the correct value for seconds.

 

peterg0417_1-1725983498108.png

 

If your time formatted measure is correctly evaluting then use that measure. Since don't know much about your data, it is hard to tell what exactly happening. If you have total seconds, then the provided measure should evalutes correctly. 

Selva-Salimi
Super User
Super User

Hi @peterg0417 

 

Did you have a column that shows the total second? if not craete a column as follows:

duration second = hour('Table'[Duration])*3600+MINUTE('Table'[Duration])*60 + SECOND(('Table'[Duration])
 
and then write a measure as follows:
VAR _sum = SUM('Table'[duration second])

VAR _hour = RIGHT("0" & ROUNDDOWN(_sum / 3600, 0), 2)
VAR _minute = RIGHT("0" & ROUNDDOWN(MOD(_sum, 3600) / 60, 0), 2)
VAR _second = RIGHT("0" & MOD(_sum, 60), 2)

RETURN
_hour & ":" & _minute & ":" & _second

 

If this post helps, then I would appreciate a thumbs up 👍 and mark it as the solution to help the other members find it more quickly. 

Yes. I already have a column that shows duration in seconds.

 

I also have a measure, Latest Execution Time, returns the latest duration (that's the requirement, in case there's more than 1 run per model on a given day.)

 

I need to Latest Execution Time from seconds to minutes and seconds while still keeping it as an integer, therefore I need to do it using dynamic formatting.

then you can use your measure instead of _sum variable

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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