Use Logic Apps Filter Array to reduce actions in For-Each

In my current project we had an integration which should pick up files based on a name pattern. When we tested the integration in our test subscription it looked just fine, some files matched and some did not. After a few weeks in production we noticed an interesting thing. A lot of actions were run and cost was much higher than normal integrations and in test. When we looked into the reason why, we found that there were a lot more files and folders (with files in them) that did not match. This led to a lot of loops (For-Each action) with the condition to see if the file matched. Our integration did the job but at a high cost.

Logic Apps Filter Array, For-Each Condition Pattern

After some research we found a way to optimize the workflow. Logic Apps Filter Array, or Query Action which is the name in the documentation, helped us to reduce the number of actions. Just put the Filter Array after the List files in folder action and do the For each on the output from Filter array. When doing this you can also skip the Condition action within the For each.

Logic Apps Filter Array For-Each Pattern

Logic Apps Filter array before For each is a nice little pattern which reduces the number of actions, especially if you have a lot of items that do not match the filter.

One thing we miss with the Query action is that you cannot build advanced filters in the same way as you now can do with the Conditon action. Of course you can do it in codeview or even advanced mode. Maybe it is just a matter of time before we get the GUI part as in Condition.

More information about the Query Action is found here.