December 13, 2024

SamTech 365

PowerPlatform, Power Apps, Power Automate, PVA, SharePoint, C#, .Net, SQL, Azure News, Tips ….etc

PowerApps Performances’ Optimisation – Parallel Processing

In the two previous posts, I covered the different data sources, and the impact of having a heaving OnStart event.

Today, we will look at how we can improve PowerApps Performances by taking advantage of the parallel processing functionalities available in PowerApps.

Parallel or Concurrent  Data Load/Read

When it comes to initializing your collections of data, in most cases you would be tempted to do something like:

ClearCollect(FirstCollection, Filter(BackEndDataSource, Column1="Project 1"));
ClearCollect(NewProducts, Filter(ProductsTables, ManufacturingYear=2022));
ClearCollect(ThirdCollection, Filter(BackEndDataSource2, ColumnFilter="Project 1"));
...
...
...
ClearCollect(NthCollection, FirstN(BackEndDataSource, 100));

The issue ?

No particular issue, except that these calls will run in a sequence (sequential order), which means that the next call won’t be made until the first one completes.

This might be a problem if we are loading large data sets, and the impact of performances can be huge.

What to do instead ?

Easy, we should wrap these calls in the concurrent function, which allows us to make these calls in parallel.

Still the overall Concurrent function won’t finish until all the sub-calls have been completed. But this minor tweak will improve considerably the overall performances of your application.

Concurrent(
   ClearCollect(FirstCollection, Filter(BackEndDataSource, Column1="Project 1")),
   ClearCollect(NewProducts, Filter(ProductsTables, ManufacturingYear=2022)),
   ClearCollect(ThirdCollection, Filter(BackEndDataSource2, ColumnFilter="Project 1"))
   ...,
   ...,
   ...,
   ClearCollect(NthCollection, FirstN(BackEndDataSource, 100))
)

 

Parallel data writing / Patch

Following the same logic, when it comes to writing data to the back end (regardless of which back end you are using), the default behaviour would be to perform a series of Patches in sequence.

Here, the same problem as previously, the next write (Patch), won’t start until PowerApps Performances the previous one finishes.

ForAll(
    ProductsToBeUpdatedColl,
    Patch(
        Products,
        LookUp(Product, ID=ProductsToBeUpdatedColl[@ID]),
        {
            Name: ProductsToBeUpdatedColl[@Name],
            Description: ProductsToBeUpdatedColl[@Description],
            PPUnit: ProductsToBeUpdatedColl[@PPUnit]
        }
    )
);

What to do instead ?

Instead of looping for each item of a collection, we should make one Patch call and pass the collection to be updated as follow:

Patch(
    Products,
    ShowColumns(
        ProductsToBeUpdatedColl,
        "Name",
        "Description",
        "PPunit"
    )
);