Earlier this week, I wrote about working with date and time in Power Automate. I explained how to use 4 functions to return the month and year from the current date. But, the values come back as numbers. What if you want the name of the day of the week or the month, like Monday or January? Returning names in Power Automate takes different expressions and the use of arrays, but it’s easy! Read on to learn how and leave a comment explaining how you’ve used arrays in Power Automate.
There is an easier way to get the names of the day of the week and month from Power Automate. However, the content of the post is still useful if you have never worked with arrays. Also, I can think of other use cases where it would be applicable, such as needing to add “st”, “nd”, and “rd” to dates like 1st, 2nd, and 3rd.
The easiest way to get the name of the day of the week from a date is to use formatDateTime with the syntax ‘dddd’ as shown below. My date is an input from a manual trigger.
The same is true for the month using this syntax.
My original use case was building a flow to automate a once-a-month email. Sending an email once a month isn’t super time-consuming, unless you need to change 7 dates and 2 days of the week in the email body. When done manually, the risk of error is high, and you probably don’t want to do it from your phone. That means being at a computer. All of a sudden, automation triggered from a mobile device looks pretty good.
For ease of explanation, I built a simpler flow to illustrate how to perform this task using a manual trigger. I’m going to walk thru the same 3 steps to get the name of the days of the week and then the name of the month. This flow uses a manual trigger and Initiate Variable and Compose actions.
This is a very simple 3-step process.
- Create array variable(s) with names
- Build Compose action with expression to return the number related to the day or month
- Create Compose action with expression to return the name of the day or month
I’ll walk thru the steps for getting the name of the day of the week first. Then, I’ll show the outputs for 2 different dates. Finally, Then, I’ll do the same thing for the name of the month. I think you’ll be surprised at where these two processes differ.
In order to make this happen, we need to learn about array variables.
Name of Day of Week
Step 1 – Create Array Variable
My last post introduced the Variables category of actions. Think of variables as containers to hold your data, whether that data is a string, a number, a date, or a list of data (an array). When working with variables, always begin with the Initialize variable action, which specifies the type of variable.
In order to return the name of the day of the week or the month, we begin by creating an array variable as shown below with the names of the days of the week. Note, the content needs to be enclosed in square brackets, separated by commas, and wrapped in quotes.
Step 2 – Compose to Find Number
In step 2, we write an expression to return the number associated with the day of the week. For this task, Power Automate has a super handy dayOfWeek function that I combine with dynamic content from the trigger. The result comes back as an integer.
I want to pause here and review the trigger schema because I ran into a bit of confusion when working with my original use case. If we inspect the trigger after the flow run, you can see I entered 2022-01-05 as the Trigger date. “Trigger date” is the title of my date input. But, the name of the input in the code or syntax is ‘date’. This is visible in the screenshot above and also in the 2nd screenshot below. If I had added another date into the manual input it would have been named ‘date_1’. Power Automate names the manual inputs based on the type and number of inputs.
Okay, now we are ready to actually get the name of the day of the week.
Step 3 – Compose to Return Name of Day
To do so, we use another Compose action and reference the array variable we want to pull from and the location or line in the array that we want. If I always wanted to pull the 4th line in the array, I would write this expression.
You see, array references always start at zero. So, to pull the 4th line in the array, I use 3. But, we want to pull based on the integer returned from the dayOfWeek function, so we reference that output in the expression. Note, you must click in the Expression tab and type out “variables(‘DaysOfWeek’)”.
Now, I am going to run the flow using 2 different dates, September 3rd and September 4th, 2022. These dates are Saturday and Sunday respectively. Here’s a reminder of the array variable.
Array positions start at zero. Therefore…
- Sunday = 0
- Monday = 1
- Tuesday = 2
- Wednesday = 3
- Thursday = 4
- Friday = 5
- Saturday = 6
The dayOfWeek function returns 6 for September 3rd. This is a Saturday, and that does return the correct day of the week.
The dayOfWeek function returns 0 for September 4th. This is a Sunday, and that does return the correct day of the week.
Name of Month
Step 1 – Create Array Variable
Okay, now we will find the name of the month. The first step is the same, with the only difference being the array content.
Step 2 – Compose to Find Number
Our second Compose differs a bit. We use the formatDateTime function to return the month number, which will always return the result in a string. In order to pull from an array, we must convert it to an integer. That’s why you see the entire expression wrapped in the int function.
Step 3 – Compose to Return Name of Month
Now, here’s where things get different. The dayOfWeek function will return a value of 0 for Sunday, and everything lines up nicely with our array. However, when using formatDateTime to return the number of the month, the result is a number (really a string) between 01 and 12.
Because array references start at zero, we must subtract one from our integer to return the correct line reference for the array. We use the sub (or subtract) function to do this. For example, the following expression would return a result of 2.
sub(5,3) = 2
Here is the final expression. We reference the variable, and then use the sub function to subtract one from the month integer, which gives the correct line reference in the array. Let’s look at the outputs.
I entered the date 2022-01-01, and the following came back.
I entered the date 2022-12-05, and the following came back.
And now you know how to get the name of a month or day of the week in Power Automate. My next upgrade for this automation is to add in another array variable so I can insert date abbreviations list 1st, 2nd, or 3rd. How have you used array variables in Power Automate flows? Let me know if the comments below.