Power Automate provides the "union" function to combine multiple arrays into one while automatically removing duplicates. It's one of those functions that does more than its name suggests - beyond merging arrays, it's the go-to trick for deduplicating data without any loops.
The "removing duplicates" is an amazing side effect since we can use it to our advantage, but let's first take a look at how it works.
Usage
It follows a simple pattern.
- First collection (array or object)
- Second collection (array or object)
- (Optional) Additional collections
All parameters must be the same type - either all arrays or all objects. You cannot mix them.
Here's a basic example:
union(createArray(1, 2, 3), createArray(1, 2, 10, 101))
will return
[1, 2, 3, 10, 101]
The result contains all unique items from both arrays. Notice that 1 and 2 appeared in both arrays but show up only once in the result.
Deduplicating a single array
This is one of the most popular uses of the "union" function. Pass the same array twice to remove duplicates:
union(variables('MyArray'), variables('MyArray'))
Or combine with an empty array:
union(variables('MyArray'), json('[]'))
Both approaches return only the unique values from your array. It's an elegant one-liner that replaces what would otherwise require a loop.
Multiple arrays
You can merge more than two arrays at once:
union(createArray(1, 2), createArray(2, 3), createArray(3, 4))
will return
[1, 2, 3, 4]
With objects
When used with JSON objects (not arrays of objects, but actual objects), the "union" function merges the properties. If the same property name appears in multiple objects, the last one wins:
union(json('{"name":"Manuel","age":30}'), json('{"name":"Manuel","city":"Lisbon"}'))
will return
{"name":"Manuel","age":30,"city":"Lisbon"}
When there's a conflict, the value from the last object takes priority.
Edge Cases
Duplicate objects in arrays
When deduplicating arrays of objects, the "union" function compares all properties. Two objects must be completely identical to be considered duplicates. If even one property differs, both objects remain in the result.
String comparisons are case-sensitive. "manuel@example.com" and "Manuel@Example.com" are treated as different values. If you're deduplicating email addresses or names, convert them to a consistent case first using the "toLower" function before applying "union".
Object property conflicts
When merging objects (not arrays), if two objects share the same property name, the last object's value wins. This is important to remember when the order of parameters matters:
union(json('{"status":"active"}'), json('{"status":"inactive"}'))
will return
{"status":"inactive"}
The second object's "status" overwrites the first.
Empty arrays
Combining with an empty array has no effect on the result other than deduplication:
union(createArray(1, 2, 2, 3), json('[]'))
will return
[1, 2, 3]
This is actually the recommended pattern for deduplication when you don't want to pass the variable name twice.
Numbers vs strings
The "union" function treats the number 1 and the string "1" as different values. If your array contains a mix of types, the function won't deduplicate across them:
union(createArray(1, '1'), createArray(1))
will return
[1,"1"]
Keep this in mind when working with data from different sources where the same value might arrive as a number in one place and a string in another.
Using union inside a loop
A common mistake is placing the "union" function inside an "Apply to each" action, expecting it to deduplicate values as they're added. This doesn't work because each iteration only sees the current item. Build the full array first using the "Append to array variable" action, then apply "union" after the loop completes in a separate "Compose" action.
Limitations
No "combine with duplicates" option
The "union" function always removes duplicates. If you actually need to combine two arrays and keep all values including duplicates, use the "concat" function with arrays or build the combined array using the "Compose" action and "Append to array variable" action instead.
Cannot mix arrays and objects
All parameters must be the same type. Passing an array and an object together will cause an error.
Full object comparison only
Just like the "intersection" function, deduplication compares entire objects. There's no way to deduplicate based on a single property. If you need to deduplicate by a specific field (like email or ID), extract that field first with a "Select" action, deduplicate the simple array, and then reconstruct your objects.
Recommendations
Here are some things to keep in mind.
Use it as a deduplication tool
The most common and powerful use of the "union" function isn't merging two different arrays - it's cleaning up a single array. Whenever you have an array that might contain duplicates, union(yourArray, yourArray) is the simplest fix.
Order of parameters matters for objects
When merging JSON objects, the last parameter wins on property conflicts. Place the object with the "correct" or "priority" values last.
Pair with intersection for array comparison
The "union" function and the "intersection" function are complementary. Use "union" to get all unique items from both arrays, and "intersection" to get only the common items. Add the "except" function to find items in one array but not the other, and you have all three set operations covered without loops.
Always add a comment
Adding a comment will help others understand your formula. Indicate what the function is doing and why, especially if the expression is complex.
Final Thoughts
The "union" function is one of the most versatile collection functions in Power Automate. Whether you're merging arrays, deduplicating data, or combining JSON objects, it handles it all in a single expression. Just remember that it always removes duplicates and that string comparisons are case-sensitive, and you'll be in great shape.
Sources
Microsoft's "union" Function Reference
Back to the Power Automate Function Reference
Photo by Tatiana Rodriguez on Unsplash
No comments yet
Be the first to share your thoughts on this article!