New Function Expressions in Power Automate Flow

By | October 19, 2022

Introduction

Power Automate has been a game changer and a welcomed addition to the suite of tools available for Dynamics 365. This low-code tool has eased the lives of citizen developers and users, who can build and achieve their requirements themselves by simply creating a Power Automate Flow. However, there are still a few functionalities to achieve where there’s a need to write complex logic, and with the recent feature introduction, Microsoft has addressed those by easing them out as well.

In this blog, we will see how the new Function Expressions have simplified the designing process of rather complex tasks and also dive into the new Expression editor.

Function expressions:

1. Sort(): It is used to sort items in ascending order. Using the mentioned function you can sort basic arrays (like strings and numbers) and can sort the array of objects as well.

  • Sorting a basic numeric array

We will pass a numeric array [1,4,2,0,5,3] and after using sort() we will get the following output [0,1,2,3,4,5]

Syntax: sort(<Array>)

E.g. sort(outputs(array))

Below is an example of how you can use the same.

Power Automate flow

  • Sorting an array of objects

Similar to the above scenario if we want to sort an array of objects by any particular attribute, then in the sort() we can pass the second parameter as the name of the attribute that you need to sort on. As shown below, we will sort an array of objects by property ‘name’.

Syntax: sort(<Array of object>,’attribute’)

E.g. sort(arrayofObj,’name’)

Power Automate flow

Below is the output to sort an array of objects based on the name parameter:

Power Automate flow

2. Reverse(): It is used to reverse the sorting order of items. And unlike the sort() expression, the reverse() expression accepts only a single parameter.

Syntax: reverse(<Array>)

Thus using the reverse expression we can reverse the orders as below:

Scenario 1: When we pass basic array as [1,4,2,0,5,3] and after using reverse() we will get the following output [3,5,0,2,4,1]

Syntax: reverse(<Array>)

E.g. reverse(outputs(array))

Power Automate flow

Scenario 2: When we want to sort a basic array [1,4,2,0,5,3] in descending order, we will have to use the sort() expression inside the reverse() expression. And this will give us the output as [5,4,3,2,1,0]

Syntax: reverse(sort(<Array>))

E.g. reverse(sort(outputs(array)))

Power Automate flow

Scenario 3: When we want to sort an array of objects by any particular attribute in descending order then, similar to Scenario 2 we will have to use the sort() expression inside of the reverse() expression as below.

Syntax: reverse(sort(<Array of object>,’attribute’))

E.g. reverse(sort(arrayofObj,’name’))

Power Automate flow

3. Chunk(): Using this expression we can either split a string or an array into chunks.

Scenario 1: If we pass a string Alina to the expression and set the number of chunks as 2 we will get the output as [“Al”,”in”, “a”].

Syntax: chunk(<string>,number of chunks to split into)

E.g. chunk(output(‘Alina’),2)

Power Automate flow

Scenario 2: In the below example, we have passed an array of objects and set the number of chunks as 2.

Syntax: chunk(<ArrayofObj>, number of chunks to split into)

E.g. chunk(output(‘arrayofObj’),2)

Power Automate flow

As we have mentioned the number of chunks as 2, the chuck function divides the object into two equal parts and creates two new arrays.

Power Automate flow

Below are a few more methods released in PowerAutomate that can be used while development.

  • isInt(): This expression is used to verify if the input passed is of type integer or not. And will return a Boolean value based on the same.
  • isFloat(): This expression is used to verify if the input passed is a floating-point number or not. And will return a Boolean value based on the same.
  • dateDifference(): This expression will return the difference in dates in a timestamp format.
  • formatDateTime(): Using this expression we can format the dates in the desired format and also as per the locale specified.
  • parseDateTime(): If the string input passed to this expression consists of a timestamp that gets returned in the timestamp format.
  • nthIndexOf(): Using this expression we can find the index of the defined occurrence of the mentioned value in the provided string. For example, We pass the string as ‘123456789123456789’ in which we want to find the index of the 2nd occurrence of the number 1.

Syntax: nthIndexOf(<Input String>, <Value to be found>,<Occurrence to be searched>)

Eg. nthIndexOf(‘123456789123456789′,’1’,2)

  • Slice(): Using this expression we can return a string value between the start and end position defined from the input provided.

Below is an example using all the mentioned methods.

Power Automate flow

Outputs:

Power Automate flow

Power Automate flow

New expression editor:

Earlier to define any expression that needed dynamic values, we used to switch between the expression and dynamic content continuously which was quite hectic work.

Now, with the release of the new experimental feature, Microsoft has given a new enhanced UI to act simply and easily without switching back and forth.

Below is a screenshot of the old UI.

Power Automate flow

And here’s the new UI.

Power Automate flow

Kindly follow the below steps to configure this feature.

Step 1: Navigate to https://make.powerautomate.com/ -> Select your CRM environment -> Click on Settings

Step 2: Click on View all Power Automate Settings

Power Automate flow

Step 3: Set the Experimental features toggle button to ON and Save

Power Automate flow

After changing the setting we can see a better UI where we get the expression and dynamic content in the same experience. Even a list of functions link is provided that will navigate to the online Microsoft documentation.

Power Automate flow

Conclusion

Thus in this blog, we experienced the new editor and explored the new Function Expressions.

Microsoft Power Platform