DateTime.ToRecord

D

T

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

Understanding the DateTime.ToRecord Function

The DateTime.ToRecord function is used to convert a datetime value into a record format. This function takes a single argument, which is the datetime value that we want to convert. The output of this function is a record that contains separate fields for year, month, day, hour, minute, second, and millisecond values.

Here’s an example of how we can use the DateTime.ToRecord function:


let

myDateTime = #datetime(2022, 1, 1, 10, 30, 0),

recordValue = DateTime.ToRecord(myDateTime)

in

recordValue


In this example, we first create a datetime value using the #datetime function. We then pass this value to the DateTime.ToRecord function, which returns a record containing the individual date and time components.

Dive into the M Code

Now let's take a closer look at the M code behind this function. Here's the code for the DateTime.ToRecord function:


(DateTimeValue as datetime) =>

#datetimezone(

Date.Year(DateTimeValue),

Date.Month(DateTimeValue),

Date.Day(DateTimeValue),

Time.Hour(DateTimeValue),

Time.Minute(DateTimeValue),

Time.Second(DateTimeValue),

Duration.FromText(“00:00:00.0”),

DateTime.LocalNow() – DateTime.UtcNow

)


As you can see, this function takes a single argument, which is the datetime value we want to convert. The function then uses a combination of the Date and Time functions to extract the year, month, day, hour, minute, and second values from the datetime value.

The function also includes two additional arguments: Duration.FromText("00:00:00.0") and DateTime.LocalNow() - DateTime.UtcNow. The first argument specifies the milliseconds value, which is always zero in this case. The second argument specifies the timezone offset, which is calculated as the difference between the local time and UTC time.

The result of this function is a record that contains the individual date and time components, as we saw in the previous example.

The DateTime.ToRecord function is a powerful tool in Power Query for working with date and time values. By understanding the M code behind this function, we can gain a deeper understanding of how it works and how we can use it in our own projects.

Hopefully, this article has provided some valuable insights into the M code behind the DateTime.ToRecord function. As you continue to work with Power Query, don't be afraid to explore the other date and time functions that are available and experiment with them in your own projects.

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)