Smart Systems for Home

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

Power Query Time.From 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.From function in Power Query (M language) converts a value—typically a text string or a number—into a Time data type. To use it, you wrap your target value in the function, such as Time.From("14:30:00"), which tells Power Query to interpret that specific input as a time of day rather than a generic piece of text or a decimal.

What You'll Need

  • Microsoft Excel (with Power Query enabled) or Power BI Desktop.
  • A data source containing time-related information (e.g., a CSV, Excel table, or SQL database).
  • Access to the Power Query Editor (accessed via Data > Get Data or Transform Data).

Step-by-Step

  1. Open the Power Query Editor: In Excel, go to the Data tab and select Get Data or launch an existing query by double-clicking it in the Queries & Connections pane. In Power BI, click Transform Data on the Home ribbon.
  2. Identify the Target Column: Locate the column you wish to convert. This column usually contains values like "08:00 AM", "15:45", or decimal numbers representing fractions of a day.
  3. Create a Custom Column: Instead of overwriting your original data, it is safer to create a new column. Go to the Add Column tab on the ribbon and select Custom Column. (Note: Menu labels may vary slightly depending on your version of Excel or Power BI).
  4. Enter the Time.From Formula: In the Custom Column dialog box, give your new column a name (e.g., "Converted Time"). In the formula box, type the following: = Time.From([YourColumnName]). Replace [YourColumnName] with the actual name of the column containing your time data by double-clicking it from the list of available columns on the right.
  5. Verify the Data Type: Once you click OK, Power Query will generate the new column. Look at the icon to the left of the column header. If it shows "ABC/123", it is still set to "Any". Click the icon and select Time from the dropdown menu to explicitly set the data type.
  6. Handle Nulls or Errors: If some rows show "Error", right-click the column header and select Remove Errors or Replace Errors to ensure your dataset is clean before loading it into the model.
  7. Close and Apply: Once the conversion is verified, go to the Home tab and select Close & Apply to bring the converted time data into your Excel worksheet or Power BI report.

Common Mistakes

One of the most frequent errors is attempting to use Time.From on a value that includes both a date and a time (a DateTime value). While Time.From can often extract the time portion from a DateTime object, it is more precise to use DateTime.Time when you know the input is specifically a DateTime type. Using Time.From on a complex string that doesn't follow a standard time format (like "About 2pm") will result in an Expression.Error because the function cannot parse natural language.

Another common mistake is ignoring the locale settings. Power Query relies on the system's regional settings to interpret time strings. For example, a user in the US might use "12:30 PM", while a user in Europe might use "12:30". If the input string does not match the expected regional format of the machine running the query, Time.From may return an error or, worse, convert the time incorrectly without warning.

Lastly, users often forget that Time.From is case-sensitive. In the M language, writing time.from (lowercase 't') will cause a syntax error. The function must be written exactly as Time.From.

Tips

To avoid errors caused by regional settings, consider using Time.FromText instead of the generic Time.From. The Time.FromText function allows you to provide an optional second argument for the culture (e.g., Time.FromText([Column], "en-US")), which forces Power Query to interpret the string using a specific region's rules regardless of where the file is opened.

If you are dealing with decimal numbers where 0.5 represents noon and 0.75 represents 6 PM, Time.From is highly effective. Power Query treats the number 1 as a full 24-hour day. Therefore, passing a decimal value into Time.From automatically converts that fraction of a day into a standard time format, saving you from having to manually calculate hours and minutes.

For those building complex transformations, combine Time.From with a conditional column. For instance, if you have a column that says "Closed" for some rows and a time for others, use an if...then...else statement: if [Status] = "Closed" then null else Time.From([Status]). This prevents the function from attempting to convert the word "Closed" into a time, which would otherwise trigger an error.

Troubleshooting

If your Time.From function is returning "Error" for several rows, the first step is to check for leading or trailing spaces in your text. A value like " 14:00" (with a leading space) can sometimes confuse the parser. Use the Transform > Trim function on the original column before applying the Time.From conversion.

If the time is converting to the wrong hour (e.g., 2:00 PM becomes 2:00 AM), check if your source data is in a 12-hour format without an AM/PM indicator. Power Query defaults to a 24-hour clock. If your data is "02:00" but is intended to be PM, you will need to create a custom logic step to add 12 hours to any value where the original source indicated PM in a separate column.

If the function is not recognized at all, ensure you are writing the formula in the Custom Column dialog or the Advanced Editor, and not in a standard Excel cell. Time.From is a Power Query M function, not an Excel worksheet function. If you are in the Excel grid, you would use TIMEVALUE() instead.

When dealing with "null" values, Time.From(null) will simply return null. However, if the cell contains an empty string (""), it may result in an error. Use the Replace Values transformation to change empty strings to actual nulls before running the function.

Frequently Asked Questions

Can Time.From convert a date and time combined?

Yes, it can. If you pass a DateTime value into Time.From, it will strip away the date portion and return only the time. However, for clarity in your code, DateTime.Time is the preferred function for this specific task.

What is the difference between Time.From and Time.FromText?

Time.From is a general-purpose converter that handles various input types (numbers, dates, text). Time.FromText is specialized for text strings and allows you to specify a culture/locale to ensure consistent parsing across different computers.

Why does my time look like a decimal number after using the function?

This usually happens because the column data type is still set to "Any" or "Decimal Number". You must manually change the data type to "Time" using the icon next to the column header to see the formatted time (HH:MM:SS).

How do I handle times that are over 24 hours?

The Time data type in Power Query only represents a time of day (00:00:00 to 23:59:59). If you need to represent a duration (e.g., 30 hours), you should use the Duration data type and functions like Duration.From instead of Time.From.

Does Time.From work with AM/PM notation?

Yes, provided the string is formatted in a way that matches your system's regional settings. If your system is set to English (US), "1:00 PM" will be converted correctly to 13:00:00.

Can I use this function to create a specific time from scratch?

While you can use Time.From("12:00:00"), the more professional way to create a specific time in M is using #time(hour, minute, second), such as #time(12, 0, 0). This avoids the need for text parsing entirely.