You're Probably Overusing "Apply to each" in Power Automate
When I started building Power Automate solutions, my answer to almost every array was the same: Use "Apply to each." It worked. My flows ran successfully, and I moved on. But as projects became larger, I noticed a pattern: Flows took longer to execute. Action counts kept increasing. Debugging became painful. Simple tasks required dozens of actions. That's when I started looking for ways to simplify my flows-and XPath became one of my favorite techniques for working with XML.
The Problem
Imagine receiving an XML response with hundreds of employee records. If you only need one employee's name, should your flow really loop through every record? Probably not.
The Better Approach
XPath lets you query XML directly. Instead of saying: "Check every employee until you find ID 102." You simply ask: "Give me the employee whose ID is 102."
The expression looks like this:
xpath(
xml(outputs('Compose')),
'/Employees/Employee[Id="102"]/Name/text()'
)
The result?
David
That's it. No loop. No condition. Just the data you need.
Why This Matters
Using XPath can help you:
- Reduce unnecessary actions
- Improve execution speed
- Simplify maintenance
- Build cleaner enterprise automation
It's a small optimization, but when applied across dozens of production flows, the impact adds up.
When to Use XPath
XPath is ideal when you're working with:
- XML files
- SOAP APIs
- XML responses from legacy systems
- SharePoint XML payloads
- Structured XML documents
If your data is JSON, Power Automate already provides excellent functions such as filter(), select(), and first(). Choose the right tool for the job.
Final Thoughts
One lesson I've learned while building automation solutions is this: The fastest flow isn't always the one with the most actions-it's the one with the fewest necessary actions. Next time you add an Apply to each, pause for a moment and ask yourself: Can XPath solve this instead? You might be surprised how much simpler your flow becomes.
If you enjoy practical tips on Power Automate, SharePoint, Microsoft 365, SPFx, Python, and AI-powered automation, stay connected for more articles.
Comments
No comments yet. Start the discussion.