Power Apps  

How to Use User Defined Function (UDF) in Power Apps

Introduction

In Microsoft Power Apps, a User Defined Function (UDF) allows you to create your own reusable formula that can be called multiple times within your app.

Instead of writing the same formula again and again, you define it once and reuse it anywhere in the app just like built-in functions (Sum, If, LookUp, etc.).

⚑ UDFs improve readability, reusability, and maintenance of your app.

βœ… 1. What is a User Defined Function?

A User Defined Function (UDF) is a custom function you create using:

FunctionName(Parameter1: Type, Parameter2: Type): ReturnType = Formula

You define it in the App β†’ Formulas property.

πŸ‘‰ App β†’ Formulas

2

Steps:

  1. Select App (top of the Tree view).

  2. Go to the Formulas property in the formula bar.

  3. Define your function there.

That’s the setting used to create and manage User Defined Functions in Power Apps.

πŸ‘‰ App β†’ Setting

Settting -> Updates -> Experimental -> User-define types : toggle turn ON

TinyTake17-02-2026-04-26-13

βœ… 2. Simple Example (Beginner Level)

🎯 Scenario:

You want a reusable function to calculate a 10% discount price.

πŸ”Ή Step 1: Go to

App β†’ Formulas

Add:

CalculateDiscount(Price: Number): Number =
    Price - (Price * 0.1)

πŸ”Ή Step 2: Use it anywhere

CalculateDiscount(200)

πŸ‘‰ Result: 180

Now instead of writing:

Price - (Price * 0.1)

You simply call:

CalculateDiscount(Price)

Much cleaner βœ…

βœ… 3. Medium Example – Full Name Formatter

🎯 Scenario:

You want to format a user’s full name properly.

πŸ”Ή Define function

FormatFullName(FirstName: Text, LastName: Text): Text =
    Proper(FirstName) & " " & Proper(LastName)

πŸ”Ή Use it

FormatFullName("Ketan", "Sathavara")

πŸ‘‰ Output: Ketan Sathavara

βœ… 4. Long Real-World Example (Advanced Business Case)

🎯 Scenario

You’re building a Sales Management App.

You need to calculate commission based on sales amount and role.

πŸ’Ό Commission Rules

RoleConditionCommission
ManagerAny15%
SeniorSales > 10,00012%
SeniorSales ≀ 10,0008%
JuniorAny5%

πŸ”Ή Step 1: Create the Function

Go to App β†’ Formulas

CalculateCommission(
    SalesAmount: Number,
    Role: Text
): Number =
Switch(
    Role,
    "Manager",
        SalesAmount * 0.15, 
    "Senior",
        If(
            SalesAmount > 10000,
            SalesAmount * 0.12,
            SalesAmount * 0.08
        ),  
    "Junior",
        SalesAmount * 0.05,
    0
)

πŸ”Ή Step 2: Use in Gallery Label

Assume your data source is SalesData.

Inside a label:

CalculateCommission(ThisItem.SalesAmount, ThisItem.Role)

πŸ”Ή Step 3: Total Commission Example

Sum(
    SalesData,
    CalculateCommission(SalesAmount, Role)
)

Now your business logic is:

  • Centralized

  • Reusable

  • Easy to update

If commission rules change, you update one place only βœ…

βœ… 5. Even More Advanced Example (With Multiple Conditions)

🎯 Scenario

You want a function that:

  • Applies tax

  • Applies discount

  • Adds shipping

  • Returns final invoice total

CalculateInvoiceTotal(
    SubTotal: Number,
    DiscountPercent: Number,
    TaxPercent: Number,
    ShippingCost: Number
): Number =

With(
    {
        DiscountAmount: SubTotal * DiscountPercent,
        AfterDiscount: SubTotal - (SubTotal * DiscountPercent),
        TaxAmount: (SubTotal - (SubTotal * DiscountPercent)) * TaxPercent
    },
    AfterDiscount + TaxAmount + ShippingCost
)

πŸ”Ή Use it:

CalculateInvoiceTotal(1000, 0.1, 0.15, 50)

βœ… 6. Why UDF is Powerful in Large Apps

Without UDF ❌

  • Same formula repeated in 20 screens

  • Hard to maintain

  • Error-prone

With UDF βœ…

  • One definition

  • Clean screens

  • Enterprise-level structure

  • Easy debugging

⚠️ Important Notes

  • UDFs are defined at App level

  • They support typed parameters (Text, Number, Boolean, etc.)

  • They improve performance by reducing duplicated logic

  • Currently supported in modern Power Apps formula language

🎯 When Should You Use UDF?

Use UDF when:

  • Logic is repeated 3+ times

  • Business rules may change

  • Calculations are complex

  • You want professional-level app architecture

πŸ”Ή Difference: UDF vs Variables

UDFVariable
Reusable logicStores value
Can accept parametersCannot accept parameters
Dynamic calculationStatic until changed

πŸ”Ή Important Notes

  • UDFs are created using Named Formulas

  • They are declarative (auto-update when inputs change)

  • They must return ONE value

  • You can define variable types (Number, Text, Boolean, Record, Table)

  • They do not use Set() or UpdateContext()

Conclusion

A User Defined Function (UDF) in Power Apps:

  • Is created using Named Formulas

  • Accepts parameters

  • Returns a value

  • Makes apps cleaner and scalable