Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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:
- Go to File > Options and settings > Options.
- Select Preview features and check DAX user-defined functions.
- 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.