PowerApps: Replace the Defaults() function to improve performance

The Deafults() function is often used inside a Patch() function to create new records, with this sort of syntax:

Patch(
    MyTable,
    Defaults(MyTable),
    {TextColumn: "Hello world", NumberColumn: 123.45})

Performance of this expression is slow because every time the Defaults() function is invoked, there is a call to the server where MyTable is stored.  The above expression would then make at least two calls, one for the Defaults() and one for the Patch().

One way to improve performance is to cache locally the return value of Defaults() in a global variable.  Then you can use the variable instead of Defaults() elsewhere in the app.

The most obvious way to cache the default values is to include a statement such as the following in the OnStart property of the landing screen (the first screen):

Set(DefaultsMyTable, Defaults(MyTable))

Please don’t forget that OnStart is called only when the app is opened, so after you have added the expression to OnStart, you will need to close the app and re-open it in order for the value to be cached.

You can then use DefaultsMyTable instead of Defaults(MyTable) everywhere else in your app.  You can safely do this because it is highly unlikely that the default values of your table will change during a user session.

For example:

Patch(MyTable,
    DeafultsMyTable,
    {TextColumn: "Hello world", NumberColumn: 123.45})

Performance should now be quicker, although you may lose a few seconds on app opening while the default value is cached.

You can actually even remove this delay on opening, but it requires digging a little deeper.  If you inspect the value stored in DefaultsMyTable, you will find that it is of record type and with all the columns of MyTable.  Normally the values for all columns are simply null (i.e. blank).  The only columns that are really needed are the ones that participate in the primary key.  Normally this is just an id column.  So actually in a lot of cases, something like this will work for creating a new record:

Patch(MyTable,
    {ID: Blank()},
    {TextColumn: "Hello world", NumberColumn: 123.45})

I hope this has helped you improve performance, any comments and suggestions are very welcome.

One Reply to “PowerApps: Replace the Defaults() function to improve performance”

Leave a Reply

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