Did you know that With() is one of the most underused functions in Power Fx? It can dramatically improve both readability and performance — especially if you’re guilty of calling the same LookUp() three or four times in a single formula.
The problem
Look at this very common Power Apps pattern:
If(
LookUp(Orders, OrderId = ThisItem.Id).Status = "Open",
LookUp(Orders, OrderId = ThisItem.Id).Customer & " — pending",
LookUp(Orders, OrderId = ThisItem.Id).Customer & " — closed"
)
That’s three delegated lookups firing every time the row repaints. On a gallery with 50 rows, that’s 150 calls. Most of the time it’s the same record being fetched over and over.
The fix with With()
With() lets you create a small, scoped record of named values, then reuse them inside an expression. The lookup runs once:
With(
{ order: LookUp(Orders, OrderId = ThisItem.Id) },
If(
order.Status = "Open",
order.Customer & " — pending",
order.Customer & " — closed"
)
)
One lookup. Same result. Easier to read.
When to reach for With()
- Repeated
LookUp()orFilter().First()in the same formula — the canonical case. - Long, hard-to-read expressions where the same sub-expression appears more than once.
- Expensive calculations (concatenations, date math, JSON parsing) that you’d otherwise repeat.
- Formulas you’ll come back to in 6 months and want to actually understand.
When not to use it
- For values that change between iterations of a
ForAll— the named record is calculated once at theWith()boundary. - When the inner expression is so simple that adding
With()makes it harder to read.
Bonus: chained With()
You can nest With() to build up multiple named values:
With(
{ order: LookUp(Orders, OrderId = ThisItem.Id) },
With(
{ customer: LookUp(Customers, Id = order.CustomerId) },
order.Total & " for " & customer.Name
)
)
Why this matters
Performance issues in Power Apps almost always come from repeated lookups inside galleries, forms, or labels. With() is the single fastest fix you can apply: same logic, fewer round trips to the data source, and code that the next maker can actually maintain.
It also helps with delegation diagnostics. When you scatter the same LookUp() across a complex If() formula, it becomes hard to tell which call is delegated and which one falls back to local. Pull the lookup into With() and you have one place to check the delegation warning, one place to test, and one place to swap the data source if you ever migrate (SharePoint to Dataverse, for example).
A subtle but powerful side benefit is testability. Once your formula is structured around a named record, you can temporarily replace that record with a literal value while you’re debugging the rest of the expression. That kind of scoped substitution is impossible when the same expression is repeated five times across nested branches.
Anti-patterns to retire alongside With()
- Repeating
Filter(..., column = value).First()in a label \u2014 same problem, same fix. - Putting big formulas in
OnVisibleto set context variables that are only used in one control \u2014 aWith()keeps the calculation local and easier to reason about. - Concatenating long strings inline in
Textproperties \u2014 aWith()lets you assemble named pieces and reuse them across multiple controls.
Adopt With() as your default for any formula that mentions the same record, table, or sub-expression more than once. Your future self (and your performance metrics) will thank you.
💬 Comments & Suggestions
Share your thoughts, tips, or drop a useful link below.