In Microsoft Flow, you can use expressions for every action, switch, or condition and 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 you lose 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 another 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
Let’s break this down in parts to explain what is happening here:
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 of 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 calendar
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.
You should also read:
8 thoughts on “Working with expression in Microsoft Flow”
so i have a form where the i need to notify someone based on the selected reason on the form.
for example, if the submitter choose a certain value, then i need the email notification to be sent to my co-worker
but for other values then it should notify me instead
If have you created the form in Microsoft Forms, then you can just read out the answer in a condition, and proceed based on that. You could use this template to get started: https://emea.flow.microsoft.com/en-us/galleries/public/templates/19f79910d8da440981811b9f8e43ffcc/send-an-email-to-responder-when-response-submitted-in-microsoft-forms/
I read your instructions how to use first(), Last() and split. But how can I access to the splitted strings between first and last.
I ´d like to split an filename and compose a new one.
thanks for help
If the filename is manual-lab01.docx
Then we can get lab01 with the following function:
If it doesn’t help, then give me an example of the filename 😉
Realy useful post! How do you reference outputs and/or getrecords output inside an expression. E.g. I have a getrecord step to retrieve a record from CDS entity. I later want to format one of the fields and store as a variable. But I am not sure the syntax to reference the field in an expression… any help appreciated.
The new expression editor will make it a lot easier.
Other options are to use the peek code option. You create a simple compose action, select the field from the dynamic field list, and then select the 3 dots > Peek code.
would you be so kind to advise, how to access, in an expression, user input fields in manually triggered flows? I’m trying to run ticks() function on a date field provided by user, yet it doesn’t seem to work out for me. I tried many things, and the one that would make most sense – triggerBody()?[‘StartDate’] returns NULL unfortunately. Do I need to initialize new variables and pass the data there perhaps? I would appreciate your help a lot. Thanks in advance!
It seems like a bug in Flow: https://powerusers.microsoft.com/t5/General-Flow-Discussion/Using-a-Datepicker-Date-Value-as-Flow-Input-doesn-t-work/m-p/317435#M26933
All the date-time functions in PowerApps require a timestamp, but the date input field returns a string (just “01-12-2019”). That’s isn’t working. This doesn’t work either: