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
| Control | Name | Purpose |
|---|---|---|
| Text Input | txtPrincipal | Principal Amount |
| Text Input | txtInterest | Interest Rate (annual %) |
| Text Input | txtTenure | Loan Tenure (months) |
| Button | btnCalculate | Triggers EMI calculation |
| Label | lblEMI | Displays 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:
- Create a custom component.
- Accept
Principal,InterestRate, andTenureas input properties. - Use the same formula in the component.
- Expose
EMIas 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.








