Published: December 10, 2025  •  20 min read  •    By UPLYNK

The Power Fx Masterclass: Leveraging the With() and ForAll() Functions Effectively

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()?

Syntax

The basic syntax is simple:

With(
Record,
Formula
)

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()?

Syntax

ForAll(
Source,
Formula (Action)
)

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.

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.”