Date.DayOfWeekName

D

T

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

What is the Date.DayOfWeekName Function?

The Date.DayOfWeekName function is a Power Query M function that returns the name of the day of the week for a given date. It is a simple function that takes a date as an argument and returns a string value representing the name of the day of the week. For example, if you provide the date 1/1/2021 as an argument, the function will return “Friday” as the output.

How Does the Function Work?

The M code behind the Date.DayOfWeekName function is relatively simple. It uses the List.Dates function to generate a list of dates for a given period and then applies the Date.DayOfWeek function to each date in the list to get the day of the week. Finally, it uses the List.Transform function to convert the numeric day of the week value into a string value representing the name of the day of the week.

Here is the M code behind the Date.DayOfWeekName function:


(Date as date) =>

let

dayNumber = Date.DayOfWeek(Date),

dayNames = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”},

dayName = dayNames{dayNumber}

in

dayName


Let's break down this code step by step:

1. The function takes a date as an argument.

2. The DayOfWeek function is used to get the numeric value representing the day of the week for the given date.

3. The dayNames variable is defined as an array of strings representing the names of the days of the week.

4. The dayName variable is defined as the value of the element in the dayNames array corresponding to the numeric day of the week value.

How Can You Use the Date.DayOfWeekName Function?

The Date.DayOfWeekName function can be used to manipulate and transform data in various ways. Here are some examples of how you can use this function:

1. Group Data by Day of the Week

You can use the Date.DayOfWeekName function to group your data by day of the week. For example, if you have a table of sales data with a date column, you can group the data by day of the week to get a weekly sales report. To do this, you can create a calculated column that uses the Date.DayOfWeekName function to get the day of the week name for each date.

2. Filter Data by Day of the Week

You can also use the Date.DayOfWeekName function to filter your data by day of the week. For example, if you want to extract data for a specific day of the week, you can create a custom filter that uses the Date.DayOfWeekName function to filter the data based on the day of the week name.

3. Create Custom Date Formats

You can use the Date.DayOfWeekName function to create custom date formats that include the day of the week name. For example, if you want to create a report that displays the date and the day of the week, you can use the Date.DayOfWeekName function to add the day of the week name to the date value.

The Date.DayOfWeekName function is a simple but powerful function that can be used to manipulate and transform data in various ways. By understanding the M code behind this function, you can gain a deeper understanding of how it works and how you can use it to solve real-world data problems. We hope this article has been helpful in explaining the M code behind the Power Query M function Date.DayOfWeekName.

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)