Skip to content

Let's POWER Automate

From no-code to low-code

Menu
  • Expressions
  • Filters
  • General
  • Problems
  • Triggers
  • Application specific solutions
    • Excel
    • Forms
    • Planner
    • Outlook
    • SharePoint
    • Teams
  • Resources
Menu

Turn two arrays into one through a shared value with Power Automate

Posted on July 6, 2022July 12, 2022 by Tom

“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!

Power Automate two arrays into one

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']
Power Automate two arrays into one

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.


Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

1 thought on “Turn two arrays into one through a shared value with Power Automate”

  1. Dan says:
    August 3, 2022 at 9:27 am

    That’s impressive!

    Reply

Leave a Reply Cancel reply

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

Now available:

The Ultimate Power Automate expressions cheat sheet
Equip yourself with the tool to translate your thoughts into Power Automate expressions!

Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

Hello and welcome!

My name is Tom and I'm a business process automation consultant and Microsoft MVP living in the Czech Republic. I’ve been working with Microsoft technologies for almost 10 years, currently using mainly Power Automate, SharePoint, Teams, and the other M365 tools.

I believe that everyone can automate part of their work with the Power Automate platform. You can achieve a lot by "clicking" the flows in the designer, but you can achieve much more if you add a bit of coding knowledge. And that's what this blog is about.

To make the step from no-code Power Automate flows to low-code flows: using basic coding knowledge to build more complex yet more efficient flows to automate more of your daily tasks.

  • Check if email already exists in Outlook contacts (Power Automate)August 7, 2022
  • Set permissions for a SharePoint list or library with Power AutomateAugust 3, 2022
  • Handle deleted files in synchronised SP library in Power AutomateJuly 31, 2022
  • Create item with lookup column in a dynamic list (Power Automate)July 27, 2022
  • How to move list between SharePoint tenants with Power AutomateJuly 24, 2022

Power Automate blogs worth visiting

Damien Bird
Dennis (Expiscornovus)
Paul Murana

© 2022 Let's POWER Automate | Powered by Superbs Personal Blog theme