Refactoring Power Apps Formulas with With()

A practical Power Fx refactoring pattern for using With() to remove repeated dropdown logic and add agency-specific section values.

Sometimes a Power Apps formula starts out simple, then grows as more business rules are added.

That is usually the moment when I stop and ask: can this be easier to read?

I recently had this situation with two dropdowns:

  • ddEntity for selecting an entity
  • ddSection for selecting a section under that entity

The normal rule was simple. When a user selected an entity, the section dropdown should show the sections linked to that entity.

Then came one extra rule: if the selected entity is an agency, the section dropdown should also include:

  • Country Rep
  • Deputy Country Rep

The Requirement

The source collection was colEntitiesSections.

Each record contained the entity in Org, the section in Section, and a boolean flag called 'Agency?'.

For normal entities, the section dropdown should only show the distinct sections linked to the selected entity.

For agencies, it should show those same sections plus the two additional agency-specific options.

The Repetition Problem

The first version of the formula worked, but the section-filtering logic had to be repeated in more than one place.

That made the formula harder to scan and more annoying to maintain. If I needed to change how sections were filtered later, I would have to update the same expression in multiple branches of the formula.

The repeated part was this:

Distinct(
    ShowColumns(
        Filter(
            colEntitiesSections,
            Org.Value = ddEntity.Selected.Value
        ),
        Section
    ),
    Section
)

It is not a complicated expression, but once it appears inside a larger If, the formula becomes noisy very quickly.

Refactoring with With

Power Fx With() is useful when part of a formula needs a temporary name.

Instead of repeating the section lookup, I can calculate it once as varSections, then reuse it in the rest of the formula.

With(
    {
        varSections:
            Distinct(
                ShowColumns(
                    Filter(
                        colEntitiesSections,
                        Org.Value = ddEntity.Selected.Value
                    ),
                    Section
                ),
                Section
            )
    },
    If(
        ddEntity.Selected.'Agency?',
        Ungroup(
            Table(
                {tbl: varSections},
                {
                    tbl: Table(
                        {Value: "Country Rep"},
                        {Value: "Deputy Country Rep"}
                    )
                }
            ),
            tbl
        ),
        varSections
    )
)

This version is easier to follow because the formula now has two clear steps:

  1. Get the distinct sections for the selected entity.
  2. If the entity is an agency, append the extra agency section values.

Why Ungroup Works Here

The agency branch needs to combine two small tables:

  • the existing varSections table
  • a manually created table containing Country Rep and Deputy Country Rep

This part creates a table of tables:

Table(
    {tbl: varSections},
    {
        tbl: Table(
            {Value: "Country Rep"},
            {Value: "Deputy Country Rep"}
        )
    }
)

Then Ungroup() flattens those nested tables into one table that the dropdown can use.

Ungroup(
    Table(
        {tbl: varSections},
        {
            tbl: Table(
                {Value: "Country Rep"},
                {Value: "Deputy Country Rep"}
            )
        }
    ),
    tbl
)

The important detail is that both tables use the same output column name: Value.

That matches the shape returned by Distinct(), so the dropdown receives one consistent table.

Adding the Agency Flag to the Entity Dropdown

For the section dropdown formula to work, ddEntity.Selected also needs to include the 'Agency?' flag.

I updated the Items property of the entity dropdown like this:

AddColumns(
    Distinct(
        colEntitiesSections,
        Org.Value
    ),
    'Agency?',
    LookUp(
        colEntitiesSections,
        Org.Value = Value
    ).'Agency?'
)

This creates a distinct list of entities, then adds the related agency flag back onto each entity row.

After that, the section dropdown can use:

ddEntity.Selected.'Agency?'

to decide whether to add the agency-specific section values.

Result

The formula now does the same work with less repetition.

When a normal entity is selected, the section dropdown shows only the sections linked to that entity.

When an agency is selected, the dropdown also includes:

  • Country Rep
  • Deputy Country Rep

The biggest benefit is maintainability. The base section logic now lives in one place inside With(), so future changes are easier and less risky.

Takeaway

With() is one of my favorite Power Fx tools for making formulas easier to read.

It is especially helpful when:

  • the same expression is used more than once
  • a formula has nested If logic
  • a dropdown or gallery formula is doing several steps at once
  • a temporary name would make the business logic clearer

Small refactors like this do not change the app's behavior, but they make the formula easier for the next maker to understand.