This is another requirement which is quite common but may be difficult for citizen developer. After I wrote the blog post on how to find unique elements in an array, I received a query on how to perform the following operations.
- Intersection of two arrays
- Union of two arrays.
- Compare array objects based on array item property
So let’s start with an example here. And let’s start with a simple arrays. We are going to explore complex arrays post that.
Array 1
["Tom", "Hardy", "Harry", "John"]
Array 2
["Tom", "Maria", "Harry", "Campbell"]
We need to compare these two arrays and find the unique elements for each array. Also we find the elements which are common to both the arrays. Let’s get started.
For this demo, I have used a manual trigger flow. I declared couple of Array variables.
The next I declare an array variable to hold records which are common to both. We are looking at intersection here. For this, there is dedicated expression in Power Automate.
intersection(variables('Array1'),variables('Array2'))
Below is the output when I run the flow.
Similarly we can have a union of arrays.
union(variables('Array1'),variables('Array2'))
So we are good with simple arrays. How about complex arrays where each item of an array is essentially an object having their properties.
In this case, the union and intersection shall compare all the properties of an array item. For example – below is a result of intersection of these two arrays.
The next question comes, what if you want to compare based on specific property of each item of the array or compare dissimilar arrays. Well, in that case there is no inbuilt function. You would need to loop through each item and compare each array item of one array to another.
Hope this helped!
Debajit Dutta
Business Solutions MVP
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.