Published: December 10, 2025 • 20 min read • By UPLYNK
The Power Fx Masterclass: Leveraging the With() and ForAll() Functions Effectively
In Power Apps, writing clean, readable and efficient code is crucial for long-term maintainability. Two of the most powerful and often underutilized Power Fx functions that can drastically improve your code quality are With() and ForAll().
If you're looking to move beyond basic formulas, this masterclass will show you how to leverage these functions to simplify complex logic, enhance performance and adhere to the highest coding standards.
⭐ 1. Mastering the With() Function: The Power of Local Scope
The With() function allows you to define a named temporary record or variable that can be used within its scope. Think of it as introducing a temporary, well-named variable right where you need it, eliminating the need for repeating complex calculations or saving results to a context variable (UpdateContext).
Why use With()?
- Readability: It breaks down long, nested formulas into logical, named steps.
- Performance: The calculation for the temporary variable runs only once, even if you reference it multiple times.
- Self-Documenting Code: The names you assign to the local variables serve as natural comments.
Syntax
The basic syntax is simple:
With(
Record,
Formula
)
- Record: The local record definition where you define the temporary named values.
- Formula: The main calculation that uses the values defined in the Record.
Example: Simplifying a Complex Calculation
❌ Bad (Nested and Repetitive Logic):
Imagine calculating a tax on an item after applying a discount, then checking if the final price is over a threshold.
If(
(txtPrice.Text * (1 - SliderDiscount.Value / 100)) * (1 + 0.05) > 1000,
"High Value Item",
"Standard Item"
)
✅ Good (Using With() for Clarity and Efficiency):
With(
{
// Step 1: Calculate the discounted price
varDiscountedPrice: txtPrice.Text * (1 - SliderDiscount.Value / 100),
// Step 2: Calculate the final price with tax (5%)
varFinalPrice: varDiscountedPrice * 1.05
},
// Final logic is clean and clear
If(
varFinalPrice > 1000,
"High Value Item",
"Standard Item"
)
)
By using With(), the logic is transformed from a dense mathematical expression into three clear, sequential steps.
⭐ 2. Leveraging ForAll(): Efficient Bulk Operations
The ForAll() function is designed to perform the same action or calculation for every record in a table. It is the go-to function for bulk data manipulation, especially when dealing with collections or data sources.
Why use ForAll()?
- Bulk Patching/Updates: It's the standard way to update multiple records in a data source (like Dataverse or SharePoint) in one command, often more efficiently than iterating manually.
- Collection Transformation: It allows you to transform one collection into a new one, adding new columns or manipulating existing data.
- Parallel Execution (Key Benefit): Power Apps can often execute the logic within ForAll() in parallel for all records, which can lead to significant performance gains over sequence-based commands like Sequence() + ClearCollect(). When you use ForAll(SourceTable, Logic), Power Apps can process the Logic against the records of the SourceTable concurrently, rather than one after the other. This ability to run operations in parallel leads to significant performance improvements, especially when the logic involves complex computations, multiple data source updates or a large number of records.
Syntax
ForAll(
Source,
Formula (Action)
)
- Source: The table or collection you want to iterate over.
- Formula: The action you want to execute for each record (e.g., Patch, Collect, or a calculation).
Real-World Example: Bulk Status Update
ForAll(
// 1. Source: The collection of tasks to be updated
colPendingTasks,
// 2. Action: Patch each record to change the Status
Patch(
'Task Table',
ThisRecord, // Reference the current record being processed
{
Status: 'Status (Tasks)'.Approved,
ApprovedBy: User().FullName
}
)
)
💡 Pro-Tip: Transforming Data
You can also use ForAll() to quickly generate a new table without committing the changes to a data source.
For example, to quickly calculate the projected sales value for every line item in a sales collection:
ClearCollect(
colProjectedSales,
ForAll(
colLineItems,
{
ItemName: ThisRecord.Title,
UnitCost: ThisRecord.Cost,
ProjectedRevenue: ThisRecord.Cost * 1.15 // Adding 15% margin
}
)
)
⭐ 3. Avoiding the ForAll() Pitfall: When Not to Use It
While ForAll() is excellent for bulk operations, it has one key limitation you must be aware of:
You cannot directly modify the table you are iterating over within the ForAll() loop.
If you try to use Collect() or ClearCollect() on the source collection inside the ForAll() loop, you will receive an error.
❌ Invalid Code (Self-Modification Attempt):
// This will cause an error!
ForAll(
colItems,
Collect(colItems, {NewValue: ThisRecord.Value * 2})
)
✅ The Solution: Use ClearCollect() with the ForAll() calculation outside the function, as shown in the Transforming Data example above or use the Collect() function on a different collection.
Conclusion
By integrating With() and ForAll() into your daily Power Fx practice, you move from being a casual maker to a skilled developer.
- Use With() to simplify complex formulas and improve readability.
- Use ForAll() to execute bulk operations (like updating records or transforming tables) efficiently and in parallel.
These functions are foundational to building professional, high-performing and maintainable Power Apps that scale with your organization's needs.
“At UPLYNK, we’re committed to empowering the Microsoft Dynamics 365 community through insightful blogs, practical tutorials and real-world implementation guidance — helping professionals learn, grow and stay ahead in the ever-evolving D365 ecosystem.”