Duration.From

Updated on

Duration.From is a Power Query M function that converts a given value to a duration value. The function returns a duration value or null if the input value is null.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

Duration.From( value as any ) as nullable duration

Description

The Duration.From function converts a given value into a duration. If the value is null, it returns null. If the value is already a duration, it returns that duration. It can convert text (in d.h:mformat) and numbers (representing days) into durations. Other types will cause an error.

Examples

Let’s explore how Duration.From works with different types of inputs through some detailed examples.

Converting a Number to a Duration

If you pass a value that is already a duration to the Duration.From function, it simply returns the same duration.

 // Output: #duration( 10, 5, 3, 20 )
Duration.From( #duration( 10, 5, 3, 20 ) )

Here, the function receives a duration of 10 days, 5 hours, 3 minutes, and 20 seconds, and returns it unchanged.

Converting Text to a Duration

The Duration.From function can convert a text string formatted as d.h:m:s into a duration.

// Output: #duration( 1, 12, 30, 15 )
Duration.From( "1.12:30:15" )

This string represents 1 day, 12 hours, 30 minutes, and 15 seconds, which is converted into the corresponding duration.

Converting a Number Value

When the input value is a number, it is interpreted as a number of days and then converted into a duration.

// Output: #duration( 3, 12, 0, 0 )
Duration.From( 3.5 )

In this case, 3.5 days are converted to 3 days and 12 hours.

Handling Null Values

If you pass a null value to the Duration.From function, it returns null.

Duration.From(null) // Output: null

This ensures that null values are handled without causing errors in your data transformation process.

Unsupported Value Types

If the input value is of an unsupported type, such as a list or a record, the Duration.From function will result in an error.

// Output: [DataFormat.Error] We couldn't convert to Duration.
Duration.From( { 1,2,3 } )

This example shows that passing a list (e.g., {1, 2, 3}) to the Duration.From function results in a data format error because lists are not a supported input type for this function.

Other functions related to Duration.From are:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/duration-from

2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy