# Duration.ToRecord

## What is the Duration.ToRecord function?

The Duration.ToRecord function is used to convert a duration value into a record. A duration represents a length of time, such as 2 hours and 30 minutes. A record is a collection of key-value pairs, where each key represents a field name and each value represents the data in that field.

So, the Duration.ToRecord function takes a duration value and converts it into a record with two fields: “Hours” and “Minutes”. The “Hours” field contains the number of hours in the duration, while the “Minutes” field contains the remaining minutes.

## Using the Duration.ToRecord function

To use the Duration.ToRecord function, you’ll need a duration value to convert. This can be a column in a table, a variable, or a literal value. Here’s an example:

let

durationValue = #duration(0, 2, 30, 0),

recordValue = Duration.ToRecord(durationValue)

in

recordValue

``` In this example, we're creating a duration value of 2 hours and 30 minutes using the #duration function. Then, we're passing that value to the Duration.ToRecord function to create a record. The resulting record will look like this: ```

[Hours=2, Minutes=30]

``` Understanding the M code behind Duration.ToRecord Now that we've seen how to use the Duration.ToRecord function, let's take a look at the M code behind it. Here's the code for the function: ```

(Duration as duration) as record =>

let

hours = Duration.Hours(Duration),

minutes = Duration.Minutes(Duration) + Duration.Seconds(Duration)/60 + Duration.Milliseconds(Duration)/60000

in

[Hours=hours, Minutes=minutes]

``` This code defines a function that takes a duration parameter and returns a record. The first line of the function specifies the type of the parameter (duration) and the type of the function's return value (record). The next two lines of the function calculate the number of hours and minutes in the duration. The Hours function is a built-in function that returns the number of hours in a duration. The Minutes function returns the number of minutes in a duration, but we need to do some additional calculations to get the total number of minutes. We add the number of seconds (converted to minutes) and the number of milliseconds (converted to minutes) to get the total number of minutes. Finally, the function creates a record with two fields: "Hours" and "Minutes". The values for these fields are the variables we calculated earlier. The Duration.ToRecord function is a useful tool for converting duration values into records. By understanding the M code behind this function, you can customize it to suit your specific needs and gain a deeper understanding of how Power Query works. Power Query and M Training Courses by G Com Solutions (0800 998 9248) Power Query and M Intensive Training Course £1,050.00 – £26,550.00 Select optionsContinue Loading Done Power Query and M Introduction £395.00 – £9,750.00 Select optionsContinue Loading Done Power Query and M Intermediate £395.00 – £9,750.00 Select optionsContinue Loading Done Power Query and M Advanced £395.00 – £9,750.00 Select optionsContinue Loading Done Upcoming Courses Contact Us Subject Your Name (required) Company/Organisation Email (required) Telephone Training Course(s) Power BI Intensive TrainingPower BI introduction Power BI IntermediatePower BI AdvancedDAXPower Query MPower BI CertificationPower BI AdministrationPower PlatformPower AutomatePower AppsOTHER Your Message Upload Example Document(s) (Zip multiple files) ```
``` ```
``` ```
``` ```
``` ```
``` ```