Skip to main content
Go back

Código más limpio con la función With

#powerapps #power-fx

When a Patch or Filter formula grows in complexity, it is common to end up repeating User().Email or LookUp(...) multiple times, making it hard to read. The With function is the solution for “Clean Code” in Power Apps.

It allows you to define local variables that only exist during the execution of that formula, making the code more readable and efficient by avoiding recalculating the same values.

Use Case: Creating a Complex Record

Without With, the code becomes noisy and repetitive.

power fx
// ❌ Repetitive: Calling User() 4 times
Patch(Requests, Defaults(Requests), {
  Title: "Request from " & User().FullName,
  RequesterEmail: User().Email,
  Manager: LookUp(Users, Email = User().Email).Manager,
  Department: LookUp(Users, Email = User().Email).Department
});

// ✅ With 'With': Define context once
With({
// Scope: Local variables calculated just once
CurrentUser: User(),
UserProfile: LookUp(Users, Email = User().Email)
},
Patch(Requests, Defaults(Requests), {
Title: "Request from " & CurrentUser.FullName,
RequesterEmail: CurrentUser.Email,
Manager: UserProfile.Manager,
Department: UserProfile.Department
})
);

It is better than creating context variables (UpdateContext) that you later have to remember to clear. Use it whenever you need to control the scope of local variables.

Mind the separator

Remember that if your Power Apps is configured for the European region, you will need to change commas , to semicolons ; in formulas. And use double semicolons ;; for chaining functions where normally a single semicolon ; would be used.