Date.IsInNextNDays

D

T

The M Code Behind the Power Query M function Date.IsInNextNDays

Understanding the Date.IsInNextNDays Function

Before we dive into the M code behind the Date.IsInNextNDays function, let’s first understand what this function does. The Date.IsInNextNDays function takes in two arguments: a date and a number of days (N). It then checks whether the given date falls within the next N number of days.

For example, if today is January 1st, 2022, and we want to check whether January 7th, 2022 falls within the next 7 days, we can use the Date.IsInNextNDays function as follows:


Date.IsInNextNDays(#date(2022,1,7), 7)


This will return true, since January 7th, 2022 falls within the next 7 days from January 1st, 2022.

The M Code Behind the Date.IsInNextNDays Function

Now that we understand what the Date.IsInNextNDays function does, let's take a look at the M code behind this function. The M code for the Date.IsInNextNDays function is as follows:


(date as date, days as number) =>

let

today = Date.From(DateTime.LocalNow()),

endDate = Date.AddDays(today, days),

result = date <= endDate

in

result


Let's break down this code into smaller parts to understand how it works.

The Input Parameters

The Date.IsInNextNDays function takes in two parameters: `date` and `days`. `date` is the date that we want to check, and `days` is the number of days from today that we want to check against.

The LocalNow Function

The `today` variable in the M code above is set to the current date and time using the `DateTime.LocalNow()` function. This function returns the current date and time in the local time zone.

The AddDays Function

The `endDate` variable is set using the `Date.AddDays` function, which adds the number of days specified in the `days` parameter to the `today` variable. This gives us the end date that we want to check against.

The Result

The `result` variable is set to `true` if the `date` parameter is less than or equal to the `endDate` variable. This means that the `date` falls within the next `days` number of days from today.

In this article, we explored the M code behind the Power Query M function Date.IsInNextNDays. We learned that this function is used to check whether a given date falls within the next N number of days, and we examined the M code that powers this function. By understanding the M code behind this function, we can gain a better understanding of how Power Query works and how we can use it to transform our 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)