Date.IsInPreviousNYears

D

T

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

What is the Date.IsInPreviousNYears function?

The Date.IsInPreviousNYears function is a Power Query M function that returns true if a date falls within the specified number of years from the current date. It takes two arguments:

– Date: The date to be checked.

– Years: The number of years to check against the current date.

This function is useful for creating dynamic reports that require data from a specific time frame, such as the last 3 years.

How does the Date.IsInPreviousNYears function work?

The Date.IsInPreviousNYears function works by subtracting the specified number of years from the current date using the Date.AddYears function, and then checking if the date is greater than or equal to the resulting date.

Here is the M code for the Date.IsInPreviousNYears function:


(Date as date, Years as number) as logical =>

Date >= Date.AddYears(DateTime.LocalNow(), -Years) and

Date < DateTime.LocalNow()


The function takes the Date and Years arguments and compares the date to the result of subtracting the Years from the current date. If the date falls within the specified time frame, the function returns true, otherwise false.

How to use the Date.IsInPreviousNYears function?

To use the Date.IsInPreviousNYears function, you need to follow these steps:

1. Load your data into Power Query.

2. Select the column that contains the dates you want to filter.

3. Open the Transform tab and click on the "Date" dropdown menu.

4. Select the "Is in Previous N Years" option.

5. Enter the number of years you want to check against the current date.

After applying the filter, only the dates that fall within the specified time frame will be displayed.

The Date.IsInPreviousNYears function is a powerful tool for filtering dates in Power Query. It allows you to easily create dynamic reports that show data from a specific time frame. By understanding how the function works and how to use it, you can save time and effort when preparing your data for analysis.

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)