Microsoft Flow – 2 methods to not use Apply to Each action when only a single filtered item is expected

  • Post Category:Flow
  • Post Comments:8 Comments

In this post I will show you 2 methods when using Apply to each Flow action is not necessary. By not using them you make your Flows look cleaner, less complicated.

Please note, these methods are mostly good when only a single item is expected after filtering an array.

I created a SharePoint List with two text columns – Title and Color. Both columns are required. Title values are set to be unique (!).

When using Get Items action, an array of items is returned regardless if it’s a single item in it or not. The moment you try to use a Condition action on its result, the system will insert an Apply to Each action to go through every single possible item. Again, it’s redundant when by design a single item is returned after filtering.

In my test flow I’m using a very simple Filter Query on purpose – Title eq ‘Test 1’. In a real life situation that value you are filtering by can come from any other source and should make more sense.

The Flow without using Apply to Each action:

Method 1 uses a First function. The code for Condition action:

@equals(first(body('Get_items')?['value'])?['Color'], 'Blue')

Method 2 uses ?[0] to get the first item in the array. The code for Condition action:

@equals(body('Get_items')?['value']?[0]?['Color'], 'Blue')

Both of these expressions above are for when Edit in advanced mode is selected for Condition actions.


This Post Has 8 Comments

  1. Sherwin

    After many hours of searching the web, this got the job done!! I had to query a SharePoint list of over 70k records and Apply for each was killing me. Runtime used to go for over 4 hours now it’s down to seconds. I can’t thank you enough for this. Cheers!

    1. Pavel Bludov

      Hi Sherwin,
      This is the kind of feedback that is always great to see!

  2. ouss

    Hello, I had 500000 files I need to go trough with flow. Can I use this methode as well?
    Can’t figure out the right way to write it down. I need to filter on “Starts with” 0345,0355 etc.

    Can I use this methode as well and how do I write it down? It takes ages now to go through the apply to each…..

    1. Pavel Bludov

      Apply To Each can only go through 5,000 or 100,000 depending on your license – more
      Get Items supports pagination, Get Files doesn’t support it. However, for both you can use a workaround explained by a Microsoft Employee here –
      Filter Query should support StartsWith – more here
      Getting back to my post, if you know the position of an element, you can always address it through something like this – body(‘Get_items’)?[‘value’]?[3000]?[‘Color’]

  3. ouss

    My file name (field is the default name field) 0345, 0355 etc

    is the query something like this: body(‘Get_files’)?[‘value’]?[0345]?[name’]?

    1. Pavel Bludov

      I don’t think this approach will work for you as a file name very unlikely correlates with Apply To Each.
      Also replying to your email.

  4. ouss

    ah oke, how can I create this formula?


    It gives me an error now…

    1. Pavel Bludov

      Replied to your email.


Leave a Reply