Date.AddQuarters

Date.AddQuarters

D

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

Understanding the Date.AddQuarters Function

The Date.AddQuarters function is used to add a specified number of quarters to a given date. The syntax for the function is as follows:


Date.AddQuarters(dateTime as any, numberOfQuarters as number) as any


The function takes two arguments. The first argument, dateTime, is the date to which the quarters will be added. The second argument, numberOfQuarters, is the number of quarters to be added to the date.

For example, if we wanted to add two quarters to the date 1/1/2021, we would use the following formula:


Date.AddQuarters(#”1/1/2021″, 2)


This would result in the date 7/1/2021.

The M Code Behind the Date.AddQuarters Function

Behind the scenes, the Date.AddQuarters function is powered by M code. M code is the language used by Power Query to perform data transformations. When we use the Date.AddQuarters function, Power Query generates M code that performs the necessary calculations.

The M code for the Date.AddQuarters function is as follows:


() => (dateTime as any, numberOfQuarters as number) =>

DateTime.LocalNow() + #duration(0, (numberOfQuarters 3), 0, 0)


Let's break down this code and understand what it does.

The First Line

The first line of the code defines a function with no arguments. This function is used to create a closure that will be returned by the Date.AddQuarters function.


() => (dateTime as any, numberOfQuarters as number) =>


The Second Line

The second line of the code performs the actual calculation. It uses the DateTime.LocalNow() function to get the current date and time. This is then added to a duration that is calculated based on the number of quarters to be added.


DateTime.LocalNow() + #duration(0, (numberOfQuarters 3), 0, 0)


The #duration function is used to create a duration value. The duration is calculated by multiplying the number of quarters by three months (since there are three months in a quarter). The resulting duration is then added to the current date and time to get the new date.

In this article, we have taken a closer look at the M code behind the Power Query M function Date.AddQuarters. We have seen how the function works and how it is powered by M code. By understanding the underlying code, we can better utilize the Date.AddQuarters function and other 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)

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: