Write complex expressions

Completed

Complex expressions are when you combine more than one function to get the result you want. In the Math functions and Date Time functions sections, you already saw examples of these expressions. You saw that to add three numbers, you need to combine two add functions like:

add(add(12,13),15)

Which resulted in the output of 40.

Then you saw how you use the utcNow function to get the current date and time and then use convertFromUtc to change it to the Eastern Standard time zone as shown here:

convertFromUtc(utcNow(), 'Eastern Standard Time', 'dd-MM-yyyy hh:mm tt' )

Which resulted in the output of 09-02-2024 11:28 AM.

When you think of complex expressions, it's more than one function in an expression where you use the output of one function as an input of another. There's no special syntax, operators, or considerations.

For a final example of a complex expression, take the scenario of having a couple of inputs as part of your Manually trigger a flow trigger and then using that input in a formula to compute a new time. You can use the example flow that you used to test your expressions or to build a new flow that looks like this one.

Screenshot of Manually trigger a flow with Compose input.

Now select Add an input under Manually trigger a flow and add a Number. Then select Add an input again and add a Date. Your trigger should now look like this:

Screenshot of Manually trigger a flow with Number and Trigger date.

Now, in the Compose step you add an expression to add the number of days from the trigger to the date.

addDays(triggerBody()?['date'], triggerBody()?['number'])

This expression uses the Date Time function addDays and the Referencing function triggerBody.

Note

If you find yourself asking "How do I know what goes in for trigger body?" then you are thinking correctly. The secret is you can combine dynamic content into your expressions. To do so, start your expression by typing addDays() and then with your cursor between the (), select Dynamic content. Then you can choose your fields. This is a great way to reference that content while letting Power Automate do the hard work of writing the formula.

If you test your flow and enter the date 2024-02-09 and the number 2, your output is 2024-02-11T00:00:00.0000000. This is the UTC format for February 11, 2024.

Now you can find out what day of the week that is by using another Compose action with the following expression:

dayOfWeek(outputs('Compose'))

The output from the Compose action is 2024-02-11T00:00:00.000000. The output of Compose1 is 0, which represents Sunday. Sunday is 0 and each day increases by 1. Here's a screenshot of the current flow to validate what you built.

Screenshot of validate flow built with Manually trigger a flow, Compose, and Compose 2.

Now add another Compose step to check if the date selected is a Thursday. Enter the following expression:

if(equals(outputs('Compose_1'),4), 'You chose a Thursday', 'You didn't choose a Thursday')

For February 9, 2024 this will return the string "You didn't choose a Thursday."

This example is a typical build pattern for a complex expression, building piece by piece in separate steps. Now that you have all of the functions necessary worked out, add another Compose step. In the added Compose, write one large expression that does everything in one step. The expression looks like:

if(equals(dayOfWeek(addDays(triggerBody()['date'], triggerBody()['number'])),4), 'You chose a Thursday', 'You did not choose a Thursday' )

The output for February 9, 2024 will be the string "You didn't choose a Thursday." Congratulations. You wrote a complex expression by doing small steps and then putting it all together in the end.