Friday, February 21, 2025

Table of Contents

Modularizing Formulas in Power Fx: Why and How

Ever struggled with long, messy formulas in Power Fx? You’re not alone. Writing complex formulas can quickly turn into a nightmare—hard to read, tough to maintain, and easy to break. That’s where modularizing formulas comes in.

What is Modularizing Formulas?

Simply put, it’s breaking down big, repetitive formulas into smaller, reusable pieces. This makes your code cleaner, easier to manage, and more efficient.

Why Should You Modularize?

Better readability – No more scrolling through long formulas.
Reusable logic – Avoid writing the same formula over and over.
Easier maintenance – Fix or update formulas in one place.
Improved performance – Reduce unnecessary calculations.

How to Modularize Formulas in Power Fx

Use Variables Instead of Repeating Code

Instead of performing the same calculation multiple times, store the result in a variable.

🔴 Without modularization:

If(TextInput1.Text = "Yes" || TextInput1.Text = "YES" || TextInput1.Text = "Y", "Approved", "Rejected")

🟢 Modularized version:

Set(Response, Lower(TextInput1.Text));
If(Response = "yes" || Response = "yes" || Response = "y", "Approved", "Rejected")

Here, we store the input in a variable (Response) and reuse it, making the formula shorter and more efficient.

Create Reusable Functions or Components

If you find yourself using the same logic multiple times, consider creating reusable Power Fx functions or custom components. This way, you only need to update logic in one place rather than across multiple screens or apps.

Use Collections and Tables Smartly

Instead of filtering or recalculating values multiple times in a gallery, store the results in a collection and reference it. This reduces repeated calculations and improves performance.

By modularizing your formulas, you’ll write cleaner, faster, and more maintainable Power Fx code. Have you tried modularization in your apps? Let me know in the comments! 🚀

Why You Should Avoid Nested IFs in Canvas Apps

Ever found yourself tangled in a web of nested If statements in Power Fx? At first, they seem like a simple way to handle multiple conditions, but as they grow, they quickly become a nightmare—hard to read, difficult to maintain, and inefficient.

Let’s break down why deeply nested Ifs are a bad idea and explore better alternatives to keep your Canvas Apps clean and efficient.

What Are Nested IFs?

A nested If statement is when you place multiple If conditions inside each other. While this allows for complex decision-making, it can get messy fast.

Example of a Nested If in Power Fx:

If(
TextInput1.Text = "Admin", "Access Granted",
If(
TextInput1.Text = "User", "Limited Access",
If(
TextInput1.Text = "Guest", "Read-Only Access",
"No Access"
)
)
)

At first glance, this looks manageable. But what happens when you add more roles? Debugging becomes painful, and modifying it turns into a risky game.

Why Nested IFs Are a Problem

🔴 Reduced Readability

  • Complex If statements become hard to read and debug.
  • Teams struggle to collaborate when logic isn’t clear.

🛑 Difficult Maintenance

  • Making changes means modifying multiple places—one small mistake can break everything.
  • Adding new conditions makes things even more complicated.

⚠️ Performance Issues

  • Power Apps evaluates every If statement, even when it finds a match early.
  • Deeply nested Ifs slow things down and waste processing power.

Better Alternatives to Nested IFs

Instead of relying on deeply nested If statements, try these cleaner and more efficient approaches:

 

1️⃣ Use Switch Statements

If you’re checking multiple exact values, Switch is a much better option.

Switch(
TextInput1.Text,
"Admin", "Access Granted",
"User", "Limited Access",
"Guest", "Read-Only Access",
"No Access"
)

Why it’s better?
✔️ Much cleaner and easier to read.
✔️ Stops evaluating once it finds a match, improving performance.

 

2️⃣ Use Lookups with Tables or Collections

If your conditions map directly to specific outputs, use LookUp with a table instead.

Set(AccessLevels, Table(
{Role: "Admin", Access: "Access Granted"},
{Role: "User", Access: "Limited Access"},
{Role: "Guest", Access: "Read-Only Access"}
));
LookUp(AccessLevels, Role = TextInput1.Text, Access, “No Access”)

Why it’s better?
✔️ More dynamic—You can update conditions without modifying the formula.
✔️ Easier to scale—Just add new roles to the table instead of modifying logic.

 

3️⃣ Use Boolean Variables for Complex Conditions

For more complex logic, define Boolean variables beforehand.

Set(IsAdmin, TextInput1.Text = "Admin");
Set(IsUser, TextInput1.Text = "User");
Set(IsGuest, TextInput1.Text = "Guest");
If(IsAdmin, “Access Granted”,
IsUser, “Limited Access”,
IsGuest, “Read-Only Access”,
“No Access”)

Why it’s better?
✔️ Easier to read and debug.
✔️ Simplifies future updates—just adjust the variables instead of modifying If logic.

 

Final Thoughts

Nested Ifs might work for simple conditions, but they can quickly become unmanageable as your app grows. By switching to Switch, LookUp, or pre-defined variables, your Power Fx code will be:

Cleaner
Faster
Easier to maintain

Got a messy If statement in your app? Try refactoring it with these techniques, and let me know if you need help! 🚀

Optimizing Power Fx Formulas: Best Practices for Faster Apps

Ever noticed your Power Apps running slow? Laggy UI, slow data loads, or formulas that take forever to process? That’s often due to inefficient Power Fx formulas.

Power Fx is powerful, but if not optimized, it can lead to performance issues that frustrate users. Let’s explore some best practices to keep your apps fast and efficient.

Common Performance Issues in Power Fx

Before jumping into solutions, here are a few common culprits behind slow apps:

🔴 Too many nested If statements – Slows down execution and makes logic hard to read.
⚠️ Overusing LookUps inside galleries – Causes unnecessary recalculations.
🛑 Fetching too much data at once – Large datasets slow things down.
Unnecessary recalculations – Formulas in labels and controls constantly re-evaluating.

Best Practices for Power Fx Performance Optimization

1️⃣ Use Variables to Store Data

Instead of repeatedly evaluating the same expression, store it in a variable and reuse it.

🔴 Inefficient:

If(TextInput1.Text = "Admin", "Access Granted", If(TextInput1.Text = "User", "Limited Access", "No Access"))

🟢 Optimized:

Set(UserRole, TextInput1.Text);
Switch(UserRole, "Admin", "Access Granted", "User", "Limited Access", "No Access")

Why? Fewer redundant calculations mean better performance.

 

2️⃣ Use Collections for Repeated Data

Fetching data inside a gallery multiple times is a performance killer. Instead, store data in a collection and reference it.

🔴 Inefficient:

LookUp(Users, ID = ThisItem.UserID, Name)

🟢 Optimized:

ClearCollect(UserData, Users);
LookUp(UserData, ID = ThisItem.UserID, Name)

Why? Reduces repetitive data calls and speeds up retrieval.

 

3️⃣ Minimize OnVisible or OnStart Actions

Loading too much data when the app starts slows down initialization. Instead, optimize how data loads.

🔴 Inefficient:

ClearCollect(AllRecords, Filter(LargeDataset, Status = "Active"))

🟢 Optimized:

Concurrent(
ClearCollect(ActiveRecords, Filter(LargeDataset, Status = "Active")),
ClearCollect(OtherData, SomeOtherTable)
)

Why? Concurrent() loads multiple datasets at the same time, making startup much faster.

 

4️⃣ Use Delegable Queries for Large Datasets

Delegation ensures that Power Apps processes data on the server instead of bringing everything into the app first.

🔴 Non-Delegable Query (BAD!):

Filter(Users, Left(Name, 3) = "Joh")

🟢 Delegable Query (GOOD!):

Filter(Users, StartsWith(Name, "Joh"))

Why? StartsWith() is delegable, meaning Power Apps doesn’t fetch unnecessary data, keeping things fast.

More Examples of Delegable Queries:

Using Filter:

Filter(Orders, Status = "Completed")

🔹 Works with large datasets because equality comparisons are delegable.

Using Sort:

Sort(Products, Price, Ascending)

🔹 Sorting happens in the data source instead of inside the app.

Using Search (Delegable in Dataverse & SharePoint):

Search(Customers, "Smith", "LastName")

🔹 Faster server-side search instead of slow in-app filtering.

Using LookUp:

LookUp(Employees, EmployeeID = 12345, FullName)

🔹 Quickly retrieves a single record from large datasets.

 

5️⃣ Optimize Gallery Performance

Avoid re-evaluating formulas inside a gallery—this can significantly slow things down.

🔴 Inefficient:

Filter(LargeDataset, Status = "Active")

🟢 Optimized:

ClearCollect(ActiveRecords, Filter(LargeDataset, Status = "Active"));
Gallery.Items = ActiveRecords

Why? Instead of filtering the dataset multiple times, we preload it once and use the collection.

 

Final Thoughts

Optimizing your Power Fx formulas isn’t just about making your app run faster—it’s about improving the user experience. By using variables, collections, concurrent data loading, and delegable queries, you’ll keep your app smooth and responsive, even with large datasets.

Need help optimizing a specific formula? Drop a comment below! 🚀

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

Photo Ynias
Tech & Power Platform enthusiast

Ynias Bensch

©2023  All Right Reserved.  | Cookie Policy | Privacy Policy