Time.StartOfHour

D

T

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

One such function is the Time.StartOfHour function, which returns the start of the hour for a given time value. In this article, we will take a closer look at the M code behind the Time.StartOfHour function, and understand how it works.

Understanding the Time.StartOfHour Function

Before we dive into the M code behind the Time.StartOfHour function, let’s first understand what the function does. The Time.StartOfHour function takes a time value as input, and returns the start of the hour for that time value.

For example, if we have a time value of 9:30 AM, the Time.StartOfHour function would return 9:00 AM. Similarly, if we have a time value of 2:45 PM, the function would return 2:00 PM.

Now that we understand what the Time.StartOfHour function does, let’s take a closer look at the M code behind the function.

The M Code Behind the Time.StartOfHour Function

The M code behind the Time.StartOfHour function is relatively simple. Here is the code for the function:


(Time as datetime) =>

Time.FromText(Text.Start(Text.From(Time), "yyyy-MM-ddThh"))


Let’s break down this code and understand what each part does.

The Input Parameter

The first part of the code defines the input parameter for the function:


(Time as datetime) =>


The input parameter for the Time.StartOfHour function is a datetime value, which represents a specific date and time.

Converting the Time Value to Text

The next part of the code converts the input time value to text:


Text.From(Time)


This converts the datetime value to text, so that we can extract specific parts of the time value.

Extracting the Year, Month, Day, and Hour

The next part of the code extracts the year, month, day, and hour from the time value:


Text.Start(Text.From(Time), "yyyy-MM-ddThh")


This uses the Text.From and Text.Start functions to extract the year, month, day, and hour from the time value. The “yyyy-MM-ddThh” format string specifies that we want to extract the year, month, and day in the format “yyyy-MM-dd”, and the hour in the format “hh”.

Converting the Text Value Back to a Time Value

The final part of the code converts the extracted text value back to a time value:


Time.FromText(Text.Start(Text.From(Time), "yyyy-MM-ddThh"))


This uses the Time.FromText function to convert the extracted text value back to a time value. The result is the start of the hour for the input time value.

The M code behind the Time.StartOfHour function is relatively simple, but it demonstrates the power and flexibility of the M language. By understanding how this function works, we can better appreciate the capabilities of Power Query, and use it more effectively to transform and analyze 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)