Avoiding Unnecessary Recalculation of Expressions

Loading

Avoiding Unnecessary Recalculation of Expressions in Power Apps

Introduction

When developing Power Apps, inefficient calculations can slow down app performance, cause lag, and increase processing time. This happens when Power Apps recalculates expressions repeatedly, even when the values haven’t changed. By optimizing how and when calculations occur, you can improve performance and create a faster, more responsive app.

This guide explains why unnecessary recalculations happen, and provides step-by-step strategies to avoid them.


1. Why Do Unnecessary Recalculations Happen?

Power Apps automatically recalculates expressions whenever there is a change in dependencies. This includes:

🚨 Using the same calculation multiple times in different places.
🚨 Referencing dynamic controls (like sliders or text inputs) in calculations.
🚨 Performing LookUp or Filter operations repeatedly.
🚨 Using complex calculations in gallery items, OnVisible, or OnStart.

Example of Inefficient Calculation

If(
    LookUp(Orders, ID = SelectedOrderID).Status = "Completed",
    "Delivered",
    "Pending"
)

🚨 Problem: If this is used multiple times, Power Apps runs the LookUp() function each time instead of reusing the result.

βœ… Solution: Store the result in a variable instead of recalculating it each time.


2. How to Avoid Unnecessary Recalculations

2.1 Use Variables for Static Values

Instead of recalculating an expression each time it’s needed, store it in a variable using Set() or UpdateContext().

Before (Inefficient Calculation in Multiple Places)

If(User().Email = "admin@company.com", true, false)

🚨 Problem: If this expression appears in multiple controls, Power Apps recalculates it every time.

After (Optimized with a Variable in OnStart)

Set(IsAdmin, User().Email = "admin@company.com")

βœ… Now, use IsAdmin throughout the app instead of recalculating it.


2.2 Use Collections Instead of Multiple LookUps

If you’re fetching data multiple times using LookUp(), store it in a collection instead.

Before (Repeated LookUp in Multiple Controls)

LookUp(Users, Email = User().Email).Role

🚨 Problem: Runs the LookUp() function each time it is used.

After (Optimized by Storing in a Collection on Start)

ClearCollect(UserData, Filter(Users, Email = User().Email));
Set(UserRole, First(UserData).Role)

βœ… Now, use UserRole instead of LookUp() in multiple places.


2.3 Avoid Recalculating Dynamic Controls (Like Sliders, Dropdowns, or Text Inputs)

If you reference a control directly in a formula, it recalculates every time the control changes.

Before (Inefficient Formula in Multiple Places)

Slider1.Value * 10

🚨 Problem: Every time the slider moves, all calculations that use it will be recalculated.

After (Optimized by Storing in a Variable on Change)

Slider1.OnChange = Set(SliderValue, Slider1.Value * 10)

βœ… Now, use SliderValue instead of Slider1.Value * 10 throughout the app.


2.4 Use With() to Store Temporary Values Instead of Recalculating

With() allows you to store temporary values within a single formula without creating global variables.

Before (Repeated Calculation in a Single Formula)

If(
    LookUp(Orders, ID = SelectedOrderID).Status = "Completed",
    "Delivered",
    "Pending"
)

🚨 Problem: LookUp() runs twice.

After (Optimized Using With())

With(
    { OrderStatus: LookUp(Orders, ID = SelectedOrderID).Status },
    If(OrderStatus = "Completed", "Delivered", "Pending")
)

βœ… Now, LookUp() runs only once, and OrderStatus is reused.


2.5 Optimize Formulas in Galleries

Galleries recalculate formulas for each row, which can slow down performance.

Before (Inefficient LookUp in a Gallery)

Text = LookUp(Customers, ID = ThisItem.CustomerID).Name

🚨 Problem: Runs LookUp() for every row in the gallery.

After (Optimized by Preloading Data in a Collection)

ClearCollect(CustomerList, Customers)
Text = LookUp(CustomerList, ID = ThisItem.CustomerID).Name

βœ… Now, LookUp() is much faster because it queries a local collection instead of an external data source.


2.6 Move Calculations to OnStart or OnVisible When Possible

Instead of recalculating values every time the screen is used, store them on app start or when the screen is loaded.

Example (Before: Inefficient Calculation in a Control’s Property)

Filter(Orders, Status = "Pending")

🚨 Problem: This runs every time the control refreshes.

Example (After: Optimized by Storing in OnVisible)

Screen.OnVisible = ClearCollect(PendingOrders, Filter(Orders, Status = "Pending"))

βœ… Now, use PendingOrders instead of filtering the entire dataset each time.


3. Best Practices to Avoid Unnecessary Recalculations

βœ… Use Variables (Set(), UpdateContext()) for static or frequently used values.
βœ… Use With() to store temporary values inside a single formula instead of re-evaluating expressions.
βœ… Cache data using ClearCollect() to avoid repeated API or database calls.
βœ… Minimize LookUps in galleries by preloading the dataset.
βœ… Move calculations to OnStart or OnVisible when possible.
βœ… Avoid referencing dynamic controls (Slider, Dropdown, TextInput) directly in formulas.


4. Conclusion

Avoiding unnecessary recalculations in Power Apps boosts performance, speeds up load times, and improves user experience. By storing values in variables, using collections for lookups, and optimizing gallery formulas, you can ensure your app runs efficiently.

Would you like specific optimizations for SharePoint, SQL, or Dataverse?

Leave a Reply

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