Working with expression in Microsoft Flow
In Microsoft Flow, you can use expressions for every action, switch, or condition and to manipulate data. Using this in combination with variables allows you to do some cool and very useful things (calculating dates, manipulating strings, extracting parts of a string and a lot more).
A small tip upfront, I like to write the expression in notepad. The input field in flow is pretty small and if you start writing bigger expressions you will notice youlosee the overview. Making it harder to check if you have all the brackets in the right place.
Where to find the functions
If you click on the Expression tab you will get a list with some of the functions that are available. The functions are categorized so make sure you click on See More in each category to get see the most important functions. But there is more! Not all functions are listed in the Expression tab, for example, if you start typing sub you will see a list of additional functions that are available.
So in this post, I won’t go over each and every function, there are simply too many functions. And it’s not necessary either, because when you start using a function, you will get a nice tooltip explaining how to use it. Just type in the function and add an open bracket at the end. In the tooltip you will get the correct syntax and a description:
Variables in Microsoft Flow
Inside expression, you want to use dynamic values that you get back from other actions. For example, you have a flow that is triggered when you receive an email. Then you might want to use the sender’s company name that you extract from the email address somewhere in your flow.
Now there are 2 ways to do this. One, you create a variable and store the dynamic content init. This is the clearest way to do this, but on a more complex flow, you will have to initialize a lot of variables at the start of your flow.
Creating a variable
To create a variable you first have to initialize it at the top of your flow (before any condition).
- Add an action and search for Variables.
- Add the initialize variable flow.
- Now, this is pretty straightforward, give your variable a name and set the type.
You can leave the value empty if you don’t have the data for it yet, but you can also add dynamic content to it. Just click the value field and select the dynamic content that you want to use. If you want to set the value later, then you can use the Set variable action in your flow.
The variables can be used as dynamic content and in expression. If you want to use it in an expression, you need to use the following function:
Using dynamic content directly in an expression
The other option to use the value from a step in an expression is referencing it directly. Let go back to our reference case where we have a flow that is triggered by receiving an email. For this email we want to get a notification of the subject and sender (just because we can…).
Yes, we could use Dynamic content for this, but I like to keep the example simples. So we are going to combine the two string, subject and sender, with the concatenate function. To get an item from another step we can use different functions. The first step, the trigger, is referenced by the function triggerBody(). Other steps can be accessed with the body() function or items() inside a for each step.
So in our example, we want to get the data from the trigger:
If you want to access a property from an other step, let’s say the Get Email step, then you write:
Pay close attention to the underscore here. The step name has a space in it, you need to replace that with an underscore in your expression. Also with the trigger body, you don’t need to fill in the step name.
Splitting an email address
First, we split the email address that we get from the triggerBody (the first step) on the @ symbol. This will give use two values back.
Next, we want to split the last part, contoso.com, on the dot.
Finally, we only need the first part of the last split:
Getting the file extension
Another string manipulation you will probably use a lot is getting the file extension. This will require to split the filename on the dot and get the last part of it:
Working with Dates
Date manipulation is one the most used expressions. Getting the events for the coming week for example can be done with the following action and expression:
- Create action Get calendar view of events (v2)
- Select the calender
Now for the start time we need to enter the date in a specific format: yyyy-mm-ddT00:00:00Z. So we can use the following expression to set the start date:
And the end date:
So if we run the flow every Sunday, we get all the events from Monday until Friday midnight.
I hope this will help you get started with using expressions in your flows. Also, take a look at the Microsoft Flow documentation, they have some great starting guides to help you get started with using Microsoft Flow.