#time

D

T

The M Code Behind the Power Query M function #time

What is the #time function?

The #time function is a part of the DateTime.Local function family in Power Query. It allows you to create a new datetime value by specifying the hour, minute, and second components. The syntax for the #time function is as follows:


#time(hour, minute, second)


For example, to create a datetime value for 10:30:00, you would use the following M code:


#time(10, 30, 0)


Understanding the M code behind #time

The M code behind the #time function is quite simple. It takes the hour, minute, and second components and returns a new datetime value. Let’s take a closer look at the M code for the #time function:


(DateTime.LocalTime(hour, minute, second))


As you can see, the M code for the #time function uses the DateTime.LocalTime function to create a new datetime value. The DateTime.LocalTime function takes three parameters – hour, minute, and second – and returns a new datetime value with the specified time components.

Using the #time function in Power Query

Now that we understand the M code behind the #time function, let’s take a look at how we can use it in Power Query. One common use case for the #time function is to calculate the duration between two datetime values. For example, let’s say we have a table with two columns – Start Time and End Time – and we want to calculate the duration between these two times. We can use the #time function to extract the hour, minute, and second components of each datetime value and then calculate the duration between them.

Here’s how we can do it using Power Query:

1. Load the table into Power Query.

2. Add a custom column using the following M code:


Duration.TotalSeconds(DateTime.LocalTime([End Time])-DateTime.LocalTime([Start Time]))


This code uses the DateTime.LocalTime function to extract the hour, minute, and second components of the Start Time and End Time columns and then calculates the duration between them using the Duration.TotalSeconds function.

In this article, we took a look at the M code behind the #time function in Power Query. We learned that the #time function allows you to create a new datetime value by specifying the hour, minute, and second components. We also learned that the M code behind the #time function is quite simple and uses the DateTime.LocalTime function to create a new datetime value. Finally, we saw how we can use the #time function to calculate the duration between two datetime values in Power Query.

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)