Unlock Efficiency with Power Apps Named Formulas
Power Apps offers several ways to manage and reuse values, including variables, collections, and context variables. However, modern Canvas Apps now boast a powerful feature called Named Formulas, allowing developers to define reusable logic in a centralized location. This enhancement significantly reduces code duplication, improves readability, increases maintainability, and optimizes application performance. Unlike traditional variables, named formulas automatically recalculate when their dependencies change, much like familiar Excel formulas.
What are Named Formulas?
Named Formulas are reusable expressions defined within the App.Formulas property of a Canvas App. Instead of repeating the same formula across multiple controls, you define it once and reference it throughout your application.
For example, instead of writing:
ColorValue("#005A9C")
on multiple controls, you can define a named formula.
Why Use Named Formulas?
Consider the difference in maintenance:
Without Named Formulas:
Label1.Fill = ColorValue("#005A9C")
Label2.Fill = ColorValue("#005A9C")
Label3.Fill = ColorValue("#005A9C")
If the color needs to change, you must update every single formula. This is tedious and error-prone.
With Named Formula:
First, define the named formula:
PrimaryColor = ColorValue("#005A9C")
Then, all controls can reference it:
Label1.Fill = PrimaryColor
Now, if the color needs to change, only one update to the PrimaryColor named formula is required, propagating the change instantly across the app.
Benefits of Named Formulas
- Reusable Logic: Define complex logic once and use it everywhere.
- Better Performance: Power Fx intelligently determines when calculations need to run, minimizing unnecessary processing.
- Easier Maintenance: Manage and update formulas from a single, central location.
- Cleaner Applications: Significantly reduce redundant code, making your app more organized.
- Faster App Startup: Avoid lengthy
OnStartprocessing by deferring calculations until needed.
How Named Formulas Work
The workflow is straightforward:
- Define a Named Formula within
App.Formulas. - Reference this Named Formula across your app.
- Power Fx automatically recalculates the formula when its dependencies change.
Unlike variables that often require explicit updates using Set(), named formulas automatically update based on data changes.
Enable Named Formulas
To start using this feature, navigate to:
Settings → Upcoming features → Experimental features → Named Formulas.
Ensure the feature is enabled in your environment.
Creating Your First Named Formula
Open your Canvas App, navigate to the App object, and select the Formulas property. Here, you can create your first named formula:
PrimaryColor = ColorValue("#005A9C")
Save your application after defining the formula.
Using a Named Formula
Any control within your app can now reference the named formula. For instance:
Label1.Fill = PrimaryColor
This will apply the defined blue background to the label.
Example 1: Application Theme Color
Define your theme colors:
App.Formulas:
PrimaryColor = ColorValue("#005A9C");
SecondaryColor = ColorValue("#0078D4");
Use them in your controls:
Button1.Fill = PrimaryColor
Label1.Color = SecondaryColor
This ensures consistent application branding.
Example 2: Current User Information
Simplify user data retrieval:
CurrentUser = User()
Then use it like this:
Label1.Text = CurrentUser.FullName
Instead of repeatedly calling User(), you use a single, reusable formula.
Example 3: Company Information
Centralize company-specific details:
App.Formulas:
CompanyName = "CRMONCE"
SupportEmail = "support@crmonce.com"
Reference them easily:
LabelCompany.Text = CompanyName
LabelSupport.Text = SupportEmail
This allows for centralized configuration management.
Example 4: Environment Variables
Manage environment-specific settings:
BaseUrl = "https://api.crmonce.com"
Use it in actions:
Launch(BaseUrl & "/dashboard")
Changing the URL requires only one update in App.Formulas.
Example 5: Data Source Filtering
Centralize complex filtering logic:
ActiveAccounts = Filter(
Accounts,
Status = "Active"
)
Apply the filter to a gallery:
Gallery1.Items = ActiveAccounts
This greatly improves readability and centralizes filtering logic.
Named Formulas vs. Variables
| Feature | Named Formula | Variable |
|---|---|---|
| Automatic Recalculation | Yes | No |
| Reusable | Yes | Yes |
Requires Set() |
No | Yes |
| App Startup Dependency | No | Often |
| Performance | Better | Depends |
| Maintenance | Easier | Moderate |
Named formulas automatically recalculate based on dependencies, whereas variables generally require explicit updates, making named formulas more efficient for read-only derived values.
Named Formula Example for App Settings
Consolidate application metadata:
App.Formulas:
AppVersion = "1.0.0"
AppTitle = "CRMONCE Portal"
DefaultLanguage = "English"
Use these in labels:
LabelVersion.Text = AppVersion
LabelTitle.Text = AppTitle
Performance Advantages
Traditional Apps:
App.OnStart
→ Multiple Set()
→ Initialize Variables
→ Load App
With Named Formulas:
App.Formulas
→ Automatic Evaluation
→ Optimized Calculation
→ Load App Faster
Microsoft recommends using App.Formulas and named formulas to improve app startup performance and allow Power Fx to optimize formula evaluations.
Best Practices
- Use Descriptive Names: Employ clear, meaningful names like
PrimaryColor,CurrentUser, orActiveAccountsinstead of generic ones likex,y, ortest. - Centralize Configuration: Store URLs, colors, application settings, and common filters within
App.Formulas. - Avoid Duplicate Logic: Create reusable formulas for any logic that might be needed in multiple places.
- Use for Read-Only Calculations: Named formulas are ideal for values that are derived from other data and do not change independently.
- Keep Formulas Organized: Group related formulas together logically within the
App.Formulasproperty.
Common Use Cases
- Application Themes: Centralize colors and styles for a consistent look and feel.
- Environment Configuration: Manage URLs, API endpoints, and other environment-specific settings.
- User Information: Store and easily access details about the currently logged-in user.
- Common Filters: Reuse complex filtering logic across different data sources and controls.
- Business Calculations: Consolidate calculations that are used across multiple screens or components.
Real-World Example: Employee Portal
Consider an employee portal:
App.Formulas:
CurrentUser = User();
CurrentYear = Year(Today());
CompanyName = "CRMONCE";
Usage:
WelcomeLabel.Text = "Welcome " & CurrentUser.FullName
Footer.Text = CompanyName & " - " & CurrentUser.Department & " - " & CurrentYear
This approach leads to:
- Centralized Logic
- Better Performance
- Easy Maintenance
Conclusion
Named Formulas represent one of the most valuable enhancements in modern Power Apps development. By defining reusable expressions within App.Formulas, developers can drastically reduce duplicate code, improve application performance, simplify maintenance, and create cleaner, more robust Canvas Apps. Whether you are managing application themes, user information, environment settings, filters, or complex business calculations, Named Formulas offer a scalable and efficient approach that aligns perfectly with Power Fx's declarative programming model.