Power Query Duration.FromText: Step-by-Step Guide
In this article
Duration.FromText converts a text value into a duration in Power Query, but the text must follow a specific format: days, hours, minutes, and seconds separated by dots, with an optional leading minus sign. For example, Duration.FromText("2.10:30:00") returns a duration of 2 days, 10 hours, and 30 minutes. If your text uses a different layout—like "2h 30m" or "10:30"—the function will throw an error, so you need to reshape the text first or write a custom conversion. This guide walks through the correct syntax, how to prepare your data, and how to fix the most common failures.
What You'll Need
- Power Query Editor open in Excel, Power BI Desktop, or another host that supports Power Query (the steps are nearly identical across hosts).
- A column containing duration text that you can inspect and, if needed, transform.
- Basic familiarity with adding a custom column or editing the formula bar.
Step-by-Step
- Load your data into Power Query. In Excel, select your table and go to Data > From Table/Range. In Power BI Desktop, use Home > Transform data to open the Power Query Editor. Menu labels can differ slightly by version and host, but the core editor is the same.
- Inspect the duration text. Click the column header and look at the values in the preview. Duration.FromText expects the pattern
[d.]hh:mm:ss. Valid examples:10:30:00,2.10:30:00,-1.02:15:30. Invalid examples:2h 30m,10:30(missing seconds),1 day 2 hours. - Add a custom column. Go to Add Column > Custom Column. Give it a name such as
Duration. In the formula box, enterDuration.FromText([YourColumn]), replacing[YourColumn]with the actual column name. Click OK. - Check the result type. Power Query should automatically detect the new column as type Duration. If it shows as Text or Any, click the type icon in the column header and choose Duration.
- Handle errors before they spread. If any rows show
Error, click the error cell to read the message. The usual cause is a text value that does not match the expected pattern. You can filter out errors, replace bad values, or clean the source text first (see Common Mistakes). - Use the formula bar for a single-step fix. If you prefer not to add a column, you can select the existing text column and use Transform > Replace Values or add a step manually in the formula bar with
Table.TransformColumns. For most users, the custom column approach is simpler and reversible. - Apply and close. When the preview looks correct, choose Home > Close & Load (Excel) or Home > Close & Apply (Power BI). The duration column will now be available to your data model, where you can format it or use it in calculations.
Common Mistakes
- Using a non-standard text format. Duration.FromText does not parse words like "hours" or abbreviations like "h" and "m". Convert those to the dotted format first, for example with Text.Replace and Text.Combine, or split the parts and rebuild a duration with #duration.
- Forgetting seconds. A value like
10:30is treated as invalid because the function expects hours, minutes, and seconds. Add:00if seconds are missing. - Mixing decimal separators. In some regions, a comma is the decimal separator. The function uses a dot between days and time, so
2,10:30:00will fail. Normalize the text to use a dot. - Assuming a leading zero is required. Hours can be one or two digits, but minutes and seconds should be two digits. Values like
1:5:0may not parse as expected; pad them to01:05:00. - Ignoring culture settings. If your workbook uses a culture where the time separator differs, the function may interpret the text differently. Test with a few sample rows before applying to the whole column.
- Expecting Duration.FromText to handle negative values automatically. A leading minus sign is supported, but the minus must come before the entire value, not inside it.
Tips
- Use
Duration.FromTextonly after you have confirmed the text pattern. A quick way is to add a temporary column withText.Lengthand check that lengths are consistent. - If your source uses a different format consistently, write a small custom function that splits the text and calls
#duration(days, hours, minutes, seconds). This is more robust than trying to coerce Duration.FromText. - Keep a copy of the original text column until you have verified the conversion. You can remove it later.
- When working with imported logs, check for leading or trailing spaces. Use Text.Trim before Duration.FromText.
- Remember that durations in Power Query are stored as a duration type, not as a number of hours. To convert to total hours, use
Duration.TotalHours([Duration]).
Troubleshooting
If Duration.FromText returns an error, start with the simplest checks. First, click the error cell and read the message; it usually points to the exact row and value. Second, verify the text pattern against the valid examples above. Third, check for hidden characters or spaces using Text.Trim and Text.Clean. If the column contains a mix of formats, split the data into separate queries or use conditional logic to apply different conversions. If the function works for some rows but not others, filter to the failing rows and inspect them side by side with the working ones. When the format is consistent but still failing, test a single hard-coded value in a blank query: Duration.FromText("1.02:03:04"). If that works, the issue is in your data, not the function. If it does not work, confirm you are using the correct function name and that your Power Query version supports it (it has been available for many versions, but host differences exist). Finally, if you cannot reshape the text, consider using #duration with parsed numbers instead.
Frequently Asked Questions
What exact format does Duration.FromText accept?
It accepts a text value in the form [d.]hh:mm:ss, with an optional leading minus sign. Days are optional; hours, minutes, and seconds are required, and minutes and seconds should be two digits.
Can I convert text like "2h 30m" directly?
No. Duration.FromText does not parse words or abbreviations. You need to transform the text into the dotted format first, or build the duration with #duration after extracting the numbers.
Why does my duration column show as text after using the function?
The custom column may have been created with the wrong data type. Click the type icon in the column header and select Duration. If the values are still text, check that the formula returns a duration and not a text string.
Does Duration.FromText work with negative durations?
Yes, if the minus sign is at the very beginning, for example -1.02:30:00. A minus sign inside the value will cause an error.
How do I convert a duration to total hours or minutes?
Use Duration.TotalHours([Duration]) or Duration.TotalMinutes([Duration]) in a subsequent custom column. These return numbers you can use in calculations.
What should I do if only some rows fail?
Filter to the error rows and inspect the text. Common fixes are trimming spaces, adding missing seconds, replacing commas with dots, or standardizing the number of digits. If the formats are genuinely mixed, split the query into separate branches and merge the results.