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

Understanding DateTime.Time Function

Before we look at the M code, let’s first understand the DateTime.Time function. This function is used to extract the time component from a datetime value. The syntax of this function is as follows:

DateTime.Time(dateTime as any) as any

The parameter `dateTime` can be any valid datetime value. The function returns the time component of the datetime value as a time value.

The M Code Behind DateTime.Time Function

Now that we know what the DateTime.Time function does, let's see how it is implemented in M code.

The M code for the DateTime.Time function is as follows:

(dateTime as any) =>

Time.From(dateTime – Date.From(dateTime))

Let's break down this code into smaller parts and see what each part does.

Part 1: `(dateTime as any) =>`

This part defines a lambda function that takes a single parameter `dateTime` of any data type.

Part 2: `Time.From(`

This part calls the `Time.From` function, which converts a datetime value to a time value.

Part 3: `dateTime - Date.From(dateTime)`

This part calculates the time component of the datetime value. It does this by subtracting the date component of the datetime value from the datetime value itself. The `Date.From` function is used to extract the date component of the datetime value.


Let's look at some examples to see how the DateTime.Time function works in practice.

Example 1

Suppose we have a datetime value `2022-01-01 10:30:00`. To extract the time component from this value, we can use the DateTime.Time function as follows:

DateTime.Time(#datetime(2022, 1, 1, 10, 30, 0))

This will return the time value `10:30:00`.

Example 2

Suppose we have a table with a column `DateTime` that contains datetime values. To extract the time component from each value in this column, we can use the following M code:


Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText(“i45WMlTSUVKyUlQyNDS3MzQy0DA3MzAzs1KLzs8FAA==”, BinaryEncoding.Base64)),

Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DateTime = _t]),

#”Changed Type” = Table.TransformColumnTypes(Source,{{“DateTime”, type datetime}}),

#”Extracted Time” = Table.AddColumn(#”Changed Type”, “Time”, each DateTime.Time([DateTime])),

#”Removed Columns” = Table.RemoveColumns(#”Extracted Time”,{“DateTime”})


#”Removed Columns”

This code creates a new column `Time` that contains the time component of each datetime value in the `DateTime` column.

In this article, we looked at the M code behind the Power Query M function DateTime.Time. We saw how this function works and how to use it in practice. Understanding the M code behind functions can be helpful in debugging and customizing Power Query queries.

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

Upcoming Courses

Contact Us


    Your Name (required)


    Email (required)


    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)