Duration.From converts a value into a duration, taking a number of days, a d.h:m:s text string, or an existing duration and giving you back a duration value. Available in Excel (Power Query), Power BI Desktop, and Power BI Service.
If you have a decimal number of days or an elapsed-time string and you want Power Query to treat it as a real duration, this is the function you reach for.
Syntax of Duration.From Function
Duration.From(value as any) as nullable duration
where
value(required, any). The value to convert into a duration. It can be a number (read as a count of days), a text string ind.h:m:sform, an existingduration, ornull.
Returns: a duration value. If value is null, it returns null.
In plain terms, you give it a number, some text, or a duration, and it hands back a proper duration value you can do duration math with.
Example 1: Convert a number of days to a duration
A number is read as a count of days, so 2.525 means just over two and a half days.
Duration.From(2.525)
Result: 2.12:36:00
The fractional part becomes hours, minutes, and seconds, so 2.525 days is 2 days, 12 hours, and 36 minutes.
Example 2: Convert a text string to a duration
Text has to be in d.h:m:s form, with a dot before the day part and colons between the time parts.
Duration.From("3.06:30:00")
Result: 3.06:30:00
The string parses to 3 days, 6 hours, and 30 minutes.
Example 3: Get the total hours from a duration
A bare duration is hard to use in a calculation, much like the duration you get from a date difference in Power Query, so wrap it in Duration.TotalHours to get a single number.
Duration.TotalHours(Duration.From(2.525))
Result: 60.6
2.525 days is 60.6 hours in total, which is the whole span expressed in hours.
Example 4: Get the hours component from a duration
Duration.Hours pulls just the hours part, not the total.
Duration.Hours(Duration.From(2.525))
Result: 12
This returns 12, the hours component of 2.12:36:00, not the 60.6 total hours from Example 3.
Example 5: Convert a column of decimal days to total hours
The most common real use is turning a whole column of decimal-day values into a usable number.
Say you have a Shifts query with a Task and a DecimalDays column.
You want a new column showing each shift’s total hours.
Here is the starting data:
| Task | DecimalDays |
|---|---|
| REF-451 | 0.5 |
| REF-452 | 1.25 |
| REF-453 | 2.525 |
Now add a column that runs Duration.From on each value and totals the hours:
let
Source = Excel.CurrentWorkbook(){[Name="Shifts"]}[Content],
AddHours = Table.AddColumn(Source,"TotalHours",each Duration.TotalHours(Duration.From([DecimalDays])),type number)
in
AddHours
This converts each decimal-day value to a duration, then reads it back as a number of hours.
The result has the new TotalHours column:
| Task | DecimalDays | TotalHours |
|---|---|---|
| REF-451 | 0.5 | 12 |
| REF-452 | 1.25 | 30 |
| REF-453 | 2.525 | 60.6 |
Each value is read as days first, so 0.5 becomes 12 hours, 1.25 becomes 30 hours, and 2.525 becomes 60.6 hours.
Things to keep in mind with Duration.From
- A number is read as DAYS, not hours or seconds.
Duration.From(1)is one whole day (1.00:00:00), not one hour. To convert from hours divide by 24, from minutes divide by 1440, from seconds divide by 86400. - Text must be in
d.h:m:sform. The day part is separated by a dot and the time parts by colons, as in"2.05:55:20". A plain"05:55:20"with no day part is fine and parses as 0 days, but wrong separators throw an error. nullpasses through asnull. It does not error, which is handy insideTable.AddColumnover a column that may have blanks. Those rows simply getnull.- It is different from the
#duration(...)constructor.#duration(d,h,m,s)builds a duration from four explicit numbers and is the right tool when you already have the parts.Duration.Fromcoerces a single value, so use it when you have one decimal-days number or a text string to convert. - It pairs with the
Duration.Total*and component accessors. A raw duration is awkward to display or sum, soDuration.Fromis almost always followed byDuration.TotalHoursand friends to get a single number, or by component accessors likeDuration.DaysandDuration.Hoursto pull one part.
Common questions about Duration.From
Should I use Duration.From or #duration?
Use the #duration(d,h,m,s) constructor when you already have separate day, hour, minute, and second numbers. Use Duration.From when you have one decimal-days number or a d.h:m:s text string that needs converting.
How do I convert hours or minutes into a duration?
Divide by 24 for hours or 1440 for minutes first, because the number argument is read as days. For example, Duration.From(90/1440) gives 0.01:30:00, which is 90 minutes.
List of All Power Query Functions
Related Power Query Functions / Articles: