“I’ve got two arrays with various user information, can I use Power Automate to merge the arrays into one array with all the information?”
If you’ve got all the information you need in a single data source, you’ll get an array of objects with everything. But what if it’s not in a single data source? If you’ve got multiple data sources, e.g. multiple SharePoint lists or Excel files? Each of them might contain a specific piece of the information you need.
But that’ll give you multiple arrays, and working with multiple arrays is not the easiest thing to do. Could it be easier to turn it all into a single array?
Get all the arrays
The prerequisite for the solution is a shared value. If you’ve got two or more arrays with values you must know which values belong to each other.
Let’s use the ‘Name’ property as the connector, but it can be any property you’ve got.
First array
[
{
"Name": "Tom",
"Country": "CZ"
},
{
"Name": "Paul",
"Country": "UK"
}
]
Second array
[
{
"Name": "Tom",
"Age": "younger than Paul"
},
{
"Name": "Paul",
"Age": "older than Tom"
}
]
Turn them into a single array
Since the result should be a single array, the next step is to initialise an array variable. You’ll use it to store the new, merged array. Let’s call it ‘var_finalArray’.
Merging two arrays is very similar to comparing two arrays. It takes a loop to process all the entries in the first array and a ‘Filter array’ action to find the corresponding entry in the second array. Since here you’re working with two custom arrays, you must select the values to compare directly with an expression.
There’re two ways to select them:
item()?['property']
or
items('loop_name')?['property']
If you use item()?[‘property’] = item()?['Name']
you’re referencing the input of the action, on the screenshot below it’s the input of the ‘Filter array’. But to access the currently processed item from the loop you must use items(‘loop_name’)?[‘property’] = items('Apply_to_each')?['Name']
.
Make sure that you use the right expression to reference the right array!
The filtered result should be just a single object from the second array, connected to the currently processed one. As such you can use its values to build the new, merged array.
Again, you must be careful to use the right reference! If it’s a value from the first array (the one you use in the ‘Apply to each’), reference the values by:
item()?['property']
If it’s from the second array (the filtered one), you must reference it as the output from the ‘Filter array’. And since there should be only a single result, use the first(…) expression to get the sole result:
first(body('Filter_array'))?['property']
At the end of the loop you’ll have the merged array in the variable.
Summary
You can use Power Automate to turn two arrays into one as long as there’s a shared value connecting the arrays. Once you have such value you can loop through the first array, look for the corresponding entry in the second array, and store the values in a variable. And if there’re more than two arrays you can merge the arrays one by one while repeating the process.
The solution above might get slow with bigger data sets, if your array has hundreds or thousands of items check Paul’s solution.
That’s impressive!
Hello Tom.
I know Paul’s solution is faster for larger data sets but it you want to make your solution more efficient, you can use a compose instead of the array variable.
Compose in a array has the advantage of not messing up if the concurrency is turned on for the Apply to each loop.
And it can be accessed outside of the loop as an array.
Hello Adediran,
thank you for the comment, you’re right, ‘Compose’ could make many of the flows here even faster.
Tom,
Was struggling with this for a full day until I saw your post. Very nice!