Working with expression in Microsoft Flow

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.

Microsoft Flow Expression functions

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:

Microsoft Flow function 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:

variables('var1')

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:

concat(triggerBody()?['Subject'],triggerBody()?['From'])

Microsoft Flow triggerbody

If you want to access a property from another step, let’s say the Get Email step, then you write:

body('Get_Email')?]['From']

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.

Manipulating strings

Splitting an email address

A few functions that you would use a lot is the first, last, and split function. If we want to get the company name from an email address (john@contoso.com) you could write the following expression:
first(split(last(split(triggerBody()?['From'],'@')),'.'))

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.

split(triggerBody()?['From'],'@')

Next, we want to split the last part, contoso.com, on the dot.

split(last(split(triggerBody()?['From'],'@')),'.')

Finally, we only need the first part of the last split:

first(split(last(split(triggerBody()?['From'],'@')),'.'))

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:

last(split(body('Get_Attachment')['name']),'.')

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:

formatDateTime(adddays(utcnow(), 1),'yyyy-MM-ddT00:00:00Z')

And the end date:

formatDateTime(adddays(utcnow(), 6),'yyyy-MM-ddT23:59:59Z')

So if we run the flow every Sunday, we get all the events from Monday until Friday midnight.

Conclusion

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”

  1. 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

  2. Hi,
    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
    Albert

    • If the filename is manual-lab01.docx

      Then we can get lab01 with the following function:
      last(split(first(split(triggerBody()?['filename'],'.')),'-'))

      If it doesn’t help, then give me an example of the filename 😉

  3. 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.

  4. Hi,
    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!

Leave a Comment

0 Shares
Tweet
Pin
Share
Share