Date.StartOfQuarter

D

T

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

Understanding the Date.StartOfQuarter Function

The Date.StartOfQuarter function is used to return the first day of the quarter for a given date. This function takes a date as an input and returns a date. The syntax for the Date.StartOfQuarter function is as follows:


Date.StartOfQuarter(date as date) as date


The Date.StartOfQuarter function takes a single argument, which is a date value. This argument can be a reference to a cell that contains a date, or a date value entered directly into the function.

The M Code Behind the Date.StartOfQuarter Function

The M code behind the Date.StartOfQuarter function is relatively simple. The following code snippet shows the M code behind the Date.StartOfQuarter function:


(date as date) => Date.StartOfQuarter(date)


This code defines an anonymous function that takes a date as an input and calls the Date.StartOfQuarter function with the input date. The output of the Date.StartOfQuarter function is returned as the output of the anonymous function.

The Date.StartOfQuarter function uses the Date.From function to convert the input date into a date value that can be used for calculations. The following code snippet shows the M code for the Date.From function:


Date.From(date as any) as nullable date


The Date.From function takes a single argument, which can be any data type that can be converted to a date value. This function returns a nullable date value.

Using the Date.StartOfQuarter Function

The Date.StartOfQuarter function can be used in a variety of ways. For example, you can use this function to calculate the first day of the current quarter, as shown in the following code snippet:


let

today = DateTime.LocalNow(),

startOfQuarter = Date.StartOfQuarter(today)

in

startOfQuarter


This code defines a variable called today that contains the current date and time. The Date.StartOfQuarter function is called with the today variable as an input, and the output is stored in a variable called startOfQuarter.

You can also use the Date.StartOfQuarter function to calculate the first day of the previous quarter, as shown in the following code snippet:


let

today = DateTime.LocalNow(),

startOfQuarter = Date.StartOfQuarter(Date.AddQuarters(today, -1))

in

startOfQuarter


This code defines a variable called today that contains the current date and time. The Date.AddQuarters function is used to subtract one quarter from the today variable, and the result is passed to the Date.StartOfQuarter function. The output of the Date.StartOfQuarter function is stored in a variable called startOfQuarter.

The Date.StartOfQuarter function is a powerful tool for calculating the first day of a quarter for a given date. The M code behind this function is relatively simple, but it can be used in a variety of ways to transform and cleanse data. By understanding the M code behind the Date.StartOfQuarter function, you can gain a deeper understanding of how Power Query works, and how you can use it to transform and cleanse your 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)