“The Power Automate designer keeps telling me that the expression is invalid but I don’t know why… can you take a look how to fix it?”
Invalid expressions are probably the most common error message you’ll see while building a flow. You create an expression, try to place it in an action, and it won’t work. All you get is the generic error message ‘The expression is invalid’. That’s it. The expression is invalid and it’s up to you to find why it’s invalid and how to fix it.
In this article I’ll show you my approach to dealing with such expressions.
Try to enter the same expression again
The fastest and often sufficient solution is to enter the expression again as the designer sometimes rejects even expressions that are completely fine. If that’s so, closing the error popup and entering the expression again helps.
Decompose the expression in a code editor
If the designer rejects the expression even on the second attempt, there’s probably something wrong, something is missing. In these cases I take the expression out of the designer and copy/paste it in a code editor. My recommendation goes to Visual Studio Code that’ll be also used for this article.
Add some colours to the expressions
Let’s take an expression from one of the comments and paste it in the Visual Studio Code:
and(not(contains(item()?['Status'], string('Complete')),less(item()?['Expected Completion'], item()?['CurrentDate']),equals(item()?['Report Sent'], string('No')))
I can see right away that one of the brackets is red, while all the remaining characters are white. That’s the first hint that there might be some problem with the brackets – not all brackets are closed.
As I don’t want to click on all the closing bracket to see the connected opening brackets I’ll switch the formatting to R language to add some colours. It’s not the language Power Automate uses but the syntax highlighting works good enough for me.
Now all the brackets have their own colour and I can easily see how they’re connected. It’ll add colours also to the other parts of the expression – expressions, strings, etc.
These colours will help you also if you forgot e.g. a quote somewhere in the expression as the colours would be off as you can see below:
If you already see the problem you can fix it, otherwise let’s continue with the deconstruction.
Visually format the expression
Even though it’s now easier to see all the brackets and expressions, everything is still squeezed in a single line. To get a better overview of the parts you can visually separate them using new lines and spaces.
Start by adding a new line before each expression – move cursor before the expression (light blue colour) and press Enter. The code editor will automatically add spaces to create kind of a tree structure – sometimes it’s helpful, in this case it’s not.
It’s a good start, but at this point you must think about it a bit more. Reformat the code manually into columns based on their dependency. If the expressions are on the same level, they should be in the same column. If there’re parameters for the expression, they should be on the next line one more column to the right.
This expression starts with AND and evaluates 3 different conditions at the same level.
Now move the closing brackets in the same column as the expression.
Looking at the expression above you should see which of the expressions is not closed, which one doesn’t have a closing bracket underneath. The same approach can be used also to identify missing comma(s). Once you split the expression into separate functions you’ll see whether they have all/the right parameters.
To finish, add the missing bracket and copy/paste the expression into your flow.
You can even keep the new lines and spaces to make the expression easier to read in the flow.
Hint: it was the not(…) expression that wasn’t closed.
While there aren’t many reasons for an invalid expression in Power Automate, it can take some effort to find the exact place and fix it. Especially once you start combining multiple expressions into a single one. But if you use some code editor, utilise its colour formatting, and visually split the expression into pieces, you should be able to find the problem and fix it.