Date.StartOfYear

Date.StartOfYear

D

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

Understanding the Date.StartOfYear Function

Before we dive into the M code, let’s take a moment to understand what the Date.StartOfYear function does. This function takes a single argument, a date value, and returns a new date value that represents the first day of the year for that date. For example, if you pass in the date value 1/15/2022, the function will return the date value 1/1/2022.

Exploring the M Code Behind Date.StartOfYear

To better understand how the Date.StartOfYear function works, let’s take a look at the M code behind it. You can view the M code by clicking on the “Advanced Editor” button in the Power Query Editor and finding the Date.StartOfYear function in the list of available functions.

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


(date as nullable date) as nullable date =>

let

year = Date.Year(date),

result = #date(year, 1, 1)

in

result


Let's break down this code line by line:

- `(date as nullable date) as nullable date =>` - This line defines the function signature. It takes one argument, `date`, which is a nullable date value, and returns a nullable date value.

- `let` - This line starts a `let` expression, which allows us to define variables and perform calculations.

- `year = Date.Year(date),` - This line defines a variable `year` and sets its value to the year component of the `date` argument using the Date.Year function.

- `result = #date(year, 1, 1)` - This line defines a variable `result` and sets its value to a new date value using the `#date` syntax. The first argument is the year component of the `date` argument, the second argument is `1` to represent the first month of the year, and the third argument is also `1` to represent the first day of the month.

- `in` - This line ends the `let` expression and specifies the result value to be returned, which is the `result` variable.

In this article, we explored the M code behind the Power Query M function Date.StartOfYear. We learned that the function takes a date value as an argument and returns a new date value representing the first day of the year for that date. We also broke down the M code behind the function and saw how it uses the Date.Year and #date functions to calculate the result. By understanding the M code behind these built-in functions, you can gain a deeper understanding of how Power Query works and how to create your own custom functions.

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: