Skip to main content

How to Use the With Function in Power Apps

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

With gives a value a name for the length of one formula. Calculate it once at the top, then use the name as often as you need below. When the formula ends, the name is gone, so nothing is left behind on the screen.

Syntax​

Syntax
With({Name1: Value1, Name2: Value2}, Formula)
  • The first argument is a record. Its fields become names you can use in Formula.
  • Formula can calculate a value or perform actions, such as Patch.

Microsoft's With function reference says these names act like local variables confined to the With, and prefers With over context or global variables because it's self-contained.

Example: One Lookup, Used Three Times​

The Competency screen of the Career Development Plan app (Part 9) saves a competency. Before saving a new one, it looks for one with the same name and job title. That lookup is needed three times: to decide whether it's a duplicate, as the item to update, and in the message. With names it existing:

btnSaveCS → OnSelect (structure)
With(
{
existing: If(
IsBlank(locCompetencyId),
LookUp(
RoleCompetencies,
JobTitle = locJobTitle && Competency = Trim(txtNameCS.Text)
),
locCompetency
)
},
If(
IsBlank(locCompetencyId) && existing.IsActive,
Notify(existing.Competency & " is already in the plan for " & locJobTitle & ".", NotificationType.Error),
Patch(
RoleCompetencies,
If(IsBlank(existing), Defaults(RoleCompetencies), existing),
{...}
)
)
)

Inside the second argument, existing is the item found, or blank. SharePoint is asked once, even though the formula reads existing three times. The complete formula, with the columns it saves and IfError, is in Part 9:

A code window titled btnSaveCS → OnSelect: it sets locIsSaving, then With names existing as a LookUp in RoleCompetencies by job title and trimmed name, or locCompetency when editing; If existing is an active competency, it shows an error notification, otherwise IfError wraps a Patch with Defaults or existing, setting Competency, JobTitle, Level and IsActive true, and notifies and goes back

With or UpdateContext?​

WithUpdateContext
LivesOnly inside the formulaOn the screen, until changed
Works inAny formula, such as Text or ItemsBehavior formulas only, such as OnSelect
Use it forA value needed several times in one formulaA value other controls on the screen read

existing is only needed while saving, so With fits. locCompetency, the item the screen loaded, is read by several controls, so it's a context variable (How to Use Context Variables with UpdateContext).

One Name from Another​

In the example, existing repeats Trim(txtNameCS.Text) rather than using another name from the same record. To build one name from another, nest a second With: Microsoft's reference shows a mortgage calculator where the inner With uses the outer one's names.

Common Mistakes​

MistakeWhat happensDo this instead
Calling the same LookUp several times in one formulaThe data source is asked several timesName it once with With
A context variable for a value used only onceIt stays on the screen after the formulaUse With
Using a With name outside its formulaName isn't validUse it only inside the second argument

Troubleshooting​

SymptomLikely causeFix
A field of the name is blankThe value inside is blank, such as a LookUp that found nothingCheck it with IsBlank first
The whole formula returns an errorThe first argument is an error, and With doesn't run the formula thenFix the value being named

FAQ​

Can With perform actions?​

Yes. The reference says the formula can calculate a value or perform actions, such as modifying data, like the Patch above.

Can I use With in a property like Text?​

Yes. Unlike UpdateContext, With works in any formula.

Conclusion​

With names a value for one formula. Use it when a formula needs the same value several times, and keep context variables for values the rest of the screen reads.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.