“I have a Power Automate flow that mostly works but sometimes it’s creating duplicates of the items/documents and I don’t know why.”
Some problems in a flow are easy to notice, others not so much. The worst ones are problems that happen only from time to time. The flow can run for days, weeks, even months, and then it fails. Nobody touched it, but this time the flow created duplicate entries. Why now? What changed from the previous runs?
Duplicates = mostly unnecessary loops
There can be two reasons why your flow would create duplicates. Either did the flow run multiple times or it contains a loop that shouldn’t be there. If it’s running multiple times you might be dealing with the infinite trigger loop or you might need to add a trigger condition. But if it’s inside a single flow run, it’ll be most probably due to some ‘Apply to each’ loop.
Every ‘Apply to each’ loop in the flow means that everything that’s inside the loop can run multiple times. You’ve got an array as the input of the ‘Apply to each’ and the flow will repeat all the actions inside for each of the items. For example, if I have a flow as below it’ll repeat the ‘Create file’ for each item returned by the ‘Get items’ action.
If the ‘Get items’ returns 1 item, it’ll create 1 file. If it returns 5 items it’ll create 5 files and so on. Which is fine and often the goal of the flow. When the problems start is if you’ve got another ‘Apply to each’ inside the ‘Apply to each’.
This second ‘Apply to each’ is often added automatically once you try to use some dynamic content. And the reason is that the dynamic content comes from an array. For example if you’re using SharePoint multiple selection fields (choice/lookup/people picker). The flow knows from the list structure that these are multiple selection columns and as such it wants to process all the possible entries.
Remove the inside loop
If there’s only one value, it’ll repeat the action only once. But once you get in a situation when there’re multiple values it’ll repeat the action for each of them. Following the example above, if there’s only 1 value selected, it’ll create 1 file. If there’re 5 values selected it’ll create 5 files. And it’ll repeat it over and over again for each item in the first loop.
If you want to create just 1 file with all the information, you must remove the inner ‘Apply to each’. That often means to input all the selections at once as an entire array. Once you remove it you’ll remove also the potential for creation of duplicates.
Summary
You should always be careful when Power Automate starts adding ‘Apply to each’ inside another ‘Apply to each’ as that’s the most common situation for creating duplicates. It’ll work fine if the inner loop runs only once, e.g. if there’s only 1 person/choice/lookup in a multiple selection SharePoint column. But once there’re more than 1 it’ll start creating duplicates. Therefore, if your flow starts producing duplicates, the first thing to look for are loops.
I am groing crazy with copying items. It always creates 2 or more copies and I only have one apply to each. Im using a condition if a column is equally to x then copy the item. But I don’t know where I am failing. :/
Hello Moises,
my first guess is that you’ve got ‘Apply to each’ inside another ‘Apply to each’ as that’s a common problem, try to remove as many loops as possible from the flow: https://tomriha.com/stop-power-automate-flow-from-adding-apply-to-each-automatically/
Mine has the exact same issue. I also don’t have multiple Apply to Each but I still get duplicates. That Tom dude straight-up ignored your entire sentence which is unhelpful.
Hello Scott,
if it’s creating duplicates you are probably looping through too many items. Check the flow run history to see how many times did the loop run. If it’s more than you expected than something is wrong, which you can try to debug by adding a ‘Compose’ action inside the loop and store some identifier of the currently processed value, e.g. name/title/whatever will help you identify why the flow is running that way.
Thank you!!! I spent hours trying to figure out what was wrong with my flow, but spending less than 5 minutes reading this article just now solved my problem!