Duration.FromText

Updated on

Duration.FromText is a Power Query M function that parses a text value and returns a duration value. The function returns a duration value based on the specified text format.

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

Syntax

Duration.FromText( text as nullable text ) as nullable duration

Description

The Duration.FromText function in Power Query converts a text string into a duration value. It accepts specific formats and converts them into duration values.

The function accepts text strings in the following formats:

  • hh:mm:ss.ff:
  • ddd.hh:mm:ss.ff:

These formats include days (ddd), hours (hh), minutes (mm), seconds (ss), and fractional seconds (ff). Each part has an inclusive range.

Examples

Let’s see how we can make use of the Duration.FromText function. There are two fundamental formats the functions accepts.

Format 1: hh:mm.ff

In the hh:mm.ff format, hours are separated from minutes, and minutes from seconds, using colons. The fractional seconds are separated by a full stop.

We can use that as follows:

Duration.FromText( "20:10" )        // Output: #duration( 0, 20, 10, 0 )

Duration.FromText( "20:10:5" )      // Output: #duration( 0, 20, 10, 5 )

Duration.FromText( "20:10:5.75" )   // Output: #duration( 0, 20, 10, 5.75 )

Format 2: ddd.hh:mm.ff

The second format available for this function is ddd.hh:mm:ss.ff. It is very similar to the first format, but this one includes a days component. The days component is separated from the hours by a full stop. The rest of the format follows the same structure as the first format.

Here’s how we can use it:

Duration.FromText( "9" )            // Output: #duration( 9, 0, 0, 0 )

Duration.FromText( "9.10:5" )       // Output: #duration( 9, 10, 5, 0 )

Duration.FromText( "9.10:5:30" )    // Output: #duration( 9, 10, 5, 30 )

Duration.FromText( "9.10:5:30.25" ) // Output: #duration( 9, 10, 5, 30.25 )

Handling Null Values

When provided with a null value, the Duration.FromText function returns null.

Duration.FromText( null ) // Output: null

This ensures that null values are handled without any errors.

Other functions related to Duration.FromText are:

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

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