Time.ToRecord

D

T

The M Code Behind the Power Query M function Time.ToRecord

In this article, we will take a closer look at the M code behind the Time.ToRecord function and how it can be used in Power Query.

Understanding the Time.ToRecord Function

The Time.ToRecord function is a M function that is used to convert a time value to a record. It takes a single parameter, a time value, and returns a record that contains the hour, minute, and second values of the time.

Here is an example of how the Time.ToRecord function can be used in Power Query:


let

timeValue = #time(12, 30, 0),

recordValue = Time.ToRecord(timeValue)

in

recordValue


The above M code will convert the time value “12:30:00” to a record that contains the hour, minute, and second values of the time.

The M Code Behind the Time.ToRecord Function

To understand the M code behind the Time.ToRecord function, we need to take a closer look at how the function is defined in Power Query.

Here is the M code for the Time.ToRecord function:


(Time as any) as record =>

let

hour = Time.Hour(Time),

minute = Time.Minute(Time),

second = Time.Second(Time)

in

[Hour = hour, Minute = minute, Second = second]


The above M code defines the Time.ToRecord function as a function that takes a single parameter, “Time”, and returns a record that contains the hour, minute, and second values of the time.

The function first extracts the hour, minute, and second values of the time using the Time.Hour, Time.Minute, and Time.Second functions respectively. It then returns a record that contains these values with the field names “Hour”, “Minute”, and “Second”.

Using the Time.ToRecord Function in Power Query

The Time.ToRecord function can be used in a variety of ways in Power Query. One common use case is to split a time value into its individual components.

Here is an example of how the Time.ToRecord function can be used to split a time value into its individual components:


let

Source = #table({"Time"}, {{"12:30:00"}, {"15:45:00"}, {"19:20:00"}}),

SplitTime = Table.AddColumn(Source, "TimeSplit", each Time.ToRecord([Time])),

ExpandTimeSplit = Table.ExpandRecordColumn(SplitTime, "TimeSplit", {"Hour", "Minute", "Second"})

in

ExpandTimeSplit


The above M code defines a table with a single column “Time” that contains three time values. It then adds a new column “TimeSplit” to the table using the Time.ToRecord function to split the time values into their individual components. Finally, it expands the “TimeSplit” column to show the individual components as separate columns.

The Time.ToRecord function is a powerful tool in Power Query that allows users to easily split time values into their individual components. By understanding the M code behind the function, users can gain a deeper understanding of how Power Query works and how they can use it to transform and shape their data.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)