A DAX user-defined function (UDF) is a calculation you write once, give a name and inputs, and reuse everywhere in the model. Microsoft made UDFs generally available in Power BI Desktop and the Power BI service with the June 2026 release.
Why it matters
- Before UDFs, the same rule (a tax, a discount, a fiscal-year test) was copied into many measures. Now you fix it in one place.
- A UDF is a real model object. It shows in Model explorer under the Functions node and is stored in
functions.tmdlinside a Power BI Project. - You can call a UDF from measures, calculated columns, visual calculations and other UDFs.
Step by step (everything happens in DAX query view)
- Open your model in Power BI Desktop and switch to DAX query view.
- Write the function with
DEFINEandFUNCTION(example below). - Test it with an
EVALUATEline and run the query. - Save it to the model. Use Update model with changes to save every function in the query, or Update model: Add new function to save one.
- Open Model explorer and check that the function appears under Functions.
- Call it from a measure, just like a built-in DAX function.
Example 1: a simple function
DEFINE
/// Adds tax to a price
/// @param {NUMERIC} price - amount before tax
/// @param {NUMERIC} rate - tax rate, for example 0.18
/// @returns the price including tax
FUNCTION Finance.AddTax = ( price : NUMERIC, rate : NUMERIC ) =>
price * ( 1 + rate )
EVALUATE { Finance.AddTax ( 100, 0.18 ) }The query returns 118. After saving the function, a measure can use it:
Sales Incl Tax = Finance.AddTax ( [Total Sales], 0.18 )Example 2: one function calling another
DEFINE
FUNCTION Finance.Discount = ( price : NUMERIC, pct : NUMERIC ) =>
price * ( 1 - pct )
FUNCTION Finance.NetPrice = ( price : NUMERIC, pct : NUMERIC, rate : NUMERIC ) =>
Finance.AddTax ( Finance.Discount ( price, pct ), rate )
EVALUATE { Finance.NetPrice ( 100, 0.10, 0.18 ) }This returns 106.2: a 10% discount first (90), then 18% tax. Nesting is supported; recursion is not.
The part that trips people up: type hints and evaluation mode
Each input can carry a hint in the form type subtype mode. Any part may be left out. A bare name means "any value, evaluated first". A function can have 0 to 256 inputs.
| Part of the hint | Choices | What it does |
|---|---|---|
| Type (value) | AnyVal (default), Scalar, Table | The input is worked out before the function starts |
| Type (reference) | AnyRef, ColumnRef, MeasureRef, TableRef, CalendarRef | The input is passed in unevaluated; always expr mode |
| Subtype (scalar only) | Variant, Int64, Decimal, Double, String, DateTime, Boolean, Numeric | Limits which values are accepted; implies Scalar |
| Mode | val, expr | val: worked out once, in the caller's context, before the body runs. expr: worked out inside the body, possibly several times, using only the filter context |
Plain rule: use val unless you must control filter context. Then use expr.
Example 3: why mode matters
DEFINE
FUNCTION Demo.RowsVal = ( t : TABLE VAL ) =>
COUNTROWS ( CALCULATETABLE ( t, ALL ( 'Date' ) ) )
FUNCTION Demo.RowsExpr = ( t : TABLE EXPR ) =>
COUNTROWS ( CALCULATETABLE ( t, ALL ( 'Date' ) ) )
EVALUATE
{
CALCULATE ( Demo.RowsVal ( 'Sales' ), 'Date'[Year] = 2025 ),
CALCULATE ( Demo.RowsExpr ( 'Sales' ), 'Date'[Year] = 2025 )
}The VAL version receives the Sales table already filtered to 2025, so ALL ( 'Date' ) inside the function changes nothing. The EXPR version re-evaluates the table inside the function, so ALL ( 'Date' ) removes the year filter. This follows the example in Microsoft's article; run it on your own model to see the two row counts.
Safe inputs and optional inputs
- Give an input a default with
= expression. Skip an optional input by leaving the slot empty, as inF ( 1,, 3 ). - A default can only use names visible where the function is defined, and it cannot use another optional input.
- Check input types with
ISNUMERIC,ISSTRING,ISDATETIME,ISBOOLEANand similar, and returnBLANK ()for bad input:
FUNCTION Util.SafeHalf = ( x ) => IF ( ISNUMERIC ( x ), x / 2, BLANK () )TABLEOFreturns the table that holds a column or measure, andNAMEOFreturns an object's name. Use them in generic helpers so you never hard-code table names.- To list your functions, run
EVALUATE INFO.FUNCTIONS("ORIGIN", "2"). For full metadata (needs write permission), runEVALUATE INFO.USERDEFINEDFUNCTIONS().
Naming rules
- Function names must be unique in the model. Dots are allowed for namespaces (
Finance.AddTax) but not at the start, at the end or twice in a row. Otherwise use letters, digits and underscores. - Input names use letters, digits and underscores only. No periods. No reserved words.
- Use
///comments for descriptions so IntelliSense shows them. Normal//and/* */comments do not appear there.
Watch-outs (from Microsoft's limits list)
- No recursion, no overloading, no declared return type, and no descriptions for single inputs.
- A UDF cannot be hidden, placed in a display folder or combined with translations.
- Models without tables do not support UDFs.
- Object-level security does not carry over. A UDF that uses a secured measure is not secured itself. Never put secured names in a function's name or description.
- Composite-model measures cannot call UDFs declared in the source model. In a live connection, source UDFs do not show in Model explorer.
- A UDF cannot return an enum, so built-in functions that expect an enum cannot take a UDF there.
- In a calculated column, wrap the result in
CONVERTso the type stays the same on every row. - In a visual calculation, you can only pass fields that are on the visual.
- Write columns in full, as
'Table'[Column]. An unqualified name is treated as a measure, which can break dependency tracking. - IntelliSense is weaker in the visual calculation bar, in SQL Server Management Studio and in the TMDL VS Code extension. Some valid code may show a red underline.
Best practices
- Document every function with
///and@param/@returnstags. - Namespace names with a dot, such as
Finance.AddTax. - Default to
val; useexpronly when you control filter context. - Add type hints so wrong calls fail early.
- Validate inputs and return
BLANK ()as a defined fallback. - Put shared business rules in small functions and nest them.