Date.IsLeapYear

Date.IsLeapYear

D

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

What is a Leap Year?

Before we dive into the M code behind the Date.IsLeapYear function, let’s first understand what a leap year is. A leap year is a year that is evenly divisible by 4, except for years that are divisible by 100 but not by 400. For example, the year 2000 was a leap year because it is divisible by 4 and by 400, but the year 1900 was not a leap year because it is divisible by 4 and by 100, but not by 400.

The M Code Behind the Date.IsLeapYear Function

The M code behind the Date.IsLeapYear function is relatively simple. The function takes a single input parameter, which is the year to be tested for leap year. The M code then performs a series of calculations to determine whether the year is a leap year or not.

Here is the M code behind the Date.IsLeapYear function:


(Date.Year(Date.From(DateTime.LocalNow())) mod 4 = 0 and Date.Year(Date.From(DateTime.LocalNow())) mod 100 <> 0) or Date.Year(Date.From(DateTime.LocalNow())) mod 400 = 0


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

1. The Date.Year function takes a date value as input and returns the year component of the date.

2. The Date.From function takes a datetime value as input and returns the date component of the datetime value.

3. The DateTime.LocalNow function returns the current local date and time as a datetime value.

4. The mod operator performs a modulo operation, which returns the remainder of the division of the two operands.

5. The and and or operators perform logical conjunction and disjunction, respectively.

Putting all of these elements together, the M code behind the Date.IsLeapYear function first gets the current year using the DateTime.LocalNow function. It then checks whether the year is evenly divisible by 4 using the mod operator. If the year is divisible by 4, the function checks whether the year is not divisible by 100 using the <> operator. If the year is not divisible by 100 or is divisible by 400, the function returns true, indicating that the year is a leap year. Otherwise, the function returns false, indicating that the year is not a leap year.

Using the Date.IsLeapYear Function

To use the Date.IsLeapYear function in Power Query, simply pass a year value as input to the function. For example, to test whether the year 2020 is a leap year, you can use the following M code:


Date.IsLeapYear(2020)


This will return true, indicating that 2020 is a leap year.

The Date.IsLeapYear function is a powerful tool for determining whether a given year is a leap year or not in Power Query. The M code behind the function is relatively simple and can be easily understood by anyone with a basic understanding of programming. By using the Date.IsLeapYear function, you can ensure that your data analysis and transformation processes in Power Query are accurate and reliable.

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)

Copyright, G Com Solutions Ltd, 2024.
Tower Bridge Business Centre, 46-48 East Smithfield, London E1W 1AW
0800 998 9248   |   9:00 a.m. till 5:30 p.m.

Connect With Me: