Power Query DateTime.Time Function Guide and Usage
In this article
The DateTime.Time function in Power Query is used to extract the time component from a combined date-time value, effectively stripping away the date and leaving only the hours, minutes, and seconds. This is essential for analysts who need to group data by time of day, calculate duration between events regardless of the date, or create time-based buckets for reporting. To use this function, you must have a column already formatted as a DateTime type.
What You'll Need
- Microsoft Excel (with Power Query/Get & Transform) or Power BI Desktop.
- A dataset containing a column with DateTime values (e.g., "2023-10-27 14:30:00").
- 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 Type: Locate the column containing your date and time information. Look at the icon to the left of the column header. It must show a small calendar and clock icon, indicating the type is "Date/Time". If it shows only a calendar or only a clock, the
DateTime.Timefunction will return an error because it specifically expects a combined DateTime input. To change this, right-click the column header, select Change Type, and choose Date/Time. - Create a Custom Column: Navigate to the Add Column tab on the top ribbon. Select Custom Column. This opens a dialog box where you can write your M code formula.
- Enter the Function: In the Custom column formula box, start by typing
= DateTime.Time([YourColumnName]). Replace[YourColumnName]by double-clicking the actual column name from the list of available columns on the right side of the dialog box. - Confirm and Apply: Give your new column a descriptive name, such as "Time Only", and click OK. You will now see a new column containing only the time values (e.g., 14:30:00).
- Set the Final Data Type: By default, the new column may be set to "Any" type. Click the data type icon in the column header and select Time to ensure Power BI or Excel recognizes it correctly for future calculations.
- Remove Original Column (Optional): If you no longer need the original combined date-time column, right-click its header and select Remove.
- Close and Apply: Go to the Home tab and click Close & Apply to load the transformed data back into your spreadsheet or data model.
Common Mistakes
One of the most frequent errors is attempting to apply DateTime.Time to a column that is already formatted as a "Time" type. Because the function is designed to extract time from a date-time pair, passing it a value that is already just a time will result in an Expression.Error. Always check your source data type before writing the formula.
Another common mistake is case sensitivity. Power Query uses the M language, which is strictly case-sensitive. Writing datetime.time or DateTime.TIME will cause the query to fail. The function must be written exactly as DateTime.Time.
Users also often confuse DateTime.Time with Time.From. While both can result in a time value, DateTime.Time is specifically optimized for DateTime types. Using Time.From is more flexible (it can handle text strings), but it can be slower on very large datasets and may produce unexpected results if the text format is ambiguous.
Finally, neglecting to set the data type of the resulting column is a common oversight. If the column remains as the "Any" type, you may encounter issues when trying to create time-based hierarchies or using the column in DAX measures in Power BI, as the system won't know how to treat the values for sorting.
Tips
For those who prefer not to write code, you can achieve the same result using the user interface. Select your DateTime column, go to the Add Column tab, click the Time button, and select Time Only. Power Query will automatically generate the DateTime.Time function in the background for you.
If you are dealing with time zones, it is best to convert your DateTime column to a local time zone using DateTimeZone.ToLocal before extracting the time. This ensures that the time extracted is relevant to your specific region rather than UTC.
To create time buckets (e.g., grouping by the hour), you can wrap the function or use the "Round" feature. For example, after extracting the time, you can use the Transform > Time > Hour option to get a whole number representing the hour of the day, which is much easier to use in a pivot table or bar chart.
When working with massive datasets, try to perform the time extraction as early as possible in your query steps. Reducing the complexity of the data stored in memory by splitting date and time into separate, typed columns can sometimes improve the performance of subsequent merge or group-by operations.
Troubleshooting
If you encounter an "Expression.Error: We cannot convert the value to type DateTime," the most likely cause is that your column contains null values or text that cannot be parsed as a date. To fix this, first use the Replace Values feature to handle nulls or use the "Remove Errors" option found in the Home tab under the Remove Rows menu.
If the function returns the correct time but it looks strange (e.g., showing as a decimal number like 0.60416), this is because Power Query is displaying the underlying numeric value of the time. To fix this, click the icon in the column header and explicitly change the data type to Time.
If your query is running slowly after adding this column, check if you have applied too many "Change Type" steps. Every time you change a type, Power Query adds a step to the script. Try to consolidate your type changes into a single step at the end of your transformation process.
If the time extracted is off by several hours, check your source data for a "Z" suffix or "+00:00" offset. This indicates the data is in UTC. You will need to use the DateTimeZone functions to shift the time to your local offset before applying DateTime.Time.
Frequently Asked Questions
Can I use this function to get the date instead of the time?
No, DateTime.Time is specifically for the time component. To extract the date, you should use the DateTime.Date function.
Does this function work on text columns?
No, it requires a DateTime data type. If your data is text, you must first change the column type to Date/Time using the column header menu before the function will work.
Is there a difference between DateTime.Time and Time.From?
Yes. DateTime.Time is designed specifically to split a DateTime value. Time.From is a more general conversion function that can attempt to turn various formats (including text) into a time value.
Will this function change my original data?
If you use it in a "Custom Column" under the Add Column tab, it creates a new column and leaves the original intact. If you use it within a "Transform" step, it will overwrite the values in the existing column.
Can I extract just the hour using this function?
DateTime.Time extracts the full time (hours, minutes, seconds). To get just the hour, you can follow this step with Time.Hour([YourTimeColumn]) or use the Transform > Time > Hour menu option.