Calculate Duration in Days Hours Minutes and Seconds Dynamically in Power BI using DAX

In Power Query, there is an easy way to use Duration and get the number of days, hours, minutes and seconds from it. However, sometimes you need this calculation to be dynamic as a measure in DAX. I had that requirement too. And I wrote a simple DAX calculation which will give you the result. If you like to know more about Power BI, read Power BI book from Rookie to Rock Star.

You have the duration in Minutes and want to calculate Hours/Minutes

If you have the duration in Minutes and want to calculate it in hours and minutes. this is a DAX measure that can help with that.

*Note: The [Duration in Minutes] part of the expression below should be replaced with the value/field/measure you have the minutes coming from it.

String Duration in Hours and Minutes = 
var vMinues=[Duration in Minutes]
var vHours=int( vMinues/60)
var vRemainingMinutes=MOD(vMinues, 60)
return
  vHours&" Hours & "& vRemainingMinutes& " Minutes"

And here is the result:

You have the duration in Seconds and want to calculate Hours/Minutes/Seconds

If you have the duration in seconds and want to have a result that tells you how many hours, minutes and seconds you have in total. here is the expression for that.

*Note: The [Duration in Seconds] part of the expression below should be replaced with the value/field/measure you have the seconds coming from it.

String Duration in Hours Minutes and Seconds = 
var vSeconds=[Duration in Seconds]
var vMinutes=int( vSeconds/60)
var vRemainingSeconds=MOD(vSeconds, 60)
var vHours=INT(vMinutes/60)
var vRemainingMinutes=MOD(vMinutes,60)
return
  vHours&" Hours & "&
  vRemainingMinutes&" Minutes & "& 
  vRemainingSeconds& " Seconds"

And the result looks like this:

You have the duration in Seconds and want to calculate Days/Hours/Minutes/Seconds

If you want to also add days to the total, which would look like: “x” days, “y” hours, “z” minutes, and “s” seconds. then you can use the expression below:

*Note: The [Duration in Seconds] part of the expression below should be replaced with the value/field/measure you have the seconds coming from it.

String Duration in Days Hours Minutes and Seconds = 
var vSeconds=[Duration in 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 & "& 
  vRemainingSeconds& " Seconds"

The result will look like this:

Why not calculate the duration in Power Query?

If you have worked with Power Query, you might know that there is a much easier way to calculate these from a duration data type in Power Query. However, Power Query does all the calculations as pre-calculated. Sometimes you do need these to be dynamically calculated in Power BI using DAX. Like the example below; you might want to choose the date range of a timesheet and see in total, how many days, hours, minutes and seconds have been recorded.

Summary

Hope the calculations above help you in your questions. If you have any questions about Power BI, or DAX specifically, feel free to ask below in the comments or reach out to me or one of our team members for a one-on-one consultation session.

Reza Rad on FacebookReza Rad on LinkedinReza Rad on TwitterReza Rad on Youtube
Reza Rad
Trainer, Consultant, Mentor
Reza Rad is a Microsoft Regional Director, an Author, Trainer, Speaker and Consultant. He has a BSc in Computer engineering; he has more than 20 years’ experience in data analysis, BI, databases, programming, and development mostly on Microsoft technologies. He is a Microsoft Data Platform MVP for 12 continuous years (from 2011 till now) for his dedication in Microsoft BI. Reza is an active blogger and co-founder of RADACAD. Reza is also co-founder and co-organizer of Difinity conference in New Zealand, Power BI Summit, and Data Insight Summit.
Reza is author of more than 14 books on Microsoft Business Intelligence, most of these books are published under Power BI category. Among these are books such as Power BI DAX Simplified, Pro Power BI Architecture, Power BI from Rookie to Rock Star, Power Query books series, Row-Level Security in Power BI and etc.
He is an International Speaker in Microsoft Ignite, Microsoft Business Applications Summit, Data Insight Summit, PASS Summit, SQL Saturday and SQL user groups. And He is a Microsoft Certified Trainer.
Reza’s passion is to help you find the best data solution, he is Data enthusiast.
His articles on different aspects of technologies, especially on MS BI, can be found on his blog: https://radacad.com/blog.

11 thoughts on “Calculate Duration in Days Hours Minutes and Seconds Dynamically in Power BI using DAX

  • Hi Reza

    You may want to check out an alternative solution from the moderator of r/PowerBI:
    https://www.reddit.com/r/PowerBI/comments/9urrmm/show_time_data_as_total_hours_and_chart_it/

    This solution sorts correctly and can even be used in visuals: https://imgur.com/Lox6QvN

    Unfortunately, it’s now a bit out-dated because custom formatting is available in Power BI (it used Tabular Editor), however, perhaps it’s worth of mentioning in your article.

  • Hi, nice explanation.
    what I do not understand is why Duration Data type isn’t available in Data Modeling.
    I have two columns with date/time information. if I subtract those two i get a duration.
    this duration is well visible in query editor and under transform I can even change data type to Duration.
    it is in form day.hh:mm:ss which is great. but after close and apply this gets damaged and become decimal number. do you know why there is not under data modeling formating duration as well?

    • Having duration data type in the modelling would have been very nice, I agree.
      but unfortunately not available.
      I always bring the difference in the most detailed number (minutes or seconds) and then do the rest of the calculation in DAX if I want this to be dynamic.
      Cheers
      Reza

  • This is great. How about an additional challenge: very often organizations have to calculate time (total hours) and then deduct holidays, weekends, training days. In excel you can do this with NETWORKINGDAYS formula and additional table with days to deduct. How would you handle this in DAX?

    • Same thing in DAX and Power BI.
      You would need to have a proper date table with working days and non-working days
      then you can use that in your DAX expressions as extra filter logic.
      Cheers
      Reza

  • i have one field “Datetime” i need create hours, last hours, today, yesterday and last 5 days dax calculation.

    “value” is another numeric field. this is avg(value), max, min, current/lastvalue.

    x- axis is datetime
    y-axix is avgerage of value, when user select hours, last hours, today, yesterday and last 5 days dynamically change in my report. and as well as Avg. max,min.current\last.

    hot create this dax calculation in power bi

    • Hi vnk
      You need to be a bit more specific about where you want to use this calculation for? is it going to be slicer? or is this a calculation of count/avg/aggregation of something based on the last hours, days etc?
      based on that, then you would know do you need a measure or calculated column for that.
      and then you can use DAX functions with combination of time intelligence functions and subtracting the datetime field from the NOW function

      Cheers
      Reza

  • Thanks for all your help. I have carried out the procedure above, which I get the minutes converted to hours, and hours to days, which is great, but when I move my date, from a day to several days later it does not give me a total of the days and hours and minutes, it only gives me the total of the one I click on. What am I missing?

  • Thanks Reza, it’s always nice when you need a DAX Solution and BOOM there it is. Love your use of variables as well.
    Best,
    Dan

Leave a Reply