“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.
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.