Power Query Time.FromText Function Guide and Usage
In this article
The Time.FromText function in Power Query is used to convert a text string representing a time into a formal Time data type. To use it, you wrap the text value or the column reference in the function, such as Time.FromText([ColumnName]), which allows Power Query to recognize the value for calculations, sorting, and time-based filtering. This is essential when importing data from CSVs or external APIs where time values are often treated as generic text strings.
What You'll Need
- Microsoft Excel (with Power Query enabled) or Power BI Desktop.
- A dataset containing time values formatted as text (e.g., "14:30", "2:15 PM").
- Access to the Power Query Editor (accessed via Data > Get Data or Transform Data).
Step-by-Step
- Open the Power Query Editor: Launch Excel or Power BI. Go to the Data tab, select Get Data, and connect to your source. Once the data loads in the preview window, click Transform Data to open the Power Query Editor.
- Identify the Target Column: Locate the column containing the time strings. Ensure the column header is clearly named. Note that if the column icon shows "ABC", it is currently formatted as text, which is the required input for this function.
- 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 formula.
- Enter the Function: In the Custom column formula box, type
= Time.FromText([YourColumnName]). Replace[YourColumnName]by double-clicking the actual column name from the list on the right side of the dialog box to ensure the syntax is correct. - Apply Culture Settings (Optional): If your time format differs from your system's local settings (for example, using a 24-hour clock in a region that defaults to 12-hour), add a second argument to the function:
= Time.FromText([YourColumnName], "en-GB"). The second argument specifies the culture code, ensuring the function interprets the text correctly regardless of the user's computer settings. - Validate the Result: Click OK. A new column will appear. Check the icon next to the column header; it should now show a small clock icon, indicating the data type is now Time.
- Clean Up: Right-click the original text column and select Remove to avoid redundancy in your final dataset.
- Finalize: Go to the Home tab and click Close & Apply to load the converted time data into your worksheet or report.
Common Mistakes
One of the most frequent errors is attempting to use Time.FromText on a column that is already typed as a Time or DateTime object. This function is specifically designed for text strings. If the input is not text, Power Query will return an Expression.Error. Always verify the data type in the column header before applying the function.
Another common mistake is ignoring the "AM/PM" designator. If your text is "02:00" but you intended it to be 2:00 PM, Time.FromText will default to 2:00 AM unless the string explicitly includes "PM". If the source data lacks these designators but the time is meant to be 24-hour format, ensure your culture setting matches a region that uses 24-hour time (like "en-GB" or "de-DE").
Users also often forget that Time.FromText cannot handle empty strings or nulls gracefully in all versions. If a cell is completely empty, the function may return an error for that specific row, which can break the entire refresh process if not handled with a try-otherwise block.
Tips
To handle potential errors in your data, such as typos (e.g., "25:00" which is an invalid time), use the try...otherwise syntax. For example: try Time.FromText([TimeColumn]) otherwise null. This prevents the entire query from failing and instead marks invalid entries as null, which you can then filter or replace easily.
For maximum performance on large datasets, consider using the "Change Type" UI option (clicking the ABC icon and selecting Time) before resorting to the custom function. The UI option often uses Table.TransformColumnTypes, which is highly optimized. Use Time.FromText specifically when you need to perform conditional logic during the conversion or when dealing with non-standard culture formats that the basic type change cannot resolve.
If you are dealing with a string that contains both a date and a time (e.g., "2023-10-01 14:30"), do not use Time.FromText. Instead, use DateTime.FromText first, and then use the DateTime.Time function to extract only the time portion. Trying to force a full date-time string into a time-only function will result in an error.
Troubleshooting
If you encounter a "Format.Error: We couldn't parse the input text" message, the first step is to check for leading or trailing spaces in your text column. Use the Transform > Format > Trim feature to remove invisible spaces that often sneak into CSV imports. A space before " 12:00" can cause the function to fail.
If the time is being shifted by several hours, check if your source data is in UTC and your system is in a local timezone. Time.FromText does not handle timezone offsets (like "+05:00"). If your text includes an offset, you must first convert it using DateTimeZone.FromText and then convert that result to a local time or a simple time value.
If the function returns "Error" for every single row, double-check the column name in your formula. Power Query is case-sensitive. [Time] is not the same as [time]. Ensure the column reference exactly matches the header name.
If you are seeing "null" values where you expect times, check if the original column had nulls. If the source was null, the result will be null. If the source had text that was simply incompatible, check if you used a try...otherwise block that is masking the actual error.
Frequently Asked Questions
Can Time.FromText handle seconds?
Yes, it can handle formats like "14:30:05". As long as the string follows a standard time format recognized by the specified culture, seconds will be preserved in the resulting Time data type.
What is the difference between Time.FromText and Change Type?
Change Type is a bulk operation applied to a column via the UI. Time.FromText is a specific M function that gives you more control, allowing you to specify culture settings or embed the conversion within a more complex custom column formula.
Does this function work with 12-hour clocks?
Yes, provided the text string includes the AM or PM indicator. If the string is "2:00" without an indicator, Power Query generally assumes a 24-hour clock and treats it as 2:00 AM.
How do I handle times that are just numbers (e.g., 1430)?
Time.FromText will not work on a number like 1430 because it expects a string with delimiters (like colons). You would first need to convert the number to text and use Text.Insert to add colons in the correct positions before applying Time.FromText.
Can I use this to convert a duration to a time?
No. Time represents a point in the day, while Duration represents a length of time. If you have a duration string (e.g., "25:00:00"), Time.FromText will fail because a day only has 24 hours. Use Duration.FromText for values exceeding 24 hours.