Smart Systems for Home

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

Power Query Time.Hour Function Guide and Usage

· 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

The Time.Hour function in Power Query is used to extract the hour component (a number from 0 to 23) from a value that has a Time data type. To use it, you wrap your time column or value in the function: Time.Hour([YourTimeColumn]). This is essential for analysts who need to group data by the hour of the day, identify peak activity windows, or create time-based buckets for reporting without needing the full timestamp.

What You'll Need

  • Microsoft Excel or Power BI Desktop.
  • A dataset containing a column with a Time or DateTime data type.
  • Access to the Power Query Editor (Get & Transform Data).

Step-by-Step

  1. Open Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click Transform Data on the Home ribbon.
  2. Verify Data Types: Locate the column containing your time data. Look at the icon to the left of the column header. It must be a clock icon (Time) or a calendar-and-clock icon (DateTime). If it says "abc" (Text) or "123" (Whole Number), right-click the header, select Change Type, and choose Time or Date/Time. Labels may differ slightly depending on your software version.
  3. Create a Custom Column: Navigate to the Add Column tab on the top ribbon. Click on Custom Column. This opens a dialog box where you can write your M formula.
  4. Enter the Formula: In the Custom column formula box, type the following: = Time.Hour([YourColumnName]). Replace [YourColumnName] by double-clicking the actual column name from the available columns list on the right side of the window.
  5. Set the New Column Name: Give your new column a descriptive name, such as "Hour of Day," in the New column name field.
  6. Confirm and Apply: Click OK. Power Query will generate a new column where every row contains an integer from 0 (midnight) to 23 (11 PM).
  7. Change Data Type to Whole Number: By default, custom columns may be set to "Any" type. Click the data type icon in the header of your new hour column and select Whole Number to ensure you can perform mathematical operations or sorting on it.
  8. Close and Apply: Go to the Home tab and click Close & Apply to load the transformed data back into your Excel sheet or Power BI model.

Common Mistakes

One of the most frequent errors is attempting to use Time.Hour on a column that is formatted as Text. Power Query is strictly typed; if the column contains strings that look like times (e.g., "14:30") but are stored as text, the function will return an Error in every cell. You must explicitly convert the column to a Time type before applying the function.

Another common mistake is confusing Time.Hour with DateTime.Hour. While Time.Hour is the standard for extracting the hour, users often try to apply it to a DateTime column without understanding that Power Query handles this automatically by ignoring the date part. However, if you are writing complex M code in the Advanced Editor, ensuring the input is specifically a time value is safer for stability.

Users also frequently forget that Power Query uses a 24-hour clock. If you are expecting a result of "2" for 2:00 PM, you will be disappointed to see "14". To convert this to a 12-hour format, you would need an additional conditional column or a modulo operation, as Time.Hour only returns the raw integer of the hour.

Tips

For those who prefer not to write formulas, you can achieve the same result using the user interface. Select your time column, go to the Add Column tab, click the Time button, select Hour, and then choose Hour. This automatically generates the Time.Hour M code for you in the background, reducing the risk of syntax errors.

If you are analyzing large datasets, consider using the "Group By" feature after extracting the hour. By grouping by the hour column and counting the rows, you can quickly create a distribution chart showing which hours of the day have the most activity, which is a standard requirement for operational dashboards.

To create time "buckets" (e.g., Morning, Afternoon, Evening), use the extracted hour column as the basis for a Conditional Column. For example, if Hour is less than 12, then "Morning"; if Hour is less than 17, then "Afternoon"; else "Evening". This is much more efficient than trying to write a complex nested IF statement within a single custom column formula.

Troubleshooting

If you encounter an "Expression.Error: We cannot convert the value... to type Time," the first step is to check your source data for nulls or invalid entries. If a cell contains a value like "N/A" or a blank space in a column you've told Power Query is a Time type, the function will fail. Use the Replace Errors or Remove Errors feature found in the Transform tab to clean the data before applying Time.Hour.

If the function returns a value that seems incorrect (e.g., the hour is shifted by several hours), check your data source's timezone. Power Query often handles times in UTC. If your source is a cloud database, the time might be stored in UTC, but you are expecting local time. You may need to use DateTimeZone.ToLocal or add a specific number of hours using #duration before extracting the hour.

If the Custom Column dialog tells you there is a syntax error, check your brackets. Column names must be enclosed in square brackets [ ], and the function name Time.Hour is case-sensitive. Writing time.hour (lowercase) will result in an error because M is a case-sensitive language.

Frequently Asked Questions

Does Time.Hour work with Date/Time columns?

Yes. When you pass a DateTime value into Time.Hour, Power Query automatically extracts the time portion of the value and then identifies the hour. You do not need to split the date and time into two columns first.

How do I get the hour in a 12-hour format (AM/PM)?

The Time.Hour function only returns integers from 0-23. To get a 12-hour format, you can create a custom column with a formula that checks if the hour is greater than 12; if so, subtract 12. To add "AM" or "PM", use a conditional column based on whether the hour is less than 12.

Can I use this function to calculate the duration between two times?

No. Time.Hour only tells you what hour of the day a specific timestamp represents. To find the difference between two times, you should subtract one time column from another, which results in a Duration type, and then use Duration.TotalHours to get the total elapsed time.

What happens if the time value is null?

If the input value is null, Time.Hour will return a null value in the resulting column. It will not throw an error unless the data type of the column is strictly enforced as something other than null/time.

Is there a difference between Time.Hour and DateTime.Hour?

In the current version of Power Query M, Time.Hour is the primary function used for this purpose. While some older documentation might refer to different variations, Time.Hour is the standard and most compatible way to extract the hour from either a Time or DateTime value.