Date.IsInNextYear

D

T

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

Understanding the Date.IsInNextYear Function

Before we dive deep into the M code behind the Date.IsInNextYear function, let’s first understand what this function does. The Date.IsInNextYear function takes a date value as an argument and returns a Boolean value indicating whether the date is in the next year or not.

For example, consider the following date value:


Date.IsInNextYear(#date(2021, 12, 31))


The above expression will return a value of "True" because the date value represents a date in the next year.

The M Code Behind the Date.IsInNextYear Function

Behind the scenes, the Date.IsInNextYear function is written in M code. M is the formula language used in Power Query. It is used to create custom functions, perform data transformations, and load data into Excel.

The M code behind the Date.IsInNextYear function is relatively simple. It consists of a single line of code:


each Date.Year([Date]) = Date.Year(Date.AddYears(DateTime.LocalNow(),1))


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

The each Keyword

The each keyword is used to indicate that the following expression should be evaluated for each row of data in the table.

The Date.Year Function

The Date.Year function is used to extract the year value from the date value.


Date.Year([Date])


This expression returns the year value from the "Date" column in the current row of data.

The Date.AddYears Function

The Date.AddYears function is used to add a specified number of years to a given date value.


Date.AddYears(DateTime.LocalNow(),1)


This expression returns the date value representing the date one year from the current date.

The = Operator

The = operator is used to compare the year value of the date in the current row of data with the year value of the date one year from the current date.


Date.Year([Date]) = Date.Year(Date.AddYears(DateTime.LocalNow(),1))


If the two year values are the same, the expression returns a value of "True." Otherwise, it returns a value of "False."

In conclusion, the Date.IsInNextYear function is a useful function in Power Query that helps users to check if a given date is in the next year or not. The M code behind this function is relatively simple and consists of a single line of code that compares the year value of a given date with the year value of the date one year from the current date. Understanding the M code behind this function can help users to customize and create their own functions in Power Query.

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)