3 Ways to Append to an Array in Power Automate

Do you need to append items to an array variable in Power Automate?

Perhaps you have an array of email addresses and need to add new addresses during the flow. This means you will need to append to your original array.

This is very useful when you need to combine data from multiple sources into a single array.

This can be achieved with a built-in action as well as using some expressions. This post will show you how to add items to your arrays in Power Automate.

Append to an Array with the Append to Array Action

This will use a built-in action to append to an array.

Initialize an Array Variable

To start, you need to create an array variable using the Initialize variable action.

  1. Add the Initialize variable action to your flow.
  2. Choose a Name for your variable.
  3. Set its type to Array.
  4. Add the initial Value for your array either as a value or dynamic content.

Now you will be able to use the Append to array variable action in your flow.

Append Single Item to Array Variable

Once the array variable is initialized, you can append items using the Append to array variable action.

  1. Add the Append to array variable action to your flow.
  2. Select your array variable from the Name dropdown.
  3. Provide the Value you want to append.

This action is designed to append single items, such as strings, numbers, or objects, to the existing array.

Unfortunately, you won’t be able to append an array of items. You will need to use an Apply to each loop in order to append all items from an array.

Append Array to Array Variable

If you want to append an entire array rather than a single item, you can use a loop in the flow.

  1. dd an Apply to each loop action.
  2. Set the input to the array you want to append. In this example, it is dynamic content referencing an array inside a compose action.
  3. Inside the loop add the Append to array variable action.
  4. Select the array to append from the Name dropdown.
  5. Add the items('Apply_to_each') expression to the Value input. This references the current item of the array from the loop.

This will loop through your array and append each item to the array variable.

By following these steps, you will be able to effectively append either single items or arrays of items to an array variable in via the Append to array variable action.

Append an Array to an Array with the Union Function

The union function will return all the items from multiple arrays.

You can use the union function to append values to an array while ensuring that unique values are maintained. This is especially helpful when working with multiple columns or datasets that may have overlapping data.

union(collection1, collection2, ...)

Where collection1 is the first array of items you want to append to and collection2 is the array of items you want to append. You can add more collections by separating them with commas.

union(outputs('Array1'), outputs('Array2'))

For example, the above expression will append the array in the Array1 compose action with the array in the Array2 compose action.

📝 Note: This will remove any duplicate values. For example if Array1 is ["A","B","B"] and Array2 is ["B","C"], this will result in ["A","B","C"] and not ["A","B","B","B","C"].

Append an Array to an Array with the Join, Split, and Concat Functions

You can combine the join, split, and concat functions to append one array to another.

The main idea will be as follows.

  1. Convert the two arrays into strings separated by a delimiter using the join function.
  2. Concatenate these two strings together separated by the same delimiter in step 1 by using the concat function.
  3. Split the string into an array based on the delimiter by using the split function.

This will result in a new array with all the elements from both arrays.

This will allow you to append all items from an array to another array. Unlike the union function, this will retain any duplicate items from the arrays.

The Join Function Syntax

join(array, delimiter)

The join function will join all the items in your array into a single delimited string.

  • array is a required parameter that represents the array of strings to be joined.
  • delimiter is also a required parameter that represents the string to be used as a separator between the joined items from the array.

In this use case, you will need to use a delimiter that is unlikely to be contained in any of the array items of your data.

For example, ||| would be unlikely to appear in any of the array data and will therefore be used as the delimiter.

The Concat Function Syntax

concat(text1, text2, ...)

The concat function will concatenate items into a single string output, text1 through textN are parameters that represent the text strings to be concatenated.

The Split Function Syntax

split(text, delimiter)

The split function will split the text into an array based on a delimiter value.

  • text is a required parameter that represents the text string to be split.
  • delimiter is also a required parameter that represents the character or characters used to split the string.

The function returns an array of strings.

Expression to Append to an Array

split(
	concat(
		join(outputs('Array1'),'|||'),
		'|||',
		join(outputs('Array2'),'|||')
	),
	'|||'
)

The join function is used twice to join two arrays Array1 and Array2 with the delimiter |||.

The concat function is then used to concatenate the two joined arrays with the delimiter ||| in between.

Finally, the split function is used to split the resulting concatenated string into an array of strings using the delimiter |||.

Overall, this expression is used to combine the elements of two arrays into a single array by joining the arrays with a delimiter, concatenating the joined arrays with another delimiter, and then splitting the resulting string into an array of strings.

⚠️ Warning: Make sure the delimiter used ||| in this expression doesn’t appear in the array data. Otherwise, the split function will not work as intended.

Conclusions

Appending to an array is a very useful process when combining data sources in Power Automate.

Utilize the Append to array variable action to easily add new items to your existing array. You can also use this action if you want to add multiple items, but this will require an Apply to each action.

There are various functions, such as union, join, and split, which can help you manipulate and manage your arrays effectively.

The union function is useful for combining two or more arrays, but the results will remove any duplicated items in the arrays.

The join, split, and concat functions can be combined for a similar effect as the union function but without removing duplicates.

I hope this helps you to append data to your arrays within a flow. Let me know in the comments!

About the Author

John MacDougall

John MacDougall

John is a Microsoft MVP and freelance consultant and trainer specializing in Excel, Power BI, Power Automate, Power Apps and SharePoint. You can find other interesting articles from John on his blog or YouTube channel.

Related Articles

Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Get the Latest Tech Tips

Write For Us

Are you a tech enthusiast with a talent for writing great content? Come write for us!

Follow Us

Follow us on social media to stay up to date with the latest in tech!