Date.Year

D

T

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

One of the key features of Power Query is the M language, which is used to define the data transformation steps. The M language is a functional programming language that is used to define data transformation queries. In this article, we are going to take a closer look at the M code behind the Power Query M function Date.Year.

What is the Date.Year Function?

The Date.Year function is a built-in function in Power Query that is used to extract the year from a given date. The function takes a date value as input and returns the year value as an integer. For example, the formula `Date.Year(#date(2021,1,1))` returns the value 2021.

Understanding the M Code Behind the Date.Year Function

When you use the Date.Year function in Power Query, you are actually using underlying M code to perform the data transformation. Let’s take a look at the M code behind the Date.Year function.


let

dateYear = (date) =>

Date.Year(date)

in

dateYear


The M code above defines a function called `dateYear`, which takes a date value as input and returns the year value. The function is defined using the `let` keyword, which is used to define local variables. In this case, we are defining a variable called `dateYear` that is a function.

The function `dateYear` is defined using an arrow function syntax, which is a shorthand way of defining functions in M. The function takes a single parameter called `date`, which represents the date value that is passed to the Date.Year function.

Inside the function, we simply call the built-in Date.Year function, passing the `date` parameter as input. This returns the year value for the given date.

Finally, the function is returned using the `in` keyword, which is used to define the output of the function.

Using the Date.Year Function in Power Query

Now that we understand the M code behind the Date.Year function, let's take a look at how to use it in Power Query.

To use the Date.Year function in Power Query, follow these steps:

1. Open Power Query and connect to your data source.

2. In the Power Query Editor, select the column that contains the date values you want to extract the year from.

3. Click the "Add Column" tab and select "Custom Column" from the dropdown menu.

4. In the "Custom Column" dialog box, enter a name for the column and enter the following formula: `Date.Year([ColumnName])`

5. Click "OK" to create the new column.

The formula above uses the Date.Year function to extract the year value from the date column specified in the `[ColumnName]` variable. Power Query will automatically create a new column with the year values.

The M code behind the Power Query M function Date.Year is a simple yet powerful function that allows you to extract the year value from a given date. Understanding the M code behind the Date.Year function can help you to better understand how Power Query works and how to use it to transform your data. By following the steps outlined in this article, you can use the Date.Year function in Power Query to extract the year values from your date columns.

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)