Introduction
In Power Automate, to get the common elements in an array, we coupled functions for array or object operations.
- Union: It returns an array with unique values from both the arrays
- Intersection: it returns an array which are common in both the arrays.
If we want to get non-common elements from both arrays, there is no direct function available for that. However, this can be achieved using both these operations and Filter Array action. Let's see this in action.
The credit goes to Markus Schiller, for demonstrating this requirement in three different ways. Please refer to the link in the references section.
Steps
Step 1
Login to the Power Automate Portal (https://make.powerautomate.com) with the organization account that has subscription to Power Automate.
Step 2
From the home screen, click on instant cloud flow. Enter the name as per your choice and click ‘Create’.
Step 3
Initialize two array variables as shown in the below screen capture. For simplicity, I have initialized one array with three values and other with four values.
Array A |
Microsoft
Amazon
Facebook |
Array B |
Microsoft
Oracle
Facebook
Netflix |
Step 4
Define the intersection of the arrays using the expression:
intersection(variables('ArrayA'),variables('ArrayB'))
Step 5
Now define another compose action with function union. It has same syntax as that of the intersection.
union(variables('ArrayA'),variables('ArrayB'))
Step 6
Save and run the flow, and observe the output. For intersection, you should see the common elements from both arrays. In this case, the common values are Microsoft and Facebook.
For union, the output is single array with unique values from both the arrays. In other words, it gets distinct values from two arrays.
Step 7: Now from the Union array outputs we can use the Filter action and get the non-common values from both the arrays. To add a filter array action for the "From" value, select the "Outputs from the Union of Arrays" action.
Step 8
Now select ‘Edit in advanced mode’ for the filter condition and replace the existing query with following one.
@or(
not(contains(variables('ArrayA'),item())),
not(contains(variables('ArrayB'),item())))
The query filter here is if the current item from the arrays (from the union of arrays) not containing in “ArrayA” or in “ArrayB." Filter those items in an array. Thus output contains non-common values.
Step 9
Save and run the flow.
In the OUTPUTS section, you should see that we are getting the non-common values.
Conclusion
In this article we have seen how to get non-common elements from two different arrays in Power Automate. You can refer to the attached flow with the same operations in sequence as defined in this article.
References