Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save charlesroper/879c7ae0fb26431a633fef0ded3014e9 to your computer and use it in GitHub Desktop.
Save charlesroper/879c7ae0fb26431a633fef0ded3014e9 to your computer and use it in GitHub Desktop.
Power Automate joining and coalescing.md

Power Automate joining and coalescing

A discussion with Bing AI (creative mode) 2023-04-06


ME

Using Power Automate.

This is the array: outputs('Get_user_profile_(V2)')?['body/businessPhones']

How do I check if that is empty?

If it is empty, I'd like to return an empty string

If it is not empty, I'd like to return the first element [0]


BING

I see. You can use the coalesce function to check if the array is empty and return an empty string or the first element accordingly. The expression coalesce(outputs('Get_user_profile_(V2)')?['body/businessPhones']?[0], '') will do the following:

  • It will try to get the first element of the array by using the zero index [0].
  • If the array is empty or the first element is null, it will return an empty string '' as the second argument of the coalesce function.
  • If the array is not empty and the first element is not null, it will return the first element as the value.

You can use this expression in any action that accepts a string input, such as a compose action or a set variable action.

I hope this helps you with your specific example.😊


ME

Where can I find documentation for the coalesce function?


BING

You can find documentation for the coalesce function in Power Automate from various sources online. Here are some of them:

  • Power Automate Coalesce Function Explained - zeitgeistcode.com¹: This article explains what the coalesce function does, how to use it in different scenarios, and provides some examples with screenshots.
  • Power Automate Fundamentals # 6: Usage of Coalesce Function in Power Automate²: This article shows how to use the coalesce function to check for null values in a plan object and provides a step-by-step guide with images.
  • Usage Of Coalesce Function In Power Automate³: This article gives a brief introduction to the coalesce function and shows how to use it with an example flow.

I hope these sources help you learn more about the coalesce function in Power Automate.😊

(1) Power Automate Coalesce Function Explained - zeitgeistcode.com. https://zeitgeistcode.com/power-automate-coalesce/ Accessed 06/04/2023.

(2) Power Automate Fundamentals # 6: Usage of Coalesce Function in Power …. https://powerusers.microsoft.com/t5/Power-Automate-Community-Blog/Power-Automate-Fundamentals-6-Usage-of-Coalesce-Function-in/ba-p/1228100 Accessed 06/04/2023.

(3) Usage Of Coalesce Function In Power Automate. https://www.c-sharpcorner.com/article/usage-of-coalesce-function-in-power-automate/ Accessed 06/04/2023.


ME

I would like to do the same, but instead of returning the first element [0] I would like to return the array as a joined string.

So if outputs('Get_user_profile_(V2)')?['body/businessPhones'] contains ['One', 'Two', 'Three'] I would like the output to be "One; Two; Three". If the array is empty, I'd like a blank string.


BING

I see. You can use the join function to concatenate the elements of an array with a separator. The expression join(outputs('Get_user_profile_(V2)')?['body/businessPhones'], '; ') will return a string with the elements of the array separated by ; . However, if the array is empty, the join function will fail.

To avoid this error, you can use the coalesce function to check if the array is empty and return an empty string or the joined string accordingly. The expression coalesce(join(outputs('Get_user_profile_(V2)')?['body/businessPhones'], '; '), '') will do the following:

  • It will try to join the elements of the array with ; as the separator.
  • If the array is empty or null, it will return an empty string '' as the second argument of the coalesce function.
  • If the array is not empty and not null, it will return the joined string as the value.

You can use this expression in any action that accepts a string input, such as a compose action or a set variable action.

I hope this helps you with your specific example.😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment