Duration.Hours Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

The Duration.Hours function in Power Query returns the hours part of a duration as a whole number. So for a duration of 2 days, 7 hours, and 30 minutes, it gives you 7.

That’s handy when you want to show a time gap as hours and minutes, or label it as days and hours. The catch is that it doesn’t give you the total hours.

A 27-hour gap is stored as 1 day and 3 hours, so Duration.Hours returns 3. I’ll show you how to get the total hours when you need them.

Follow along with the example file

Duration.Hours Function Examples.xlsx

Download

Duration.Hours Function Syntax

= Duration.Hours(duration as nullable duration) as nullable number
  • duration – the duration you want the hours from. Most of the time, this is one datetime subtracted from another, such as [Closed]-[Opened]. It can also be a duration you build with #duration(days,hours,minutes,seconds).

What it returns – the hours portion of the duration as a whole number, without adding in the days, minutes, or seconds. If the duration is null, you get null back.

I’ve tested the Duration.Hours function in Power Query in Excel for Microsoft 365. It’s also part of the Power Query M language that Power BI uses.

When to Use the Duration.Hours Function

Use this function when you need to:

  • Get the hours part of the time between two datetimes
  • Show a duration as separate hours and minutes columns
  • Build a readable label such as 2d 5h from a duration
  • Group or count rows by how many full hours something took
  • Pull the hours out of duration text exported from another system

Example 1: Get the Hours From a Duration

Let’s start with a single duration, so you can see exactly which part the function returns.

Say you have a duration of 2 days, 7 hours, and 30 minutes, and you want its hours part. Create a new blank query (Data > Get Data > From Other Sources > Blank Query) and use this formula in the formula bar:

= Duration.Hours(#duration(2,7,30,0))

Result: 7

Duration.Hours returns 7 for 2 days, 7 hours and 30 minutes

Here, #duration(2,7,30,0) creates a duration of 2 days, 7 hours, 30 minutes, and 0 seconds. Duration.Hours reads only the hours part, so it returns 7.

The 2 days and the 30 minutes are left out. If you want the whole duration in hours, the Duration.TotalHours function gives you 55.5 for the same duration.

Example 2: Get the Hours Between Two Datetimes

This is the most common way you’ll use this function, with a start time and an end time in two columns.

Below is an Excel table named Tickets with the time each support ticket was opened and closed on the same day, and I want the hours each ticket took in a new column.

Support tickets with opened and closed date and time

To follow along, select any cell in the table and go to Data > From Table/Range to open it in the Power Query Editor. If Power Query adds a Changed Type step, use #"Changed Type" in place of Source in the formulas below.

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddColumn(Source,"Hours",each Duration.Hours([Closed]-[Opened]),Int64.Type)

Result: an Hours column with 3, 6, 0, 4, 5, and 1

Hours part of the time each ticket was open

Here, [Closed]-[Opened] subtracts one datetime from the other, which gives a duration. Duration.Hours then returns the hours part of that duration.

The Table.AddColumn function adds the new column, and Int64.Type makes it a whole number column, so you don’t need a separate step to change the type later.

The minutes are dropped, not rounded. T-1044 was open for 4 hours and 50 minutes, so it shows 4, and T-1043 was open for 40 minutes, so it shows 0.

The order of the subtraction matters. If you write [Opened]-[Closed] instead, the duration is negative, so 4 hours and 50 minutes comes back as -4.

Important: Duration.Hours never returns more than 23, because a full day goes into the days part instead. This works here since every ticket closed the same day, and Example 4 shows what to use when a gap can run past a day.

Example 3: Split a Duration Into Hours and Minutes

Since Duration.Hours drops the minutes, you can pair it with the Duration.Minutes function to show both.

Below is an Excel table named DeliveryRuns with the time each delivery van left and came back, and I want the length of each run as hours in one new column and minutes in another.

Delivery runs with departed and returned times

Create a blank query (Data > Get Data > From Other Sources > Blank Query). In the Power Query Editor, open the Advanced Editor (Home > Advanced Editor) and use this code.

The Source line reads the table by its name, so if your table has a different name, change DeliveryRuns to that name.

let
Source = Excel.CurrentWorkbook(){[Name="DeliveryRuns"]}[Content],
#"Added Hours" = Table.AddColumn(Source,"Hours",each Duration.Hours([Returned]-[Departed]),Int64.Type),
#"Added Minutes" = Table.AddColumn(#"Added Hours","Minutes",each Duration.Minutes([Returned]-[Departed]),Int64.Type)
in
#"Added Minutes"

Result: an Hours column with 4, 2, 5, 0, and 5, and a Minutes column with 15, 25, 20, 45, and 50

Each run split into hours and minutes columns

Here, the Added Hours step gets the hours part of each run, and the Added Minutes step gets the minutes part of the same duration. R-01 left at 6:30 AM and came back at 10:45 AM, so it shows 4 hours and 15 minutes.

The two columns together give you the full length of each run, which is easier to read in a report than a decimal like 4.25.

Example 4: Get the Total Hours When a Duration Runs Past a Day

This is where Duration.Hours gives you a number you might not expect.

Below is an Excel table named Repairs with the time each machine breakdown was reported and fixed, and I want the hours each repair took in a new column. Some of these repairs ran into the next day or later.

Machine repairs with reported and fixed date and time

Duration.Hours looks like the obvious choice here, so let’s try it first. Open the table in the Power Query Editor, as in Example 2, and add a new step with this formula:

= Table.AddColumn(Source,"Hours to Fix",each Duration.Hours([Fixed]-[Reported]),Int64.Type)

Result: an Hours to Fix column with 6, 3, 5, 2, and 18

Duration.Hours drops the whole days for repairs over 24 hours

That’s wrong for two machines. The Conveyor Belt took 27 hours, which is 1 day and 3 hours, so Duration.Hours returns only the 3, and the Label Printer took 50 hours but shows 2.

This happens because a duration stores whole days separately, and Duration.Hours only reads the hours left over after the days. So its answer never goes above 23.

Now replace the formula with this one, which uses the Duration.TotalHours function:

= Table.AddColumn(Source,"Hours to Fix",each Duration.TotalHours([Fixed]-[Reported]),type number)

Result: an Hours to Fix column with 6, 27, 5, 50, and 18

Duration.TotalHours returns the total hours for every repair

Here, Duration.TotalHours counts the days as hours too, so the Conveyor Belt gets 27 and the Label Printer gets 50.

It also keeps partial hours, so a repair of 6 hours and 30 minutes would show 6.5. That’s why this formula sets the column to type number, the type for decimal numbers, instead of Int64.Type, the type for whole numbers.

If you only want the full hours, wrap it in the Number.RoundDown function, so 6.5 becomes 6.

So when you want the total length of something in hours, use Duration.TotalHours. Use Duration.Hours when you want the hours part, next to the days or minutes, like in Examples 3 and 5.

Follow along with the example file

Duration.Hours Function Examples.xlsx

Download

Example 5: Show a Duration as Days and Hours

When a duration runs over a day, a label like 2d 5h is often easier to read than a large number of hours.

Below is an Excel table named Shipments with the time each order was shipped and delivered, and I want the transit time as days and hours in a new column.

Shipments with shipped and delivered date and time

Open the table in the Power Query Editor, as in Example 2, and add a new step with this formula:

= Table.AddColumn(Source,"Transit Time",each let d=[Delivered]-[Shipped] in
Text.From(Duration.Days(d))&"d "&Text.From(Duration.Hours(d))&"h",type text)

Result: a Transit Time column with 2d 5h, 1d 6h, 3d 9h, 0d 12h, and 3d 4h

Transit time shown as days and hours

Here, let d=[Delivered]-[Shipped] in works out the duration once and calls it d, so the formula doesn’t repeat the subtraction. The Duration.Days function gets the whole days and Duration.Hours gets the hours left over.

The Text.From function turns each number into text, and & joins the pieces with the d and h labels. ORD-3303 took 3 days, 9 hours, and 15 minutes, so the 15 minutes are left out and it shows 3d 9h.

Example 6: Count Rows by the Number of Hours

Because Duration.Hours returns a whole number, you can use it to put rows into hour groups and count them.

Below is an Excel table named ParkingLog with the time each car entered and left a parking garage on one day. I want to count how many cars stayed under 1 hour, 1 hour, 2 hours, and so on, in a new summary table.

Parking garage log with entry and exit times

Create a new blank query, open the Advanced Editor, and use this code. As in Example 3, change ParkingLog in the Source line if your table has a different name.

let
Source = Excel.CurrentWorkbook(){[Name="ParkingLog"]}[Content],
#"Added Hours Parked" = Table.AddColumn(Source,"Hours Parked",each Duration.Hours([Exit]-[Entry]),Int64.Type),
#"Grouped Rows" = Table.Group(#"Added Hours Parked",{"Hours Parked"},{{"Cars",each Table.RowCount(_),Int64.Type}}),
#"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Hours Parked",Order.Ascending}})
in
#"Sorted Rows"

Result: 2 cars with 0 hours, 3 cars with 1 hour, 2 cars with 2 hours, and 1 car with 3 hours

Number of cars for each full hour parked

Here, the Added Hours Parked step gets the full hours for each car, so a stay of 1 hour and 15 minutes counts as 1. A 0 means the car left in under an hour.

The Grouped Rows step uses the Table.Group function to make one row per hour value, and Table.RowCount(_) counts the cars in each group. You can also build this step from the ribbon with Home > Group By, using Count Rows as the operation.

The Sorted Rows step puts the hour groups in order from 0 up, so the summary reads in order no matter how the cars are listed in the table.

Important: This works because every stay here is under a day. If a car can stay 24 hours or more, a 25-hour stay would land in the 1 hour group, so use Number.RoundDown(Duration.TotalHours([Exit]-[Entry])) in the Added Hours Parked step instead.

Example 7: Get the Hours From Durations Stored as Text

Phone systems and time trackers often export durations as text in an hours:minutes:seconds format, like 01:25:40.

Below is an Excel table named CallLog with the total talk time for each support agent, stored as text, and I want the hours in a new column. Kendra Morgan hasn’t taken any calls yet, so that cell is blank.

Talk time per agent stored as text, with one blank

If you pass this text straight to Duration.Hours, every filled row gives an error that says “We cannot convert the value “01:25:40″ to type Duration.” The function only accepts a duration, so the text needs to be converted first.

Create a new blank query, open the Advanced Editor, and use this code. As in Example 3, change CallLog in the Source line if your table has a different name.

let
Source = Excel.CurrentWorkbook(){[Name="CallLog"]}[Content],
#"Added Hours" = Table.AddColumn(Source,"Hours",each Duration.Hours(Duration.FromText([Talk Time])),Int64.Type)
in
#"Added Hours"

Result: an Hours column with 1, 0, 2, null, 3, and 0

Hours part of each agent's talk time

Here, the Duration.FromText function reads the text as a duration, and Duration.Hours returns its hours part. Calvin Fisher talked for 59 minutes and 59 seconds, which is still under an hour, so he gets 0.

The blank cell comes into Power Query as null. The Duration.FromText function returns null for it, and Duration.Hours passes the null through, so Kendra Morgan gets null instead of an error.

Other Excel Articles You May Also Like:

  • Duration.From Function in Power Query M
  • Duration.TotalDays – Use Duration.TotalDays in Power Query M to get the days in a duration.
  • Time.Hour – Use Time.Hour in Power Query M to get the hour from a time or datetime.
  • DateTime.From – Use DateTime.From in Power Query M to convert values to date and time.
  • Date.Day – Use Date.Day in Power Query M to get the day of the month from a date.
  • Date.DaysInMonth – Use Date.DaysInMonth in Power Query M to get the number of days in a month.
  • Date.AddDays – Use Date.AddDays in Power Query M to add or subtract days from a date.
  • DateTime.LocalNow – Use DateTime.LocalNow in Power Query M to get the current date and time.

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.