Search techniques

  • Yellow belt
  • Standard
  • Tip 148

Shorten loops with take() while testing

Quiz question

What does take(array, 5) return if the array has only three items?

Solution

  1. A) All three items, without an errorCorrect. take() returns at most the given number. If there are fewer, all existing items come back.
  2. B) An error, because items are missingWrong. take() does not fail when the array is shorter than the requested number.
  3. C) Three items plus two empty onesWrong. take() does not fill anything up. It only cuts off what goes beyond the number.

What it is about

Testing a loop over hundreds of items costs time and actions on every run. With take() you process only the first items while testing. You replace the input of Apply to each with an expression like take(outputs('Select'), 5). The loop then runs exactly five times, no matter how large the array is. If the array has fewer items, take() simply returns all of them, without an error. This way you check the logic inside the loop on a few real records. Once everything works, you remove take() and select the original output again. The structure of the flow stays unchanged.

The expression from the tip

take(outputs('Select'), 5)

When to use it, when not

Use it

When testing loops over large lists or tables, before you let the flow loose on all data.

Better not

In production. If take() stays in the flow, it permanently processes only the first items.

Most common mistake

Forgetting take() in the flow after testing. A note on the action reminds you to remove it again.

License

Standard Runs without a premium connector, so the license from Microsoft 365 is enough. Source on Microsoft Learn: Power Automate licensing FAQ. For your whole flow: License check.

Full tip on powerplatformtip.comPowerPlatformTip #148, with screenshots

Share

Short link: powerautomate.ninja/en/148

Source: PowerPlatformTip #148, Microsoft Learn: Reference for functions in workflow expressions.