Skip to content
Back to Journal

Financial functions in Snowflake using Python UDFs

Snowflake financial functions illustration with charts, interest symbols and the Snowflake logo

When you’re working with lending, credit, or repayment modelling, two calculations come up all the time:

  • What should the payment be for a given loan? → PMT
  • What interest rate does this repayment schedule imply? → RATE

Excel has these functions baked in. Most financial libraries expose them too.
Snowflake, however, leaves it to you to build the logic yourself — which is fair, because every organisation has its own flavour of assumptions, rounding rules, and thresholds.

In this article, I’ll show you how to implement both of these functions directly inside Snowflake using Python UDFs:

  • A clean, vectorised PMT Calculator for repayment estimation
  • An iterative Interest Rate Calculator that solves for the implicit rate

The end result: your data teams can run financial calculations directly inside Snowflake, without exporting to Excel, Pandas, or downstream services.

Why Use Python UDFs for Financial Math?

Snowflake SQL is great, but long iterative loops and exponent-heavy formulas become unreadable fast.

Python UDFs solve this problem nicely:

  • They’re fast for numeric workloads
  • They run inside Snowflake (no data movement)
  • They’re version-controlled and easy to test
  • You can encapsulate finance maths in a way that analysts and engineers actually enjoy using

Think of them as your internal “finance toolkit” running natively in the warehouse.

1. PMT Calculator in Snowflake

Let’s start with the simpler one: the periodic payment (PMT) calculation.

The formula calculates how much the borrower needs to pay each period (month) given:

  • rate — periodic interest rate (e.g., annual ÷ 12)
  • nper — number of payments
  • pv — present value (loan amount)
  • fv — desired balance after final payment
  • type — payment timing (0 = end of period, 1 = beginning)

Here’s the full Snowflake UDF:

SQL
CREATE OR REPLACE FUNCTION PUBLIC.PMT(
    RATE NUMBER(38, 6),
    NPER INT,
    PV NUMBER(38, 2),
    FV NUMBER(38, 2),
    END_OR_BEGINNING NUMBER(38,0)
)
RETURNS NUMBER(38, 2)
LANGUAGE PYTHON
RUNTIME_VERSION = '3.12'
HANDLER = 'get_pmt'
AS '
def get_pmt(
    rate: float, nper: int, pv: float, fv: float = 0.0, end_or_beginning: int = 1
) -> float:
    temp = (1 + rate) ** nper
    fact = (1 + rate * end_or_beginning) * (temp - 1) / rate
    pmt = (fv + pv * temp) / fact
    return pmt
';

Using PMT function in SQL:

SQL
SELECT
    25000 AS AMOUNT,
    0.08/12 AS RATE,
    60 AS NPER,
    PUBLIC.PMT(0.08/12, 60, 25000, 0, 1) AS MONTHLY_PAYMENT;
Plain text
### Returns something like:
| AMOUNT | RATE (monthly) | NPER | PMT    |
| ------ | -------------- | ---- | ------ |
| 25000  | 0.0066667      | 60   | 503.55 |

This aligns perfectly with Excel’s =PMT().

2. Calculating the Interest Rate (RATE) in Snowflake

While PMT is straightforward, calculating the interest rate from known payments is much harder. There's no simple algebraic manipulation—you have to solve for r iteratively.

To do that inside Snowflake, we use a binary-search based Python UDF:

SQL
-- Calculate Interest Rate in SQL
CREATE OR REPLACE FUNCTION PUBLIC.RATE(
    "PMT" FLOAT,
    "AMOUNT" FLOAT,
    "NPER" FLOAT,
    "THRESHOLD" FLOAT,
    "ITER_LIMIT" INTEGER
)
RETURNS NUMBER(38, 6)
LANGUAGE PYTHON
RUNTIME_VERSION = '3.12'
HANDLER = 'get_loan_interest'
AS '
def get_loan_interest(P, A, n, threshold=0.00001, iter_limit=10000):
    def calc_diff(r, P, A, n):
        return P - ((r / 12 * A) / (1 - (1 + r / 12) ** (-n)))
    lower = 0.00001
    upper = 0.5
    interest = (lower + upper) / 2
    for number_of_trials in range(iter_limit):
        diff = calc_diff(interest, P, A, n)
        if abs(diff) < threshold:
            return interest
        if diff > 0:
            lower = interest
        else:
            upper = interest
        interest = (lower + upper) / 2
    return interest
';

Using RATE function in SQL:

SQL
SELECT
    503.55 AS PMT,
    25000 AS AMOUNT,
    60 AS NPER,
    PUBLIC.RATE(PMT, AMOUNT, NPER, 0.00001, 10000) AS INTEREST_RATE;
Plain text
| PMT    | AMOUNT | NPER | INTEREST_RATE |
| ------ | ------ | ---- | ------------- |
| 503.55 | 25000  | 60   | 0.66665       |

This matches the PMT example above — it solves the inverse.

Performance Considerations

A few tips to keep these functions efficient:

  • Use them on batches, not row-by-row in massive loops
  • Set appropriate THRESHOLD for interest convergence
  • The PMT function is extremely fast — it’s just pure arithmetic
  • Make sure you validate RATE with a handful of Excel test cases before production use

Snowflake’s Python runtime handles this workload very comfortably.

Further Reading

If you’re new to Snowflake user-defined functions or want a deeper dive into the mechanics behind SQL and Python UDFs, the official Snowflake Quickstart is a great place to start. It walks through permissions, setup, creation and execution of UDFs step-by-step:

Snowflake Quickstart — Getting Started with User-Defined SQL Functions

This guide is helpful if you want to understand how Snowflake compiles, executes and manages UDFs under the hood, or if you’re planning to roll out a broader library of analytical or financial functions across your organisation.

Snowflake + Python UDFs give you the tools to bring financial computation right into your data platform. PMT and Interest Rate (RATE) calculations are essential building blocks for analysts, product teams, and data engineers working in lending or credit analytics.