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

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail
FacebooktwitterlinkedinrssyoutubeFacebooktwitterlinkedinrssyoutube

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.

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.

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.

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.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail
FacebooktwitterlinkedinrssyoutubeFacebooktwitterlinkedinrssyoutube
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 nine 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.
His articles on different aspects of technologies, especially on MS BI, can be found on his blog: https://radacad.com/blog.
He wrote some books on MS SQL BI and also is writing some others, He was also an active member on online technical forums such as MSDN and Experts-Exchange, and was a moderator of MSDN SQL Server forums, and is an MCP, MCSE, and MCITP of BI. He is the leader of the New Zealand Business Intelligence users group. He is also the author of very popular book Power BI from Rookie to Rock Star, which is free with more than 1700 pages of content and the Power BI Pro Architecture published by Apress.
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.

8 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

Leave a Reply

Your email address will not be published. Required fields are marked *