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

What is DateTime.LocalNow?

Before we dive into the M code, let’s review what DateTime.LocalNow actually does. This function returns the current date and time in the user’s local time zone. For example, if you’re in New York and run DateTime.LocalNow, you’ll get the current date and time in Eastern Standard Time.

The M Code

The M code for DateTime.LocalNow is relatively simple. Here’s what it looks like:

() => DateTimeZone.LocalNow()

Let's break this down piece by piece.

The Anonymous Function

The code starts with an anonymous function, denoted by the parentheses with nothing inside. An anonymous function is simply a function without a name – it's defined on the spot and is only used once. In this case, the anonymous function is used to call the DateTimeZone.LocalNow function.

The DateTimeZone.LocalNow Function

The DateTimeZone.LocalNow function is what actually returns the current date and time in the user's local time zone. This function is part of the DateTimeZone type, which provides various functions for working with dates and times in different time zones.

What's the Point of Using an Anonymous Function?

You might be wondering why the M code for DateTime.LocalNow uses an anonymous function at all. After all, couldn't we just call DateTimeZone.LocalNow directly?

The reason for the anonymous function is that it allows us to delay the execution of the DateTimeZone.LocalNow function until it's actually needed. In other words, the code inside the anonymous function doesn't get executed until the function is called.

This might not seem like a big deal, but it can actually be quite useful in certain scenarios. For example, if you have a query that's pulling in data from multiple sources and doing a lot of transformations, you might not want to waste time calculating the current date and time until the very end of the query. By using an anonymous function, you can delay the execution of DateTimeZone.LocalNow until the final step of the query, which can help improve performance.

In this article, we've taken a closer look at the M code that powers the DateTime.LocalNow function in Power Query. We've seen how this function uses an anonymous function to delay the execution of DateTimeZone.LocalNow until it's actually needed, and we've reviewed the basics of what DateTime.LocalNow does. Hopefully, this information will help you better understand the inner workings of Power Query and how to use it effectively in your own projects.

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)