Remove Duplicates From Arrays in Power Automate — 3 Approaches
In this series: Power Automate gotchas
Power Automate still does not have a built-in "Remove Duplicates" action. Scalars are one expression. Objects need a key you define, a rule for which duplicate wins, and a decision about null. Get any of those wrong and the flow "dedupes" while leaving the rows you cared about, or collapsing rows you needed.
Approach 1: Union with itself, for scalars only
The union() function returns distinct values. Passing the same array twice is the usual trick:
@union(variables('myArray'), variables('myArray'))
Input: ["apple", "banana", "apple", "cherry", "banana"]
Output: ["apple", "banana", "cherry"]
It compares JSON values. Two identical strings collapse. Case is significant: "Apple" and "apple" both stay. It does not dedupe objects by a business key. Two records with the same email and different names are different JSON values, so both remain.
If the array might be null, wrap it: @union(coalesce(variables('myArray'), createArray()), coalesce(variables('myArray'), createArray())). union on null fails the run.
Stop here if the array is strings, numbers, or booleans. The rest of this post is what to do when it is not.
Approach 2: Unique keys, then pick a winner
When you have objects and a business key (email, order number, Dataverse id):
- Select — project the key into a simple array. Map:
@item()?['email'] - Compose UniqueKeys —
@union(body('Select'), body('Select')) - Apply to each on those keys. Inside, Filter array the original objects, then take one.
The filter:
@equals(item()?['email'], items('Apply_to_each'))
Then Compose @first(body('Filter_array')) and Append to array variable. One object per key, first occurrence in the original order.
Cost: one Filter per unique key. Fine for tens or low hundreds. Painful for thousands — see Approach 3.
The indexOf trick you should not paste
This expression is always true and does nothing:
@equals(
indexOf(outputs('Compose_Unique_Keys'), item()?['email']),
indexOf(outputs('Compose_Unique_Keys'), item()?['email'])
)
Both sides are the same call. Equality of a value with itself is not a uniqueness test. Do not use it as a Filter on the original array. It will keep every row. The Filter belongs inside the loop over unique keys, comparing item()?['email'] to items('Apply_to_each').
Worked: null and blank keys
Start with:
[
{ "email": "ada@contoso.com", "name": "Ada" },
{ "email": null, "name": "No email A" },
{ "email": "", "name": "Blank A" },
{ "email": null, "name": "No email B" },
{ "email": "", "name": "Blank B" },
{ "email": "ada@contoso.com", "name": "Ada Duplicate" }
]
union on those projected keys keeps ada@contoso.com, null, and "" as three different keys. Null is not the empty string. Two null rows collapse to one key; two "" rows collapse to another.
If blank email means "not comparable," filter those rows out before the Select:
@and(
not(equals(item()?['email'], null)),
not(equals(item()?['email'], ''))
)
Then run the unique-key loop only on rows that have a real key. Keep the excluded rows aside if you still need to create them. Collapsing every missing email into one "duplicate" is how you drop people. If blank does mean "the same unknown person," name the action First_of_blank_email and accept that No email B disappears.
Worked: compound keys
"Unique" across email and sku is not two unions. Concatenate in the Select:
@concat(coalesce(item()?['email'], ''), '|', coalesce(item()?['sku'], ''))
Then union that string array. Filter with:
@equals(
concat(coalesce(item()?['email'], ''), '|', coalesce(item()?['sku'], '')),
items('Apply_to_each')
)
Pick a delimiter that cannot appear in either field. | is fine for emails and skus; it is not fine in free text. Same email, different sku: two keys. Same email and sku, different qty: one key. Which qty you keep is the next section. Do not union emails and union skus separately.
Worked: last-write-wins
First match is the default because first(body('Filter_array')) is easy. It is wrong when the array is an event stream and a later object is the current state.
[
{ "email": "ada@contoso.com", "status": "draft", "modifiedOn": "2026-01-01T09:00:00Z" },
{ "email": "ada@contoso.com", "status": "submitted", "modifiedOn": "2026-01-03T09:00:00Z" },
{ "email": "bob@contoso.com", "status": "draft", "modifiedOn": "2026-01-02T09:00:00Z" }
]
first(...) keeps Ada as draft. last(...) keeps Ada as submitted. Name the Compose action after the rule (Last_for_email).
last is last in array order, not last in time. If the source is unordered, sort by modifiedOn once above the loop, then take last — or sort descending and keep first. Missing timestamps make last a position lottery; filter those rows out. Do not sort inside the Apply to each. Repeating sort per key is how a hundred-row flow hits concurrency limits.
Approach 3: Leave the flow
XPath-over-XML appears in a lot of older posts:
@xpath(xml(json(concat('{"root":{"item":', string(variables('myArray')), '}}'))), '//item[not(. = preceding-sibling::item)]')
It is brittle. Object fields that are missing, arrays nested inside items, and values that need XML escaping will fail xml(...). I do not use this in new flows. If union cannot see the key and Apply to each is too slow, the data should be unique before Power Automate sees it.
Prefer a Dataverse alternate key and an upsert, a distinct in the source query, or OData $filter so the flow never loads duplicates. A child flow that chunks, or an Azure Function, if this is a standing integration. Do not add a fourth Apply to each to "make union work on objects."
Which should you use?
| Scenario | Approach |
|---|---|
| Simple string/number arrays | union(array, array) |
| Object arrays, one key, hundreds of rows, first wins | Select keys + union + first |
| Same, but the latest row is the truth | Sort, then last (or descending + first) |
| Unique across two fields | Concatenate in the Select, then the same loop |
| Null or blank keys mean "do not compare" | Filter them out before Select |
| Thousands of objects | Deduplicate in Dataverse/SQL, or leave the flow |
When not to, and if the output still has duplicates
Skip this pattern when the duplicates are a source bug you will see again tomorrow — fixing them in the flow hides it. Skip it when two rows with the same email are two people. Skip it when you need a field-level merge (phone from A, status from B). That is a reduce, and Power Automate is a poor reduce engine.
- Confirm you are looking at the array after the Append loop.
- Search for
indexOf. If both sides ofequalsare the same call, that Filter is a no-op. - Inspect key types.
"123"and123do not union. Neither donulland"". Case is significant;toLowerin the Select if you need it. - If unique-key count matches output length and you still see "duplicates," the key is too narrow or too wide.
- If Apply to each has concurrency on, Append to array variable can interleave. Set concurrency to 1 on that loop.
Start with union(array, array) for scalars. For objects, project the key, union the keys, then take first or last of the filter per key, with nulls decided on purpose. Do not use the indexOf equals-itself trick. Above a few thousand items, stop doing this in the flow.
Keep reading
Perform a Changeset Request: Two Add a New Row Actions Are Two Transactions
Only Add, Update, and Delete belong in a Dataverse changeset. List rows is not in that transaction, and the next Add a new row is its own commit.
Add a New Row: The Lookup Is accounts(guid), Not the GUID
Add a new row binds lookups as entityset(guid). A raw GUID or name is not a bind.
Filter Rows: The Lookup GUID Is _parentcustomerid_value
Filter rows is Dataverse OData. Lookups filter on _logicalname_value, not the name. Filter array runs after the download, and @equals does not belong in that box.
Get a Row by ID: Missing Fails the Run, Empty List Rows Does Not
Get a row by ID fails the run when the GUID is missing. List rows with zero matches succeeds. Look up the business key and branch on empty.
When an HTTP Request Is Received: The Callback URL Is a Secret
The HTTP trigger URL carries a SAS signature. Anyone with it can start the flow. Restrict who can call, always Respond, and never treat that URL as public.
Power Automate Do Until: The Loop That Succeeds After Giving Up
A Do until that hits count or timeout still succeeds. Put Get inside the loop, add a Delay, and test the condition after the loop exits.
Newsletter
New posts, straight to your inbox
One email when a new post goes up. Double opt-in, unsubscribe anytime. Not a daily digest.
Comments
- No comments yet. Be the first.