DEV Community

Barret Blake
Barret Blake

Posted on • Originally published at barretblake.dev on

Power Automate – Expression Functions

While it’s fantastic to be able to use Power Automate to connect to hundreds of different systems to move data from place to place, one of the most powerful features of Power Automate are the functions you can use in expressions to manipulate data.

Over the coming weeks, each friday I’ll take a deeper dive into various functions and how they work. But for this first post, I’ll provide a high level overview of expression functions and their use in Power Automate.

One thing I would note before diving in to the overview is that most of the information about using expression functions in Power Automate also applies to Azure Logic Apps as both systems derive their use and definition of functions from the Workflow Definition Language.

Using Function Expressions

In Power Automate, for pretty much any data element in a trigger or function, you have the option to either select a “static” value provided by a previous step, or you can insert an expression. These expressions can consist of one more functions as well as references to data values from previous actions or the trigger.

The interface to add an expression is quite easy to access. Just click in any data element box for any trigger or action. If you’re browser window is wide enough, you should see a pop-up appear to the right of the box. If it doesn’t, you should also see a text button below the data box at the right that says “Add dynamic content”. You can click this box to get the popup to appear.

02-FunctionOverview-ExpressionDialog
02-FunctionOverview-ExpressionDialog

In the dialog box, you’ll see at the top a text button that says “Expression”. In order to add an expression function, click that. You’ll see the box change to from a list of data elements to a list of functions.

02-FunctionOverview-Expression-FunctionList
02-FunctionOverview-Expression-FunctionList

The inital list is a selection of the most common functions from each category. You can either select from the listed functions, or if you know the function you want you can just start typing. There is an autocomplete feature to help you find the function you’re looking for as you type.

02-FunctionOverview-Expression-Intellisense
02-FunctionOverview-Expression-Intellisense

At this point, I’ll bring to your attention the biggest problem with the dialog box. It doesn’t get any bigger. If your function is bigger than the space you see on the screen, it can be really difficult to make sure you have all your parentheses and commas in the right place. If your expression is going to be larger, you may find it easier to write out your function in a separate text editor and then copy/paste the text into the box to validate and test it. This is one of the things I most wish Microsoft would address with the Power Automate design interface.

The other main issue is that if there is a bug in your expression function code, it’s really good at spotting that and telling you. It’s not so good at identifying exactly where and what the error actually is. And let me tell you, spotting a misplaced comma or parenthesis somewhere in a large expression function can sometimes be an intensely painful experience.

Take, for instance, the following function:

outputs('List_all_RSS_feed_items')?['body'][rand(0,length(outputs('List_all_RSS_feed_items')?['body']))]
Enter fullscreen mode Exit fullscreen mode

It’s a fairly straightforward concept. We’re using a rand (random) function to grab a random element from an array of RSS feed items. However, due to the manner in which Power Automate references data elements from actions & triggers, you’re left with a pleathora of brackets, braces and parentheses. Get a single one out of place and you’ll get an invalid expression error. But it won’t tell you which one or where in the expression it’s wrong.

So, I would love to see a major effort put into improving the expression function editor. Even if they just stole the editor from Azure Data Factory that would be a massive improvement.

The biggest thing to remember is to make sure you’re on the Expression tab and not the Dynamic content tab. If you are, then even if you type the expression function in perfectly, it won’t work. It will treat it like plain text and not as an expression.

Categories

The functions are broken down into various categories, which I will highlight here.

  • String functions – PA includes a fairly comprehensive list of string functions. The one function I wish they would as is the ability to update the value of a string using the original string as part of the update. (i.e. you can’t do something like stringA = concat(stringA, stringB) ). Instead you have to use another string variable ( i.e. stringC = concat(stringA, stringB) and then stringA = stringC ).
  • Collection functions – PA does pretty well when working with arrays with a number of ways to manipulate and work with a collection. The one trick to remember is that any time you’re using a loop to iterate an array and you modify that array, the loop will continue with the updated array. If you’re not careful you can find yourself either in an infinite loop, or a prematurely ending one.
  • Logical comparison functions – The standard set of comparison functions are here, with each returning a boolean result. It can sometimes be tricky to remember that you have to use the text functions to do comparisons ( i.e. use equals(valueA,valueB) and not valueA == valueB ). For those of us used to JavaScript or C#, this concept can be difficult to remember at times.
  • Conversion functions – There is a complete set of conversion functions. Not just the basic ones like converting a string to a float, but things like converting base64 to binary or encoding/decoding URI segments. This comes in really useful when working with images or files as some connectors require such data as base64 and some require it as binary, so that conversion becomes a common one. Some conversions are implicitly supported, but I’ve found when it’s important, always use an explicit conversion to remove any doubt.
  • Math functions – This is one of the weaker areas. The list of supported math functions is quite small. Even some of the most basic functions you would expect like powers, sin, cos, tan, etc are all missing. And they only support two element functions. For instance, you can’t do something like: add(1,2,3,4,5,6,7) Instead, you have to do: add(1, add(2, add(3, add(4, add(5, add(6,7)))))
  • Date and time functions – While there are quite a few date & time functions for Power Automate, there isn’t a date variable base type in PA. This means you have to store a datetime as a string or number and constantly do conversion functions to manipulate it. Another weak area for Power Automate.
  • Workflow functions – This is naturally a strong area for Power Automate. There are all kinds of functions for manipulating and working with it’s various pieces
  • Uri parsing functions – There’s a decent set of functions for working with the various portions of a URL, including any querystring or port values
  • JSON & XML manipulation functions – PA works very well with JSON, giving you lots of support for creating, manipulating and readying JSON. XML? Not so much. There is support for xpath queries, but creating XML tends to be a nightmare

Let the functions flow

Expression functions are easy to use, so long as you keep a couple of the caveats in mind. Look for posts each Friday that will do a more in depth view on the various functions, along with examples for each.

The post Power Automate – Expression Functions first appeared on Barret Codes.

Top comments (0)