Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Duration.Hours: How to Use It

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Duration.Hours extracts the hour component from a duration value in Power Query, returning a number from 0 to 23. It does not convert a duration into total hours; it only reads the hours portion of the duration. For example, Duration.Hours(#duration(2, 5, 30, 0)) returns 2, not 53.5. If you need total hours, use Duration.TotalHours instead. This guide shows where to find the function, how to use it in custom columns and queries, and how to avoid the most common mistakes.

What You'll Need

  • Power BI Desktop, Excel with Power Query (Data > Get & Transform Data), or Analysis Services / Power Query Online where M is available.
  • A table that already contains a duration column, or the ability to create one from start and end times using Duration.From or subtraction of two datetime values.
  • Basic familiarity with the Power Query Editor: adding a custom column, editing the formula bar, or writing an M query.

Step-by-Step

  1. Open Power Query Editor. In Excel, select your data and go to Data > Get & Transform Data > From Table/Range. In Power BI Desktop, use Home > Transform data. The exact menu labels can differ slightly by version, device and region.
  2. Confirm you have a duration column. A duration column shows values like 2.05:30:00 in the Power Query Editor and has a type icon that looks like a clock. If your column is text, use Transform > Data Type > Duration to convert it. If you have separate start and end datetime columns, add a custom column with a formula such as [End] - [Start] and set its type to Duration.
  3. Add a custom column. Go to Add Column > Custom Column. Give it a name such as HoursPart. In the formula box, enter:
    Duration.Hours([YourDurationColumn])
    Replace YourDurationColumn with the actual column name. Column names with spaces need square brackets and the exact name, for example Duration.Hours([Elapsed Time]).
  4. Check the result type. Duration.Hours returns a number. After you click OK, the new column should show whole numbers from 0 to 23. If the column shows errors, the source values are not valid durations. Select the column and use Transform > Data Type > Duration to fix the type, then refresh the custom column.
  5. Use it directly in the formula bar or Advanced Editor. If you prefer not to add a column, you can call the function inside a query step. For example, in the Advanced Editor, add a step like:
    HourPart = Table.AddColumn(PreviousStep, "HoursPart", each Duration.Hours([DurationColumn]))
    This is the same operation as the Custom Column dialog and is useful when you are writing or reviewing M code.
  6. Apply and close. In Excel, select Close & Load to send the result to a worksheet or the Data Model. In Power BI Desktop, select Close & Apply. The new numeric column is now available for visuals, filters and further calculations.

Common Mistakes

  • Expecting total hours. Duration.Hours only returns the hour component. A duration of 1.12:00:00 returns 12, not 36. Use Duration.TotalHours when you need the full number of hours, including the days converted to hours.
  • Passing a datetime instead of a duration. Duration.Hours expects a duration value. If you pass a datetime, you get an error. Convert first with Duration.From, or subtract two datetimes to produce a duration.
  • Using the wrong column name. M is case-sensitive and requires exact names. If the column name contains spaces or special characters, wrap it in square brackets exactly as shown in the editor.
  • Ignoring negative durations. Duration.Hours can return a negative number when the duration is negative, for example -2 for a duration of -2.05:00:00. Decide whether negative values are valid in your data and filter or transform them if not.
  • Forgetting to set the data type. If the custom column comes through as Any or Text, later calculations may fail. Set the new column to Whole Number after creation.

Tips

  • Use Duration.Hours when you want the clock-style hour portion, such as grouping events by the hour they started within a day. Use Duration.TotalHours when you want a decimal total for billing, SLA calculations or averages.
  • Combine Duration.Hours with Duration.Minutes and Duration.Seconds to build a formatted time string, for example Text.From(Duration.Hours([D])) & ":" & Text.From(Duration.Minutes([D])).
  • If you need hours and minutes as separate numeric columns, add two custom columns rather than splitting text. This keeps the values numeric and sortable.
  • Test the function on a few rows with known values before applying it to a large table. A duration of 0.02:30:00 should return 2, and 0.00:45:00 should return 0.
  • Remember that Duration.Hours does not round. It truncates to the hour component. If you need rounding to the nearest hour, calculate Duration.TotalHours and round the result with Number.Round.

Troubleshooting

If the custom column shows Error for every row, the source column is probably not typed as Duration. Select the source column, set its data type to Duration, and refresh the query. If only some rows error, inspect those rows for blank values, text such as "n/a", or datetime values that were never converted.

If the function returns unexpected numbers, check whether you are looking at Duration.Hours or Duration.TotalHours. A duration of 1.10:00:00 returns 10 with Duration.Hours and 34 with Duration.TotalHours. Also check for negative durations, which return negative hour components.

If the formula bar rejects your column name, confirm the exact spelling and capitalization in the Applied Steps pane or the table header. Rename the column to a single word without spaces if the error persists, then update the formula.

If the query is slow after adding the column, the issue is usually upstream: a large table, a slow source or many other steps. Duration.Hours itself is a simple operation. Reduce columns early, filter rows before adding the custom column, and avoid repeating the same calculation in multiple steps.

If you cannot find the function in the Add Column menu, that is expected. Duration.Hours is an M function, not a ribbon button. Use Add Column > Custom Column and type the formula, or write it in the Advanced Editor.

Frequently Asked Questions

What is the difference between Duration.Hours and Duration.TotalHours?

Duration.Hours returns only the hour component of a duration, from 0 to 23 for positive values. Duration.TotalHours returns the entire duration expressed in hours, including days and minutes converted to hours, so 1.12:00:00 returns 12 versus 36.

Does Duration.Hours work on a datetime column?

No. It requires a duration value. Convert a datetime with Duration.From, or subtract two datetimes to create a duration, then apply Duration.Hours.

Can Duration.Hours return a negative number?

Yes. If the duration is negative, the hour component is negative as well. Filter or transform negative durations if they are not valid for your report.

Why does Duration.Hours return 0 for a 45-minute duration?

Because the duration has zero full hours. The function reads the hour component only; use Duration.TotalHours or Duration.Minutes if you need the minutes portion.

Can I use Duration.Hours in a calculated column in Power BI?

Duration.Hours is a Power Query M function, so it belongs in the Power Query Editor, not in DAX calculated columns. In DAX, use HOUR or related time-intelligence functions on a datetime column instead.

Does Duration.Hours round the result?

No. It truncates to the hour component. For rounding, calculate Duration.TotalHours and apply Number.Round to the result.