Edit

Share via


DAX user-defined functions (preview)

Note

DAX user-defined functions are currently in preview.

Data Analysis Expressions (DAX) user-defined functions (UDFs) let you package reusable, parameterized DAX logic into your models making your DAX code easier to write, maintain, and share. Instead of repeating formulas across measures, calculated columns, and visuals, UDFs bring programming-style flexibility to your semantic models, letting you define functions once and use them everywhere DAX is supported. To learn more, see DAX user-defined functions.

Why use user-defined functions?

  • Reusability and consistency: Define a calculation once and reuse it everywhere.
  • Maintainability: Update logic in one place to fix or evolve rules.
  • Safer authoring: Optional type hints and type check helpers support predictable, error-resistant code.
  • First-class model objects: UDFs live in the model and can be viewed in Model Explorer.

Get started

To try UDFs in Desktop:

  1. Go to File > Options and settings > Options.
  2. Select Preview features and check DAX user-defined functions.
  3. Select OK and restart Power BI Desktop.

Define a function

You can define a user-defined function in Power BI Desktop using DAX query view (DQV) or TMDL view.

General syntax

The general syntax for a UDF is:

/// Optional description above the function
FUNCTION <FunctionName> = ( <ParameterName>: <ParameterType>, ... ) => <FunctionBody>

Example: Simple tax function

Here's a simple example in DQV that adds tax onto the given amount. You can also evaluate UDFs in DQV.

DEFINE
    /// AddTax takes in amount and returns amount including tax
    FUNCTION AddTax = (
            amount : NUMERIC
        ) =>
        amount * 1.1

EVALUATE
{ AddTax ( 10 ) }
// Returns 11

After a UDF is defined, you can update the model or use the code lens to add the function to your model.

Screenshot of DAX query view in Power BI Desktop, highlighting two locations where you can save a user-defined function. The first is the Update model with changes button at the top of the view. The second is a status line in the code editor labeled Update model: Add new function.