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.

Leave a Reply

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