Duration.Hours

Updated on

Duration.Hours is a Power Query M function that extracts the hours portion of a duration value. The function returns an integer representing the number of hours.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

Duration.Hours( duration as nullable duration ) as nullable number

Description

The Duration.Hours function extracts the hours portion (0-23) from a duration.

Examples

Let’s see how we can use the Duration.Hours function.

Duration.Hours to Calculate Duration in Hours

The Duration.Hours function extracts the number of hours from a duration value. For example, a duration of 1.5 days returns 36 hours:

Duration.Hours( #duration( 1, 12, 0, 0) ) // Output: 36

The output is the same when the duration value specifies 36 hours directly:

Duration.Hours(#duration(0, 36, 0, 0)) // Output: 36

Creating a Time List

The Duration.Hours function is particularly useful when generating a time list. Suppose you want to create a list of hourly times starting from 12:00 PM and ending at 4:00 PM on the same day. You can use the following code:

List.Times(
  #time( 12, 0, 0 ),
  Duration.Hours( #time( 16, 0, 0 ) - #time( 12, 0, 0 ) ) + 1,
  #duration( 0, 1, 0, 0 )
)
  • List.Times: Generates a list of times.
  • Start Time: #time(12, 0, 0) specifies the starting time (12:00 PM).
  • Number of Hours: Duration.Hours(#time(16, 0, 0) - #time(12, 0, 0)) + 1 calculates the number of hours between the start and end times. Adding 1 ensures the list includes both the start and end times.
  • Increment: #duration(0, 1, 0, 0) specifies the interval between each time in the list (one hour in this case).

By using Duration.Hours, you can easily compute the number of hours between two times and generate a complete list of times for any given range.

Other functions related to Duration.Hours are:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/duration-hours

2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy