‘With’ Function – An Essential for Power Apps Developers

Why the ‘With’ Function Changes Everything

Ever stared at a formula in Power Apps and thought, “This is getting out of hand”?

That’s where the With function comes in. It’s a simple yet powerful tool that keeps your formulas clean, reduces repetition, and helps you manage even the most complex logic.

If you’re serious about building better Power Apps, mastering the With function isn’t just helpful—it’s essential.

What is the ‘With’ Function?

At its core, the With function allows you to create temporary, local variables inside a formula. These variables live only within the With function and disappear once it’s done.

Think of it as your personal workspace to break down messy formulas into manageable, reusable steps.

Syntax:

With(
  { 
    VariableName: Value 
  },
  ExpressionUsingVariable
)
JavaScript

Why Every Developer Should Use ‘With’

Cleaner Code
Stop piling up complicated formulas. With helps you organize your logic into readable chunks.

Reduced Redundancy
Need the same calculation in multiple places? Define it once and reuse it—no more copy-pasting!

Local Variables Only
Variables in With are temporary, meaning they won’t clash with or affect the rest of your app.

Tackle Nested Logic
Got multiple layers of calculations? Nest With functions to make even the most complex logic approachable.

Real-Life Examples

Simplify Repetitive Calculations

Without With, a formula to calculate a discounted price might look like this:

Text(ThisItem.Price - (ThisItem.Price * ThisItem.Discount), "Currency")
JavaScript

With With, it’s cleaner and easier to follow:

With(
    { DiscountedPrice: ThisItem.Price * (1 - ThisItem.Discount) },
    Text(DiscountedPrice, "Currency")
)
JavaScript

Nesting for Complex Logic

Sometimes, you need to layer multiple calculations. Here’s how With handles that:

With(
    { BasePrice: ThisItem.Price },
    With(
        { DiscountedPrice: BasePrice * (1 - ThisItem.Discount) },
        Text(DiscountedPrice, "Currency")
    )
)
JavaScript

Each layer is clear, organized, and easy to debug.

When to Use the ‘With’ Function

  • Long or Complex Formulas: Break them into smaller, more readable steps.
  • Reusable Calculations: Save time by storing intermediate values.
  • Nested Logic: Handle multiple layers of calculations without losing track.

Pro Tip: Keep It Simple

While With is powerful, don’t overcomplicate things. For straightforward formulas, it might be better to skip it. But for anything lengthy or repetitive? With is your best friend.

Final Thoughts

The With function might seem small, but its impact is huge. It’s the key to writing cleaner, more efficient code in Power Apps.

If you’re not already using With, give it a try—you’ll wonder how you ever worked without it.


Leave a Reply

Your email address will not be published. Required fields are marked *