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?
So I wanted to share my top 10 under used expressions and why they are cool.
- Coalesce
- Empty
- Mod
- Intersection
- JSON
- FormatNumber
- StartOfDay
- UriComponent
- Take
- Xpath
1. Coalesce
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')
)
2.Empty
How often do we check the length of an array is 0 to know if we have any data, well Empty is an a lot more elgant way to check.
empty(outputs('Get_items')?['body/value'])
3.Mod
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 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 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"
')
6.FormatNumber
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 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 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 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 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), 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()'
)
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 great for month to date quries
- 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 gives current item number in Until loops
- Workflow gives you information on the flow run, great for logging and exception handling
- ParseDateTime converts string dates to dates.
- AddProperty niche but along with setProperty & removeProperty good for handling Office Script inputs/outputs.
Top comments (2)
Make me laugh the use of XPath. I thought this was really unused since the years past from the last time I used as a revolutionary tech, more or less, it was on 2006 or 2007.
Great tips!!!!
love the tips @wyattdave