Merge arrays
Power Automate,  Power Platform

How to merge two arrays in Power Automate without apply to each?

Recently someone asked me in Microsoft Power Platform community , how we will merge two array without apply to each?.  First lets understand what is this?.

In flow, When we have two arrays and we want to merge it. Then we have option to append it using variable but this will apply looping effect in your flow. It means that it will iterate from first array then append values to second array. So that your variable will have one complete array.

Think about large set of data where we have 1000+ items to merge. Here apply to each will have huge performance impact on your flow.

To tackle such issue we need to use xml() & add properties() in our flow. Let’s deep dive into this concept.

Steps:

  • Go to your Power Automate Studio and click on +Create then choose “Instant Cloud Flow”. I am using this option to demonstrate this concept. You can use your flow trigger as per your requirement.
  • Then initialise two array variables- Array1 & Array2 as shown below-

Array Variables

  • Now you need to convert your Array2 into XML array with root element.  To do this, Let’s first include root tag in your second array.

Root Elements

  • So now you have second array with root element. After this we will use XML  function to convert above array into XML format. You will see following output-
<root><Array><Key>100</Key><quantity>10</quantity></Array><Array><Key>101</Key><quantity>25</quantity></Array></root>
Convert to XML
  • Now you will use addproperty() in a select action so that it will add second array element in to first array.

addProperty(item(),’quantity’,xpath(outputs(‘Compose-XML’),concat(‘//Array[Key/text()=”‘,item()[‘Key’],'”]/quantity/text()’))[0])

select

Note: Do not forget to add text() in your array element otherwise it will append content & content type rather that its value.

  • Here I am using addproperty() to add new element from second array into first array.

Output-

Merged array

Thank You!!!

Gopenly

One Comment

Leave a Reply