Power Automate - Top 10 Cool Under Used Expressions
Tags: powerautomate, powerplatform, rpa, lowcode
Published: 2023-10-30 08:20:32

If you have ready any of my previous blogs you will know I have strange idea of cool 😎 but hear me out. We all know the standard Power Automate expressions like `if()`, `equal()`, `formatDateTime()`, but there are lots of expressions (according to BingChat there are 113), but how many of them do we use? 50%, 25%, less?

![count of expressions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36xgfdjgdh9jbls7axda.png)

So I wanted to share my top 10 under used expressions and why they are cool.

1. Coalesce
2. Empty
3. Mod
4. Intersection
5. JSON
6. FormatNumber
7. StartOfDay
8. UriComponent
9. Take
10. Xpath

## 1. Coalesce
[Coalesce](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#coalesce
) returns the first output with a value from your inputs. I love this when using Switch's or Conditions, normally you would have to pass key values to variables to use after the branches merge, but with coalesce you pass all the outputs and only the action triggered will be used.

```
coalesce (
    body('Populate_a_Microsoft_Word_template1')
,
    body('Populate_a_Microsoft_Word_template2')
,
    body('Populate_a_Microsoft_Word_template3')
,
    body('Populate_a_Microsoft_Word_template4')
)
```

![coalesce flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gydolayo1ourya3fwbyh.png)

## 2.Empty
How often do we check the length of an array is 0 to know if we have any data, well [Empty](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#empty) is an a lot more elgant way to check.

```
empty(outputs('Get_items')?['body/value'])
```

## 3.Mod
[Mod](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#mod) returns the remainded from diving one number by another. So why is this useful, well if you wanted to only action the even, or nth in a loop, you can use mod on condition it equals 0 

```
mod(variables('iCounter'),2)
```

## 4.Intersection
Niche one here, [Intersection](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#intersection) compares to arrays and only returns items that are in both arrays. The main time I use this is looking for changes, as if the length of the Intersection of 2 arrays match then there are no changes, but if its less then we know there are changes.
```
intersection(
    outputs('Get_items_old')?['body/value']
,
    outputs('Get_items_new')?['body/value']
)
```

## 5.JSON
[JSON](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#json) is like int, string and Boolean, but for converting to a JSON (so no need for a parseJSON). This is great for converting a string to an object when the action requires it as a object, this is normally when we want to pass dynamic data in and the connector cant get the input schema.

```
json('
    "853159301":"John",
    "853159301":"Dowe"
')
```

![json flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sq646jdqu9tp21dsj6hi.png)

## 6.FormatNumber
[FormatNumber](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#formatNumber
) allows you to set the number format for your integers, Nothing exciting but its great when you are populating files like html templates or word templates. You can ensure all numbers have the same format (you an even set to a region like in Germany where they use a comma for a decimal).

```
formatNumber(1000, '0,0.00', 'en-us')
```

## 7. StartOfDay
Another niche one, [StartOfDay](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#startOfDay) is great for when you want to do computations on dates but the value is a dateTime. Example would be to get the last 7 days of entries from a list based on the most recent items created dateTime.

```
startOfDay(outputs('Get_item')?['body/Created'])
```

## 8. UriComponent()
[UriComponent](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#uriComponent()) removes none compliant characters from your url. This is mainly used when you want to build urls with query's, particularly redirects, as the redirects query's will get mixed in with the original urls query.

```
concat(
    triggerBody()['text_2']
,
    '&env=WebView&Source='
'
    uriComponent(variables('sURLwithQuery')
)

```

## 9. Take
[Take](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#take) returns the first nth values in an array and string. For an array with sort it could be used for top 10, for strings its a more elegant way to do left instead of substring.

```
`take('1234567890',4)`
```

## 10. Xpath
[Xpath](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#xpath) is one of those swiss army knives of an expression, it can be used in ways you never thought, I often use it to sum up array keys ([How to Optimize Flows](https://dev.to/wyattdave/top-5-ways-to-optimize-your-flows-3adf)), but another cool trick is parsing HTML. Anything with a structure (JSON,HTML,etc) can be converted to XML and then use Xpath to extract information.

```
xpath(
    xml(outputs('HTML'))
,
    '/html/body/div/div/span/text()'
)
```

![xpath html flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57tb22ndbsb8hu25zezu.png)


 ---

## Honerable Mentions
As I said there are over 100 expressions and growing so I could go on all day, below are a few more that are useful (though either less useful then top 10 or more likely you already use them):

- [StartOfMonth](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#startOfMonth) great for month to date quries
- [Skip](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#skip) is like Take but removes the nth from the front of the array/string. So a little less useful and not quite the same as Right in Excel.
- [IterationIndexes](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#iterationIndexes) gives current item number in Until loops
- [Workflow](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#workflow) gives you information on the flow run, great for logging and exception handling
- [ParseDateTime](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#parseDateTime) converts string dates to dates.
- [AddProperty](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#addproperty) niche but along with [setProperty](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#setProperty) & [removeProperty](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#removeProperty) good for handling Office Script inputs/outputs.