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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
yaman123
Post Patron
Post Patron

Calculate Duration from date time fields

Hi, 

 

Is there a way to calculate the duration in hours and minutes from a date time field? 

 

e.g From Date is 24/08/2021 04:03 and To Date is 25/08/2021 06:08. The Duration should be 26:05. 

 

Thanks

1 ACCEPTED SOLUTION
v-zhangti
Community Support
Community Support

Hi, @yaman123 

 

You can try the following methods.

1. First convert all intervals to seconds.

Seconds =
DATEDIFF ( [Start time], [End time], SECOND )

 

2. Then convert the seconds to hour-minute-second format.

Duration =
INT ( [Seconds] / 3600 ) & ":"
    & INT ( ( [Seconds] - INT ( [Seconds] / 3600 ) * 3600 ) / 60 ) & ":"
    & [Seconds]
        - INT ( ( [Seconds] - INT ( [Seconds] / 3600 ) * 3600 ) / 60 ) * 60
        - INT ( [Seconds] / 3600 ) * 3600

vzhangti_0-1638858706013.png

 

Best Regards,

Community Support Team _Charlotte

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

6 REPLIES 6
v-zhangti
Community Support
Community Support

Hi, @yaman123 

 

You can try the following methods.

1. First convert all intervals to seconds.

Seconds =
DATEDIFF ( [Start time], [End time], SECOND )

 

2. Then convert the seconds to hour-minute-second format.

Duration =
INT ( [Seconds] / 3600 ) & ":"
    & INT ( ( [Seconds] - INT ( [Seconds] / 3600 ) * 3600 ) / 60 ) & ":"
    & [Seconds]
        - INT ( ( [Seconds] - INT ( [Seconds] / 3600 ) * 3600 ) / 60 ) * 60
        - INT ( [Seconds] / 3600 ) * 3600

vzhangti_0-1638858706013.png

 

Best Regards,

Community Support Team _Charlotte

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

 

Also, this will yield a string so I cannot math it further... Is this really the only answer

 

In excel, all I need to do is subract the end date/time from the start date/time and format as [h]:mm to get hours and minutes.  This code is how I have to do that calculation is LESS sophisticated applications, which is annoying. WHY would the mature calculation be removed from the newer tool set?

Thanks, that does give me the reuslts I want but can it be formatted to Time so I can take the Average Time? 

Hi, @yaman123 

 

Power BI does not allow converting more than 24 hours to time. Maybe you can get the average from seconds and convert to time format.

 

Best Regards,

Community Support Team _Charlotte

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

negi007
Community Champion
Community Champion

@yaman123 

You can do it in both powerquery and dax

 

for simplicity I followed below steps in power query

 

1. added a column to calc hours

(Duration.Days([end date]-[start date])*24) + (Duration.Hours([end date]-[start date]))

 

2. added another col to calc minutes

Duration.Minutes([end date]-[start date])

 

3. added another col to calc seconds

Duration.Seconds([end date]-[start date])

 

4. then merge all columns like below

Text.Combine({Text.From([Hours], "en-US"), ":", Text.From([Seconds], "en-US"), Text.From([Minutes], "en-US"), ":", Text.From([Seconds], "en-US"), Text.From([Seconds], "en-US")})

 

below is the output

negi007_0-1638549170915.png

 

i am also attaching pbix file for your reference.

 

you can refer to below link as well for help

https://community.powerbi.com/t5/Desktop/Calculating-day-time-difference/m-p/321189

 

 




Did I answer your question? Mark my post as a solution!
Appreciate your Kudos



Proud to be a Super User!


Follow me on linkedin

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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

Top Kudoed Authors