Power Query Time.EndOfHour Function Guide
In this article
The Time.EndOfHour function in Power Query is used to return the latest possible time within a specific hour, which is effectively 11:59:59 PM for that hour. This is primarily used to standardize timestamps, create time-based buckets for reporting, or calculate the boundary of a time window for data filtering. To use it, you simply pass a time value into the function, and Power Query returns the end of that hour's duration.
What You'll Need
- Microsoft Excel (with Power Query enabled) or Power BI Desktop.
- A dataset containing a column formatted as a Time or DateTime data type.
- Basic familiarity with the Power Query Editor interface.
Step-by-Step
- Open the 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.
- Verify Data Types: Locate the column containing your time data. Ensure the data type icon to the left of the column header is set to Time (a small clock icon) or DateTime (a calendar and clock icon). If it is set to Text or Any, right-click the header, select Change Type, and choose Time.
- Create a Custom Column: Navigate to the Add Column tab on the top ribbon. Select Custom Column from the General group.
- Enter the Formula: In the Custom Column dialog box, give your new column a name (e.g., "Hour End"). In the Custom column formula box, type the following:
Time.EndOfHour([YourColumnName]). Replace[YourColumnName]with the actual name of your time column by double-clicking it from the available columns list on the right. - Confirm and Apply: Click OK. Power Query will generate a new column where every timestamp is rounded up to the 59th minute and 59th second of that hour.
- Set the New Column Type: The new column may initially be assigned the "Any" data type. Click the data type icon in the column header and select Time to ensure the values are handled correctly in subsequent steps.
- Close and Apply: Once you have verified the results, go to the Home tab and select Close & Apply to load the transformed data back into your workbook or model.
Common Mistakes
One of the most frequent errors is attempting to use Time.EndOfHour on a column that is formatted as Text. Power Query functions are strictly typed; if the function encounters a text string instead of a time value, it will return an Error in every cell of that column. Always check your data types before adding custom columns.
Another common mistake is confusing Time.EndOfHour with DateTime.EndOfHour. While they perform similar logic, Time.EndOfHour expects a Time value and returns a Time value. If you are working with a DateTime column (which includes the date), using the Time-specific function may strip away the date component or cause a type mismatch error depending on how the formula is written. If you need to preserve the date while finding the end of the hour, you must use the DateTime version of the function.
Users also frequently forget that this function does not "round" to the nearest hour in a mathematical sense; it always moves forward to the end of the current hour. For example, 10:01 AM becomes 10:59:59 AM. If you intended to round down to the start of the hour, you should be using Time.StartOfHour instead.
Tips
To make your reports more readable, combine Time.EndOfHour with a rounding or formatting step. While 11:59:59 is technically the end of the hour, many business reports prefer to see the top of the next hour (e.g., 12:00:00). You can achieve this by adding a custom column that takes the result of Time.EndOfHour and adds one second to it using the #duration function: Time.EndOfHour([Time]) + #duration(0,0,0,1).
If you are dealing with massive datasets, avoid creating multiple custom columns for different time boundaries. Instead, consider using the "Transform" tab to modify the existing column in place if you no longer need the original precise timestamp. This reduces the memory footprint of your query.
For those building dashboards, using Time.EndOfHour is an excellent way to create "Time Slices." By grouping your data by the end-of-hour value, you can create a histogram that shows activity levels per hour without having to deal with the noise of individual minutes and seconds.
Troubleshooting
If you encounter an Expression.Error: We cannot convert the value to type Time, the first step is to check the source data. Ensure there are no null values or "N/A" strings in your time column. Nulls are generally handled gracefully, but text strings in a time column will break the function. Use the "Replace Errors" or "Remove Errors" feature found in the Transform tab to clean the column before applying the function.
If the function returns a value that looks like a decimal number (e.g., 0.4999), this is not an error. Power Query sometimes displays time as a fraction of a 24-hour day internally. To fix this, simply click the data type icon in the column header and explicitly change the type to Time.
If the results are not appearing as expected—for instance, if the time is not changing—double-check that you are not accidentally referencing a different column in your formula. Ensure the column name inside the brackets [ ] exactly matches the case-sensitive name of your source column.
Frequently Asked Questions
Does Time.EndOfHour work with dates?
No, Time.EndOfHour is designed specifically for Time data types. If your column contains both a date and a time (DateTime), you should use DateTime.EndOfHour to ensure the date is preserved in the output.
What is the exact value returned by this function?
The function returns the time 59 minutes and 59 seconds into the hour. For example, if the input is 2:15 PM, the result will be 2:59:59 PM.
Can I use this function in a Power BI DAX measure?
No, Time.EndOfHour is an M language function used within the Power Query Editor. For DAX, you would use a combination of FLOOR or TIME functions to achieve a similar result in a calculated column or measure.
Is this function case-sensitive?
Yes, Power Query (M) is case-sensitive. You must write it exactly as Time.EndOfHour. Writing it as time.endofhour or Time.EndofHour will result in a syntax error.
How do I get the start of the hour instead?
You should use the Time.StartOfHour function. This will return the 00:00 minute and second mark for the hour associated with the input value.
Does this function handle midnight correctly?
Yes, if the input time is anywhere between 12:00:00 AM and 12:59:59 AM, the function will return 12:59:59 AM of that same day.