Power Fx: Understanding | With()


Power Fx is the Excel-like programming language of the Power Platform, more specifically canvas apps. As with any of their programming offerings, Microsoft has bundled plenty of pre-built functions into Power Fx. Among these bundled functions, With() is easily a standout.


If you haven’t worked the With() function into your canvas apps yet, then you’re likely missing out. As your business apps grow in complexity, you’ll definitely appreciate its usefulness. Reading a bit from Microsoft’s documentation, it:

Calculates values and performs actions for a single record, including inline records of named values.

– Microsoft

So, we know what the With() function does, but what are its parameters? Well, the function accepts two parameters. Parameter #1 is a record object, which can be simple or complex. Parameter #2 is the function formula, which outputs the result of some actions. For example, imagine passing in a person record, then outputting their concatenated “Full Name”:

E.g., function syntax.

With(Record, Formula)

E.g., function output of “Full Name”.

With({
    nameF: ThisItem."First Name",
    nameL: ThisItem."Last Name"
},
    Concatenate(nameF, " ", nameL)
)

The above Concatenate() example only grazes the surface though. Remember, as makers, we can create inline variables AND perform actions. Returning to Microsoft’s documentation, take note that makers also can create sub-formulas:

Use With to improve the readability of complex formulas by dividing it into smaller named sub-formulas. These named values act like simple local variables confined to the scope of the With.

– Microsoft

To create sub-formulas, makers can nest With() functions within With() functions:

E.g., function output of nested function.

With({
    vDate: Now()
},
    With({
        vDateM: Month(vDate),
        vDateD: Day(vDate),
        vDateY: Year(vDate)
    },
        $"{vDateM}/{vDateD}/{vDateY}"
    )
)

Admittedly, formatting a date value using the With() function isn’t very amazing, but it works as a simple use-case. For something a bit more complex, imagine calculating the number of days in a month:

E.g.,

With({
    // Provided date
    vDate: DateValue("2/15/2024")
    },
    With({
        // Get first day of the month
        vMonthFirst: DateValue(
            $"{Month(vDate)}/1/{Year(vDate)}"
        )
        },
        With({
            // Move into next month
            vMonthNext: (vMonthFirst + 32)
            },
            With({
                // Get first day of month
                firstOfNextMonth: DateValue(
                    $"{Month(vMonthNext)}/1/{Year(vMonthNext)}"
                )
                },
                // Get final day of last month
                $"{Day(firstOfNextMonth - 1)}"
            )
        )
    )
)

Conclusion:
The With() function is capable. Very capable. Let it make your life easier…

“The master’s tools will never dismantle the master’s house.”

Audre Lorde

#BlackLivesMatter

One thought on “Power Fx: Understanding | With()

  1. Pingback: Power Apps: Canvas App | Ranked Choice Voting (RCV) | console.log('Charles');

Leave a comment