Smart Systems for Home

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

Power Query Duration.From: Step-by-Step Guide

· 5 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.From converts a value into a duration in Power Query. You give it a number (interpreted as days), a text string that looks like a duration, or a datetime value, and it returns a duration record with Days, Hours, Minutes, Seconds, and fractional seconds. This is useful when you need to calculate elapsed time, format time differences, or combine durations from different sources. You do not need any special setup beyond Power Query Editor, which is built into Excel and Power BI.

What You'll Need

  • Power Query Editor open (in Excel: Data > Get Data > Launch Power Query Editor; in Power BI Desktop: Transform Data).
  • A query with a column or value you want to convert to a duration.
  • Basic familiarity with adding a custom column or using the formula bar.

Step-by-Step

  1. Open Power Query Editor. In Excel, go to Data > Get Data > Launch Power Query Editor. In Power BI Desktop, click Transform Data on the Home ribbon. Menu labels can differ slightly by version and platform, but the editor itself works the same.
  2. Load or create a query. Connect to your data source or use Enter Data to type sample values. For example, enter a column named DurationText with values like "1.02:30:00" (1 day, 2 hours, 30 minutes) or a column named Days with numbers like 2.5.
  3. Select the column you want to convert. Click the column header so the whole column is highlighted. If you are working with a single value, you can use the formula bar directly.
  4. Add a custom column. Go to Add Column > Custom Column. In the dialog, give the new column a name, such as DurationValue. In the formula box, type = Duration.From([YourColumn]), replacing YourColumn with the actual column name. If your column contains numbers, Duration.From treats the number as days (so 2.5 becomes 2 days 12 hours). If it contains text like "1.02:30:00", it parses that as a duration. If it contains a datetime, it converts the time portion to a duration. Click OK.
  5. Check the result. The new column should show duration values. If you see errors, the source values may not be in a format Duration.From recognizes. You can use Duration.FromText for text strings that follow the standard format ([-]d.hh:mm:ss.fffffff), or convert numbers explicitly.
  6. Expand or extract parts if needed. To get hours, minutes, or seconds from the duration, use Duration.Hours, Duration.Minutes, or Duration.Seconds. For example, add another custom column with = Duration.Hours([DurationValue]) to get the hours component. Note that this returns only the hours part (0–23), not total hours. For total hours, use = Duration.TotalHours([DurationValue]).
  7. Apply and close. Click Close & Load (Excel) or Close & Apply (Power BI) to load the result into your worksheet or data model.

Common Mistakes

  • Assuming numbers are seconds. Duration.From treats a number as days, not seconds. If you have 3600 and expect 1 hour, you will get 3600 days. Divide by 86400 to convert seconds to days before using Duration.From, or use #duration(0,0,0,3600) directly.
  • Using text without the right format. Duration.FromText expects a specific pattern: optional minus sign, then d.hh:mm:ss.fffffff. Strings like "2 hours 30 minutes" will cause an error. Use Duration.FromText only with properly formatted strings, or parse the text manually first.
  • Confusing Duration.From with Duration.FromText. Duration.From is the general conversion function; Duration.FromText is specifically for text. If your column is text, Duration.From will attempt to parse it, but it may fail if the format is not exact. It is safer to use Duration.FromText for text columns.
  • Expecting total hours from Duration.Hours. Duration.Hours returns the hours component (0–23), not the total hours. For total hours, use Duration.TotalHours.
  • Ignoring culture settings. Text parsing can depend on the locale. If your data uses a different date/time format, parsing may fail. Use the optional culture parameter if needed, e.g., Duration.FromText("1.02:30:00", "en-US").

Tips

  • Use #duration(days, hours, minutes, seconds) to create a duration directly from numbers without conversion. For example, #duration(1,2,30,0) gives 1 day, 2 hours, 30 minutes.
  • To convert a number of seconds to a duration, divide by 86400 and pass to Duration.From: Duration.From([Seconds] / 86400).
  • When working with datetime values, Duration.From extracts the time portion. If you need the full datetime difference, subtract two datetimes to get a duration directly.
  • Use Duration.TotalDays, Duration.TotalHours, Duration.TotalMinutes, or Duration.TotalSeconds to get the entire duration expressed in a single unit.
  • If you need to format a duration as text, use Duration.ToText. It returns a text representation in the format d.hh:mm:ss.

Troubleshooting

If you get an error like "We couldn't convert to Duration", check the source values. For text, ensure they match the expected pattern exactly. For numbers, confirm they are numeric type, not text. You can change the column type to Number or Duration using the Transform tab. If the error persists, try using Duration.FromText with a specific culture, or clean the text by removing extra spaces or characters. If you see unexpected values, remember that numbers are treated as days. For example, 1.5 becomes 1 day 12 hours. If you need a different interpretation, adjust the input accordingly. If the function is not recognized, ensure you are using it in the correct context (Power Query M formula, not DAX).

Frequently Asked Questions

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

Duration.From is a general function that converts a value (number, text, or datetime) to a duration. Duration.FromText is specifically for text strings and requires the text to be in a specific format. Duration.From will attempt to parse text but may fail if the format is not exact; Duration.FromText gives more control with an optional culture parameter.

How do I convert seconds to a duration?

Divide the number of seconds by 86400 (the number of seconds in a day) and pass the result to Duration.From. For example, Duration.From(3600/86400) returns 1 hour. Alternatively, use #duration(0,0,0,3600).

Why does Duration.From(1) return 1 day?

Because Duration.From interprets a number as a number of days. So 1 becomes 1 day, 0.5 becomes 12 hours, and so on. If you want a different unit, convert your number to days first.

Can I use Duration.From on a column with mixed types?

It is not recommended. Mixed types may cause errors. Clean and standardize your column first, or use try...otherwise to handle errors gracefully.

How do I get total hours from a duration?

Use Duration.TotalHours instead of Duration.Hours. Duration.Hours returns only the hours component (0–23), while Duration.TotalHours returns the entire duration expressed in hours, including days converted to hours.

What format does Duration.FromText expect?

It expects a text string in the format [-][d.]hh:mm:ss[.fffffff], where d is days, hh hours, mm minutes, ss seconds, and fffffff fractional seconds. For example, "1.02:30:00" means 1 day, 2 hours, 30 minutes. The minus sign indicates a negative duration.