Why?
In one of my previous posts (Power Automate: combine values from an Array without an Apply to each » Knowhere365), we saw that we can combine values from an array without an Apply to each loop. With the same type of approach, we can also compare items in two different arrays. Should we want to determine if they have common items without using too much actions.
I have seen quite some Flows in the last few years where Power Users were using an Apply to each loop to compare arrays. With conditions and variables in the loop, this is definitely one way to achieve the comparison. However these loops can have a negative impact on the performance of the Flow. It cal also consume the limited amount of API calls per 24 hours that an account has available.
What?
This post will show how we can check in two different arrays if they have common items fast and efficient.
How?
I am again using the SharePoint list with a People Picker column named Users that allows multiple selections:
Please see Power Automate: combine values from an Array without an Apply to each » Knowhere365 for more information on this list and its setup.
In this example, we want to compare these two items to determine if any of the selected users of Item 2 are present in Item 1. We will combine the already explained Select action with the Intersection function to achieve this.
1) Add a Select action in which we create a simplified array with only the output we need. We use the output from the Get item action to select properties from:
If the arrays are exactly the same, we do not need the Select actions. I am adding this to clarify the concept but also to show a working approach when the arrays are (partially) different. Simplifying the arrays to only the properties we want to compare, can also impact the performance of the Flow positively.
2) With the simplified arrays we can use the output of the Select actions to compare. This way we can check if any of the Users selected in Item 2 are present in Item 1. Just add a simple Condition action that will check if there are any “intersections” between the arrays:
The expression on the left part of the condition which does not create an Apply to each loop automatically:
length(intersection(body('Select_-_UserNames'),body('Select_-_UserNames2')))
Again: in this example we can also compare the output of the Get items action directly. This can be done if the compared arrays are exactly the same. The expression on the left part of the condition would be:
length(intersection(outputs('Get_item_-_2ofListA')?['body/Users'],outputs('Get_item_-_1ofListA')?['body/Users']))
Using the Condition action, we can determine what should happen if there are intersecting Users (true) or not (false). 💪😎👍