In Power Apps, the With() function is used to create temporary variables (kind of like short-term placeholders) inside a formula. These variables only exist while the formula is being evaluated. This helps make formulas shorter and more readable because you don’t have to repeat calculations or expressions multiple times.
1. Calculations with Shared Logic
Perform complex calculations that share common intermediate logic.
Example: Width, Height, and Depth are named formulas.
With(
{ Area: Width * Height },
UpdateContext({ Perimeter: 2 * (Width + Height), Volume: Area * Depth })
)
Area is calculated once and reused.
Perimeter and Volume are updated in the screen context.
Makes formulas clean and reusable.
2. Simple Arithmetic
Perform basic calculations and reuse intermediate results.
With(
{ Result: 50 * 10 },
Result + 50
)
Result = 500
Final output = 550
3. Working with Collections
Handle filtered collections easily.
With(
{ MyProducts: Filter(Products, Price > 100) },
CountRows(MyProducts)
)
4. Nested With()
Sequential or multi-step calculations.
With(
{ Sum: Value(TextInput1.Text) + Value(TextInput2.Text) },
With(
{ Markup: Sum * 5 },
Markup / 2
)
)
5. Using With() in Galleries
Calculate totals or other aggregate values for a gallery.
With(
{ TotalPrice: Sum(Filter(Gallery1.AllItems, Category = "Electronics"), Price) },
Text(TotalPrice, "$0.00")
)
6. Date and Time Manipulation
Extract date components for filtering or calculations.
With(
{ Today: Now() },
UpdateContext({ YYYY: Year(Today), MM: Month(Today), DD: Day(Today) })
)
Captures current date/time
Updates screen context with Year, Month, Day
With(
{ StartDate: DateValue("01-01-2022"), EndDate: DateValue("12-31-2022") },
Filter(Orders, OrderDate >= StartDate && OrderDate <= EndDate)
)
7. Conditional Formatting and Styling
Set visual styles dynamically.
With(
{ Style: If(IsAdmin, Color.Blue, Color.Red) },
UpdateContext({ HeaderColor: Style, ButtonColor: Style })
)
IsAdmin is a global variable
Updates header and button colors based on user role
Great for accessibility or role-based UI changes
8. Grouping Operations on a Single Record
Perform multiple operations on a specific record.
With(
{ CurrentProduct: Lookup(Products, ProductID = selectedProductID) },
Patch(Products, CurrentProduct, { Stock: CurrentProduct.Stock - 1 });
Notify(CurrentProduct.Stock, NotificationType.Information)
)
9. Navigating Hierarchical Data
Filter related child records in a 1-N relationship.
With(
{ SelectedCategory: Lookup(Categories, CategoryID = selectedCategoryID) },
UpdateContext({ Subcategories: Filter(Subcategories, ParentID = SelectedCategory.CategoryID) })
)
10. Displaying User Information
Simplify multi-field concatenation.
With(
{ CurrentUser: Lookup(Users, ID = User().Email) },
Concatenate(
"Name: ", CurrentUser.FullName, Char(10),
"Email: ", CurrentUser.Email, Char(10),
"Phone: ", CurrentUser.Phone
)
)
11. Converting Duration Strings to Time
Extract hours, minutes, seconds from ISO 8601 duration strings.
With(
Match("PT2H1M39S", "PT(?:(?<hours>\d+)H)?(?:(?<minutes>\d+)M)?(?:(?<seconds>\d+)S)?"),
Time(Value(hours), Value(minutes), Value(seconds))
)
Uses named capture groups
Converts to Power Apps Time value
Can format as Text(..., "hh:mm:ss") for display
12. Creating a New Order with Details
Handle parent-child database operations elegantly.
With(
{ NewOrder: Patch(Orders, Defaults(Orders), { OrderStatus: "New" }) },
ForAll(
NewOrderDetails,
Patch(
OrderDetails,
Defaults(OrderDetails),
{ OrderNumber: NewOrder, Quantity: Quantity, ProductID: ProductID }
)
)
)
Creates a new order
Iterates over NewOrderDetails table
Adds details linked to NewOrder
Uses With() to store NewOrder reference
13. Comparing Two Records from Different Sources
Check if two records match.
With(
{ SpecificOrder: Lookup(Orders, OrderID = "Order123") },
If(
SpecificOrder.CustomerID = Gallery1.Selected.CustomerID &&
SpecificOrder.OrderDate = Gallery1.Selected.OrderDate &&
SpecificOrder.TotalAmount = Gallery1.Selected.TotalAmount,
true,
false
)
)
14. Evaluating Process Status and Sending Emails
Conditional actions based on record status.
With(
{ ProcessRecord: Lookup(Processes, ProcessID = "P123") },
Switch(
ProcessRecord.Status,
"Completed", Office365.SendEmail(ProcessRecord.AssignedTo, "Process Completed", Concatenate("Process ", ProcessRecord.ProcessID, " has been completed"))
)
)
15. Adding Checked Gallery Items to a Collection
Handle interactive UI selections.
With(
{ CheckedItems: Filter(Gallery1.AllItems, Checkbox1.Value = true) },
ForAll(
CheckedItems,
Collect(CollectedProducts, { ProductID: ProductID, ProductName: ProductName, Price: Price })
)
)
Filters gallery items where checkbox is checked
Iterates and adds to CollectedProducts collection
Clean, readable, avoids repeated Filter() calls
✅Conclusion
With() is ideal for temporary variables inside a formula.
Use it to:
Avoid repeating calculations
Group multiple operations
Simplify nested or complex logic
Variables inside With() exist only in that formula scope.
Combine With() with Patch(), ForAll(), UpdateContext(), and galleries for powerful, readable formulas.