Mastering Date Transformations in Power Query: A Beginner’s Guide to M Code
Transforming and analyzing date-related data is an essential skill when working with Power Query in Excel. Whether you're creating custom columns to extract the name of the month, the day of the week, or performing other date-related transformations, mastering M Code can take your data analysis to the next level. But don’t worry if you’re new to Power Query — we’ll simplify everything for you!
In this blog post, we’ll dive deep into the world of date transformations using Power Query. Starting with the basics, we’ll walk you through how to extract specific details, such as abbreviations for months and days, and explore other useful transformations to make your data more meaningful. With easy-to-understand explanations, step-by-step M Code examples, and practical use cases, this guide will help you unlock the full potential of date data in Power Query.
By the end of this blog, you'll not only understand how to manipulate date columns effortlessly but also gain confidence in writing M Code to achieve customized transformations. Let’s get started and make date transformations in Power Query as easy as a few clicks!
1. Extract the Year from the Date
This transformation creates a new column that extracts the year from the [Date]
column.
M Code:
= Table.AddColumn(#"PreviousStepName", "Year", each Date.Year([Date]), type number)
Output Example:
Date | Year |
---|---|
2023-05-12 | 2023 |
2023-06-15 | 2023 |
2024-01-01 | 2024 |
2. Extract the Month Number
This transformation creates a new column that extracts the numeric value of the month (e.g., 1 for January, 2 for February).
M Code:
= Table.AddColumn(#"PreviousStepName", "Month_Number", each Date.Month([Date]), type number)
Output Example:
Date | Month_Number |
---|---|
2023-05-12 | 5 |
2023-06-15 | 6 |
2024-01-01 | 1 |
3. Extract the Week Number
This transformation creates a column to show the week number of the year for each date.
M Code:
= Table.AddColumn(#"PreviousStepName", "Week_Number", each Date.WeekOfYear([Date]), type number)
Output Example:
Date | Week_Number |
---|---|
2023-05-12 | 19 |
2023-06-15 | 24 |
2024-01-01 | 1 |
4. Extract the Day Number
This transformation creates a new column showing the day number of the month for each date.
M Code:
= Table.AddColumn(#"PreviousStepName", "Day_Number", each Date.Day([Date]), type number)
Output Example:
Date | Day_Number |
---|---|
2023-05-12 | 12 |
2023-06-15 | 15 |
2024-01-01 | 1 |
5. Is the Date a Weekend?
This transformation creates a column to indicate whether the date falls on a weekend (Saturday or Sunday).
M Code:
= Table.AddColumn(#"PreviousStepName", "Is_Weekend", each Date.DayOfWeek([Date], Day.Sunday) >= 5, type logical)
Output Example:
Date | Is_Weekend |
---|---|
2023-05-12 | false |
2023-05-13 | true |
2023-05-14 | true |
6. Find the Quarter of the Year
This transformation creates a column showing the quarter of the year (e.g., Q1, Q2, Q3, Q4).
M Code:
= Table.AddColumn(#"PreviousStepName", "Quarter", each "Q" & Text.From(Date.QuarterOfYear([Date])), type text)
Output Example:
Date | Quarter |
---|---|
2023-05-12 | Q2 |
2023-06-15 | Q2 |
2024-01-01 | Q1 |
7. Day of the Year
This transformation calculates the day number of the year for each date (e.g., January 1 is day 1, December 31 is day 365 or 366).
M Code:
= Table.AddColumn(#"PreviousStepName", "Day_Of_Year", each Date.DayOfYear([Date]), type number)
Output Example:
Date | Day_Of_Year |
---|---|
2023-05-12 | 132 |
2023-06-15 | 166 |
2024-01-01 | 1 |
8. Add Custom Date Formats
This transformation creates a column that formats the date in a specific way (e.g., MM/dd/yyyy
).
M Code:
= Table.AddColumn(#"PreviousStepName", "Custom_Date_Format", each Text.PadStart(Text.From(Date.Month([Date])), 2, "0") & "/" & Text.PadStart(Text.From(Date.Day([Date])), 2, "0") & "/" & Text.From(Date.Year([Date])), type text)
Output Example:
Date | Custom_Date_Format |
---|---|
2023-05-12 | 05/12/2023 |
2023-06-15 | 06/15/2023 |
2024-01-01 | 01/01/2024 |
9. First Day of the Month
This transformation calculates the first day of the month for each date.
M Code:
= Table.AddColumn(#"PreviousStepName", "First_Day_Of_Month", each Date.StartOfMonth([Date]), type date)
Output Example:
Date | First_Day_Of_Month |
---|---|
2023-05-12 | 2023-05-01 |
2023-06-15 | 2023-06-01 |
2024-01-01 | 2024-01-01 |
10. Last Day of the Month
This transformation calculates the last day of the month for each date.
M Code:
= Table.AddColumn(#"PreviousStepName", "Last_Day_Of_Month", each Date.EndOfMonth([Date]), type date)
Output Example:
Date | Last_Day_Of_Month |
---|---|
2023-05-12 | 2023-05-31 |
2023-06-15 | 2023-06-30 |
2024-01-01 | 2024-01-31 |
11. Weekday Number (0 = Sunday, 6 = Saturday)
This transformation creates a column showing the day of the week as a number (0 for Sunday, 1 for Monday, etc.).
M Code:
= Table.AddColumn(#"PreviousStepName", "Weekday_Number", each Date.DayOfWeek([Date], Day.Sunday), type number)
Output Example:
Date | Weekday_Number |
---|---|
2023-05-12 | 5 |
2023-05-13 | 6 |
2023-05-14 | 0 |
12. Custom Weekday Labels
This transformation allows you to create a custom weekday label for each date.
M Code:
= Table.AddColumn(#"PreviousStepName", "Custom_Weekday_Label", each if Date.DayOfWeek([Date], Day.Sunday) = 0 then "Weekend" else "Weekday", type text)
Output Example:
Date | Custom_Weekday_Label |
---|---|
2023-05-12 | Weekday |
2023-05-13 | Weekend |
2023-05-14 | Weekend |
Some Advanced M Code functions related to Date Transformations:
To extract the Name of the Month (e.g., Jan from the date 2023-01-15) from the date in the given example, you can use the Date.MonthName function in Power Query (M code). Below is the M code to accomplish this:
Date | Month |
---|---|
2023-01-15 | Jan |
2023-02-20 | Feb |
2023-03-10 | Mar |
Let's break down the provided M Code step by step, add commentary to guide you, and provide an easy-to-understand explanation for what it does:
M Code:
= Table.AddColumn(#"Added Custom", "Day_Of_Week", each Text.Start(Date.DayOfWeekName([Date]),3), type text)
Step-by-Step Parsing and Commentary:
= Table.AddColumn
:- This is a function in Power Query used to add a new column to an existing table.
- The new column will be calculated based on the logic provided in the code.
#"Added Custom"
:- This is a reference to the previous step in your query. In Power Query, every step is named (either automatically or manually by you).
- In this case,
"Added Custom"
refers to the name of the table/output from the previous step.
"Day_Of_Week"
:- This is the name of the new column being created.
- The new column will store the abbreviated names of the days of the week (e.g., "Mon", "Tue", "Wed").
each
:- This is a keyword in Power Query that is used to apply the subsequent logic to each row in the table.
- It essentially means "perform the following operation on every row in the table."
Date.DayOfWeekName([Date])
:- This is a Power Query function that retrieves the full name of the day of the week for the value in the
[Date]
column. - For example:
- If
[Date]
is2023-05-12
, this function will return"Friday"
. - If
[Date]
is2023-05-13
, this function will return"Saturday"
.
- If
- This is a Power Query function that retrieves the full name of the day of the week for the value in the
Text.Start(..., 3)
:- The
Text.Start
function takes the result ofDate.DayOfWeekName
(e.g.,"Friday"
) and extracts the first 3 characters. - For example:
"Friday"
becomes"Fri"
."Saturday"
becomes"Sat"
.
- This is how you get the abbreviated day names in the new column.
- The
type text
:- This specifies that the data type of the new column should be text.
- This ensures that the values in the
"Day_Of_Week"
column are stored as text (e.g., "Mon", "Tue").
What Does This Code Do?
- The code creates a new column called
"Day_Of_Week"
. - For each row in the table:
- It looks at the value in the
[Date]
column. - It calculates the full day name (e.g.,
"Monday"
or"Tuesday"
) using theDate.DayOfWeekName
function. - It extracts the first 3 characters of the day name (e.g.,
"Mon"
,"Tue"
) using theText.Start
function. - The result is stored in the new column as text.
- It looks at the value in the
Example:
If your table initially looks like this:
Date |
---|
2023-05-12 |
2023-05-13 |
2023-05-14 |
After applying the M Code, it will look like this:
Date | Day_Of_Week |
---|---|
2023-05-12 | Fri |
2023-05-13 | Sat |
2023-05-14 | Sun |
For Beginners:
This code will:
- Create a new column called
"Day_Of_Week"
. - For each row, look at the date in the
[Date]
column. - Figure out the day of the week (e.g.,
"Monday"
or"Friday"
) for the date. - Take the first 3 letters of the day (e.g.,
"Mon"
or"Fri"
). - Save this value as text in the new column.
Notes:
- Make sure the
[Date]
column in your table contains valid date values. - If the
[Date]
column contains invalid data (e.g., text or null values), you may encounter errors.
EmoticonEmoticon