Let’s take a look at the With() PowerFx function to get a better understanding of its capabilities, when to use it, and some samples.
This function was added for Canvas apps back in mid-2019, see this blog post by Greg Lindhorst1, and Greg describes it this way –
“You can now simplify large formulas by dividing them into named sub-formulas, eliminate redundant sub-formulas, and easily work with functions that return records. All while staying declarative and automatically responding to dependency changes.”
But what does this mean for you and your apps?
Let’s take a look!
What can With() do?
Or what can you do with With2? In some ways it is kind of like a variable you might create with Set() or UpdateContext(), so instead of writing the same LookUp or Filter formula over and over, you can do it once and reference the named value for the rest.
Here are some ways to think about With():
It is meant to return records but you can drill down to the item value level if you choose
It is internal to the property where it is declared – you can’t reference it outside of where it is declared like you can with variables
You can’t even reference it in another property of the same control, let alone elsewhere on the same screen or another screen in the app
You minimize calls to your data source(s) by referencing the same named value as many times as needed
It can be used to work around delegation limitations
Why should you use it?
The main reason is this function can reduce multiple calls to your data source which will increase your app’s responsiveness and speed. Take a look at Wrikto’s good example here3 where you might make three or more calls to the Office365Users connector to get different pieces of profile data, but if you use With() it can be done with a single call to the connector.
TIP – Writko’s article also touches on something else I consider a Power Apps best practice – setting record variables when possible and then referencing the items in the record as needed with an additional argument on the end of the variable (e.g. varUser.Department).
And a secondary reason, is it is faster and cleaner to write, making your code shorter and less repetitive ultimately saving you keystrokes and time.
When should you use it?
I would consider using With() a Power Apps best practice any time you have multiple calls to the same data source or are trying to deal with potential data source delegation issues4. It can also be helpful when working with complex formulas or equations, like this mortgage calculator example here5.
Syntax Example
Let’s wrap up this post with an example.
With(Record, Formula)
⇓
With({namedValue: formula},LookUp(datasource,column=namedValue.column))
⇓
With(
{
wTTID: LookUp( // first Named Value
Job_Townhome,
EwJobId = varJobID
)
},
With( // nested With()
{wLTT: Lookup_TownhomeType}, // second Named Value
First(
Filter(
wLTT, // call second Named Value
Id = wTTID.TownhomeTypeId // lookup against first Named Value
).TownhomeType
)
)
)
As always, thanks for reading!