DAX user-defined functions (UDFs) with examples

How to write DAX user-defined functions with the FUNCTION keyword, parameters and type hints (Scalar, Table, ColumnRef, MeasureRef), val and expr passing modes, defaults, where to define them (DAX query view, TMDL view), and examples for tax, growth and safe percentages.

Short answer: a user-defined function (UDF) packages DAX logic under a name, with parameters, and you call it like a built-in function: AddTax ( [Revenue] ). Define it with FUNCTION Name = ( param : type ) => body in DAX query view or TMDL view, and save it to the model. Parameters can carry type hints (Scalar with subtypes like Numeric or String, Table, or references like ColumnRef and MeasureRef) and a passing mode: val evaluates the argument once before the call, expr passes the expression to be evaluated inside the function, in its context.

Before UDFs, the same "growth against last year" logic was copied into twenty measures, each slightly different. A function writes it once; every measure calls it, and a fix lands everywhere.

What does a function look like?

Based on Microsoft's tax example, in DAX query view:

DEFINE
    /// AddTax takes in amount and returns amount including tax
    /// @param {NUMERIC} amount - The pre-tax value
    /// @param {NUMERIC} [taxRate] - Optional tax rate, default 0.1
    FUNCTION AddTax =
        ( amount : NUMERIC, taxRate : NUMERIC = 0.1 ) =>
            amount * ( 1 + taxRate )

EVALUATE
{ AddTax ( 10 ), AddTax ( 10, 0.2 ) }
// Returns 11 and 12

The general form:

FUNCTION <Name> = ( <param> : <type> <subtype> <mode> = <default>, ... ) => <body>

Save it with Update model: Add new function (CodeLens above it) or Update model with changes. In TMDL view the keyword is lowercase function, and in a Power BI project the functions live in functions.tmdl.

Which parameter types are there?

HintAcceptsPassing
AnyVal (default)a scalar or a tableval
Scalar, with subtype Variant, Int64, Decimal, Double, String, DateTime, Boolean, Numericone valueval or expr
Tablea tableval or expr
AnyRef, ColumnRef, MeasureRef, TableRef, CalendarRefa referenceexpr only
Parameter type hints
  • val (eager): the argument is evaluated once, in the caller's context, and the value is passed in.
  • expr (lazy): the expression is passed unevaluated, and the function evaluates it where it wants, for example inside CALCULATE with another filter. Time intelligence functions need this.

A function takes 0 to 256 parameters. A default makes a parameter optional: ( rate : NUMERIC = 0.2 ).

What are useful examples?

Growth against the same period last year, for any measure:

FUNCTION GrowthVsLY =
    ( m : SCALAR NUMERIC EXPR, dateCol : COLUMNREF ) =>
        VAR Cur = m
        VAR LY = CALCULATE ( m, SAMEPERIODLASTYEAR ( dateCol ) )
        RETURN DIVIDE ( Cur - LY, LY )
Revenue Growth % = GrowthVsLY ( [Revenue], 'Date'[Date] )
Orders Growth % = GrowthVsLY ( [Orders], 'Date'[Date] )

m must be expr: with val, it would be evaluated once in the current context, and CALCULATE could not shift it to last year (the same trap as with variables).

A share of a parent level:

FUNCTION ShareOf =
    ( m : SCALAR NUMERIC EXPR, col : COLUMNREF ) =>
        DIVIDE ( m, CALCULATE ( m, REMOVEFILTERS ( col ) ) )

Share of Category = ShareOf ( [Revenue], 'Product'[Product] )

How do you name and document them?

  • Names may use letters, digits, underscores and dots for namespaces (Finance.GrowthVsLY); no spaces; not a built-in function name or reserved word.
  • Document with /// above the function, with JSDoc-style tags like @param.
  • INFO.USERDEFINEDFUNCTIONS() lists the functions in a model.

What are the limits?

From Microsoft's list: functions cannot be hidden, placed in display folders or translated; models without tables do not support them; DAX query view has no "define with references" for them; in a live-connected model they do not show in Model explorer.

What goes wrong?

  • val where expr is needed. The argument is fixed before the call; context changes inside do nothing.
  • Missing type hints. Without hints a parameter is AnyVal (val); add hints so wrong arguments fail early.
  • One function per measure. Write functions for logic that repeats; a single-use function only adds a layer.

Where can you check this?

Where do you start?

Find one calculation copied into several measures (growth, share, safe percentage), write it as a function in DAX query view, and call it from two measures. DAX queries explains the editor you write it in.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.