Bhubaneswar, Odisha, India
+91-8328865778
support@softchief.com

User-Defined Function (UDF) in Power Apps Canvas App – EMI Calculator Example

User-Defined Function (UDF) in Power Apps Canvas App – EMI Calculator Example

In this blog you will know about User-Defined Function (UDF) in Power Apps Canvas App with a EMI Calculator Example

Watch the video below or scroll to read.

In Power Apps Canvas Apps, you often need to reuse complex logic across multiple places. While there’s no traditional function structure like in programming languages, you can simulate User-Defined Functions (UDFs) using Power Fx features like With() and Set().

In this blog, I’ll show you how to build a Loan EMI Calculator using Power Fx to demonstrate how UDFs can be effectively created and used.


✅ Use Case: EMI Calculator in a Banking Canvas App

Imagine a loan officer using a Canvas App to quickly calculate the Equated Monthly Installment (EMI) for a customer based on:

  • 🏦 Principal amount (P)
  • 📈 Interest rate per annum (R)
  • 📆 Loan tenure in months (N)

The formula for EMI is: EMI=P×R×(1+R)N(1+R)N−1EMI = \frac{P \times R \times (1 + R)^N}{(1 + R)^N – 1}EMI=(1+R)N−1P×R×(1+R)N​

Where:

  • R=Annual Interest Rate12×100R = \frac{\text{Annual Interest Rate}}{12 \times 100}R=12×100Annual Interest Rate​ (monthly rate)

🛠 Canvas App Setup

🎛 Controls Used

ControlNamePurpose
Text InputtxtPrincipalPrincipal Amount
Text InputtxtInterestInterest Rate (annual %)
Text InputtxtTenureLoan Tenure (months)
ButtonbtnCalculateTriggers EMI calculation
LabellblEMIDisplays the result

🧮 Simulating a UDF with With() and Set()

Canvas Apps don’t support custom functions directly. However, we can simulate a UDF using With() blocks to define local variables and reuse them like function parameters.

📌 Formula on btnCalculate.OnSelect:

powerfxCopyEditWith(
    {
        P: Value(txtPrincipal.Text),
        R: Value(txtInterest.Text) / 12 / 100,
        N: Value(txtTenure.Text),
        factor: Power(1 + Value(txtInterest.Text) / 12 / 100, Value(txtTenure.Text))
    },
    Set(
        EMI,
        Round((P * R * factor) / (factor - 1), 2)
    )
)

📌 Label lblEMI.Text:

powerfxCopyEdit"Monthly EMI: ₹" & Text(EMI, "#,##0.00")

🧠 Why This Works as a UDF

  • With() allows local variable scoping, just like a function body.
  • Set() stores the result globally for reuse.
  • You can reuse this block in multiple buttons or screens, like a function call.

You can even wrap this logic in a component for reusability across screens and apps!


🧪 Try It Out

Suppose the user enters:

  • Principal: ₹500,000
  • Interest: 10% p.a.
  • Tenure: 60 months

The calculated EMI would be:

yamlCopyEditMonthly EMI: ₹10,623.00

🚀 Bonus Tip: Make It a Reusable Component

To truly mimic a UDF:

  1. Create a custom component.
  2. Accept Principal, InterestRate, and Tenure as input properties.
  3. Use the same formula in the component.
  4. Expose EMI as an output property.

Now your “UDF” is reusable and drag-and-drop friendly across your Canvas App ecosystem!


✅ Conclusion

While Canvas Apps don’t have traditional UDFs, Power Fx gives us the tools to build modular, reusable logic using:

  • With() for scoped variables,
  • Set() for global storage,
  • and components for reuse.

The EMI Calculator is just one example—you can apply this pattern to tax calculations, discount logic, currency conversion, and more.

Hope it helps.