DAX user-defined functions

Last checked against Microsoft documentation: 14 November 2026

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.tmdl inside 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)

  1. Open your model in Power BI Desktop and switch to DAX query view.
  2. Write the function with DEFINE and FUNCTION (example below).
  3. Test it with an EVALUATE line and run the query.
  4. 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.
  5. Open Model explorer and check that the function appears under Functions.
  6. Call it from a measure, just like a built-in DAX function.

Example 1: a simple function

DAX
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:

DAX
Sales Incl Tax = Finance.AddTax ( [Total Sales], 0.18 )

Example 2: one function calling another

DAX
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 hintChoicesWhat it does
Type (value)AnyVal (default), Scalar, TableThe input is worked out before the function starts
Type (reference)AnyRef, ColumnRef, MeasureRef, TableRef, CalendarRefThe input is passed in unevaluated; always expr mode
Subtype (scalar only)Variant, Int64, Decimal, Double, String, DateTime, Boolean, NumericLimits which values are accepted; implies Scalar
Modeval, exprval: 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

DAX
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 in F ( 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, ISBOOLEAN and similar, and return BLANK () for bad input:
DAX
FUNCTION Util.SafeHalf = ( x ) => IF ( ISNUMERIC ( x ), x / 2, BLANK () )
  • TABLEOF returns the table that holds a column or measure, and NAMEOF returns 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), run EVALUATE 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 CONVERT so 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

  1. Document every function with /// and @param / @returns tags.
  2. Namespace names with a dot, such as Finance.AddTax.
  3. Default to val; use expr only when you control filter context.
  4. Add type hints so wrong calls fail early.
  5. Validate inputs and return BLANK () as a defined fallback.
  6. Put shared business rules in small functions and nest them.

Official sources