DateTime.IsInCurrentHour

D

T

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

In this article, we will take a closer look at the M code behind the Power Query M function DateTime.IsInCurrentHour.

Overview of DateTime.IsInCurrentHour

The DateTime.IsInCurrentHour function is a Power Query M function that returns a boolean value indicating whether a given datetime value falls within the current hour. The function takes a single parameter, datetime, which represents the datetime value to be tested.

The syntax of the function is as follows:


DateTime.IsInCurrentHour(datetime as any) as logical


Here, datetime is any valid datetime value, and the function returns a logical value (true or false) indicating whether the datetime falls within the current hour.

The M Code Behind the Function

The M code behind the DateTime.IsInCurrentHour function is relatively simple. The function is implemented using the DateTime.LocalNow function, which returns the current date and time in the local time zone, and the DateTime.Hour function, which extracts the hour component of a datetime value.

Here is the M code for the DateTime.IsInCurrentHour function:


(DateTime.LocalNow() – DateTime.FromText(“00:00:00”))/#duration(0, 1, 0, 0) –

(DateTime.From(DateTime.LocalNow()))/#duration(0, 1, 0, 0) = 0

and

(DateTime.Hour(datetime) = DateTime.Hour(DateTime.LocalNow()))


Let's break down the code step by step:

1. The first line of the code calculates the number of minutes that have elapsed since midnight of the current day. This is done by subtracting the current date and time (returned by the DateTime.LocalNow function) from a datetime value representing midnight of the current day (created using the DateTime.FromText function). The result is divided by a duration value representing one hour (#duration(0, 1, 0, 0)) to get the number of hours that have elapsed since midnight.

2. The second line of the code calculates the number of hours that have elapsed since midnight of the current day. This is done by subtracting the current date and time (returned by the DateTime.LocalNow function) from a datetime value representing midnight of the current day (created using the DateTime.From function). The result is divided by a duration value representing one hour (#duration(0, 1, 0, 0)) to get the number of hours that have elapsed since midnight.

3. The first part of the boolean expression (before the 'and' operator) checks whether the current time falls within the current hour. This is done by checking whether the difference between the number of minutes elapsed since midnight and the number of hours elapsed since midnight is equal to 0. If the current time is within the current hour, this expression will evaluate to true.

4. The second part of the boolean expression (after the 'and' operator) checks whether the datetime value being tested falls within the current hour. This is done by comparing the hour component of the datetime value to the hour component of the current date and time (returned by the DateTime.LocalNow function). If the datetime value falls within the current hour, this expression will evaluate to true.

In this article, we have explored the M code behind the Power Query M function DateTime.IsInCurrentHour. We have seen how this simple function is implemented using the DateTime.LocalNow and DateTime.Hour functions, and how it can be used to test whether a given datetime value falls within the current hour. By understanding the M code behind this function, we can gain a deeper appreciation for the power and flexibility of the M language and the capabilities of Power Query as a data processing and transformation tool.

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)