Smart Systems for Home

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

Power Query Duration.ToText: How to Format Durations

· 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

Duration.ToText converts a duration value into a readable text string in Power Query. The function takes a duration (the difference between two dates or times) and returns text in the format d.hh:mm:ss — for example, 1 day, 2 hours, 3 minutes, and 4 seconds becomes "1.02:03:04". You need Power Query Editor (available in Excel, Power BI Desktop, and Power Query Online) and a duration value to convert. The function is simple but has one important limitation: it always returns the full day-hour-minute-second pattern, so if you need a custom format like "2 hours 15 minutes" you will need to build it manually. This guide walks through using Duration.ToText correctly, avoiding common pitfalls, and troubleshooting when the output is not what you expect.

What You'll Need

  • Power Query Editor open in Excel, Power BI Desktop, or Power Query Online. The steps below apply to all three; menu labels may differ slightly by version and platform.
  • A query with a duration column, or two date/time columns you can subtract to create one.
  • Basic familiarity with adding a custom column or editing an existing step in the formula bar.

Step-by-Step

  1. Open Power Query Editor. In Excel, go to Data > Get & Transform Data > Launch Power Query Editor. In Power BI Desktop, click Transform Data on the Home ribbon. In Power Query Online, open your dataflow or query and select the query you want to edit.
  2. Ensure you have a duration value. Duration.ToText expects a duration type, not a number or text. If you have two datetime columns (for example, StartTime and EndTime), select one column, then hold Ctrl and select the other, and go to Add Column > Subtract. This creates a duration column. Alternatively, you can create a custom column with a formula like [EndTime] - [StartTime], which also yields a duration.
  3. Add a custom column for the text output. Go to Add Column > Custom Column. In the Custom Column dialog, give the new column a name (e.g., "DurationText"). In the formula box, type Duration.ToText([DurationColumn]), replacing [DurationColumn] with the actual name of your duration column. Note that column names with spaces must be enclosed in double quotes and a hash symbol, like Duration.ToText([#"My Duration"]).
  4. Click OK. Power Query will add a new column containing the text representation of each duration. The output will look like "0.00:00:00" for zero, "0.01:30:00" for 1 hour 30 minutes, and "2.03:04:05" for 2 days, 3 hours, 4 minutes, 5 seconds.
  5. Check the data type. The new column should be of type Text. If Power Query shows it as Any, click the data type icon in the column header and select Text. This ensures the values are treated as text in subsequent steps and when loaded to the data model.
  6. Rename and reorder if needed. Double-click the column header to rename it, or drag it to a new position. You can also remove the original duration column if it is no longer needed, but keep it if you might want to perform further duration calculations.
  7. Load the query. When finished, go to Home > Close & Load (Excel) or Home > Close & Apply (Power BI) to load the results into your worksheet or data model. The text column will appear as expected.

Common Mistakes

  • Passing a number instead of a duration. Duration.ToText requires a duration type. If you pass a number (e.g., 1.5), you will get an error. Convert numbers to durations first using #duration(days, hours, minutes, seconds) or by subtracting two datetime values.
  • Forgetting quotes around column names with spaces. In M, column names with spaces must be written as [#"Column Name"]. Using [Column Name] without the hash and quotes will cause a syntax error.
  • Expecting custom formatting. Duration.ToText always returns the fixed d.hh:mm:ss format. It does not accept a format string. If you need "2 days, 3 hours", you must build it manually with functions like Duration.Days, Duration.Hours, etc.
  • Assuming nulls are handled. If the duration value is null, Duration.ToText returns null. Use try ... otherwise or if [DurationColumn] = null then null else Duration.ToText([DurationColumn]) to handle nulls gracefully.
  • Using it on a column that is already text. If your column is text, convert it to duration first using Duration.FromText or by changing the data type.

Tips

  • To create a duration from a number of hours, use #duration(0, [Hours], 0, 0) and then apply Duration.ToText.
  • If you need the total number of hours as text (e.g., "36.5 hours"), use Number.ToText(Duration.TotalHours([DurationColumn])) instead.
  • Combine Duration.ToText with other text functions to produce friendly labels. For example, "Duration: " & Duration.ToText([DurationColumn]).
  • Remember that the day part is always included, even if zero. To omit zero days, you can use Text.AfterDelimiter or conditional logic.
  • Use the formula bar to edit the step directly if you need to tweak the formula after adding the custom column.

Troubleshooting

If Duration.ToText does not work as expected, check these common issues:

  • Error: "We cannot convert a value of type Number to type Duration." The column you are referencing is not a duration. Change its data type to Duration using the Transform tab, or create a duration by subtracting two datetime columns.
  • Error: "The name 'Duration.ToText' wasn't recognized." This usually means a typo or that you are using an older version of Power Query that does not support the function. Ensure you have the latest updates. Duration.ToText has been available for many versions, so check spelling and parentheses.
  • Output is null for some rows. The original duration value is null. Handle nulls explicitly if you want a different result, such as an empty string.
  • Output shows unexpected large numbers. If your duration exceeds 24 hours, the day part increases accordingly. For example, 25 hours becomes "1.01:00:00". This is correct; if you want total hours, use Duration.TotalHours.
  • The column appears as a number in Excel. After loading, Excel might interpret the text as a number if it looks like a decimal. Format the column as Text in Excel, or prefix with an apostrophe. Alternatively, ensure the Power Query column is explicitly typed as Text before loading.

If that did not work, try recreating the custom column from scratch, or use the Advanced Editor to inspect the M code for syntax errors. You can also test Duration.ToText in a blank query by entering = Duration.ToText(#duration(1,2,3,4)) in the formula bar to see the expected output.

Frequently Asked Questions

Can I format the output of Duration.ToText?

No. Duration.ToText does not accept a format string. It always returns the pattern d.hh:mm:ss. For custom formats, build the text manually using Duration.Days, Duration.Hours, Duration.Minutes, and Duration.Seconds, and concatenate with separators.

What happens if the duration is negative?

Duration.ToText returns a text representation with a leading minus sign, such as "-1.02:03:04". Negative durations can occur if you subtract a later time from an earlier time; ensure your logic accounts for that.

Does Duration.ToText work in Power Query Online?

Yes, Duration.ToText is available in Power Query Online, Excel, and Power BI Desktop. The function is part of the standard M library and behaves consistently across platforms.

How do I convert text back to a duration?

Use Duration.FromText. It parses strings in the format d.hh:mm:ss (and some variants) into a duration value. For example, Duration.FromText("1.02:03:04") returns a duration of 1 day, 2 hours, 3 minutes, 4 seconds.

Why does my duration show as a decimal number in Excel?

Excel may interpret text like "1.02:03:04" as a number if the column is not formatted as Text. In Power Query, ensure the column data type is Text before loading. In Excel, you can format the column as Text or prefix values with an apostrophe.

Can I use Duration.ToText with a column that contains nulls?

Yes, but the result will be null for null inputs. If you want a different output (e.g., empty string), wrap the call in a conditional: if [DurationColumn] = null then "" else Duration.ToText([DurationColumn]).