top of page

DAX UDF for Power BI

  • dhruvpatel527
  • 19 minutes ago
  • 3 min read

DAX UDFs are now in preview—a powerful new way to package and reuse logic in Power BI models. If you want cleaner, safer, and more maintainable functionality, this is a must-look. Based on Microsoft’s documentation. 

 

What exactly is a DAX UDF? 

  • User-Defined Functions (UDFs) let you define custom functions in DAX using a new FUNCTION keyword. 

  • You can include parameters (scalars, tables, references) and use type checking to make them safer and more predictable. 

  • Once defined and saved in your model, you can call UDFs from: 

    - Measures 

    - Calculated columns 

    - Visual calculations 

    - Other UDFs 

 

How to Enable UDFs 

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. 


Defining & Naming DAX UDFs the Right Way 

DAX UDFs are powerful—but only if you manage them cleanly. Here’s what matters most: 


Where to define 

  • DAX Query View (DQV): Fastest place to write, test, and debug (Quick queries built in). 

  • TMDL View: Script and version-control UDFs like code. 

  • Model Explorer: Browse and organize all functions under the Functions node. 


Naming rules that keep models safe 


  • Functions: 

    - Must be unique

    - Allow alphanumeric + underscores + periods (Finance.RollingAvg), but no leading/trailing/consecutive dots. 

    - No spaces, no special chars, no reserved words (like DEFINE, MEASURE). 

  • Parameters: 

    - Alphanumeric + underscores only. 

    - No dots, no reserved words. 

 

Using DAX query view 

You can define, update, and evaluate user-defined functions in DAX query view. 

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

Example 

DEFINE 
    /// AddTax takes in amount and returns amount including tax 
    FUNCTION AddTax =  
        ( amount : NUMERIC ) => 
            amount * 1.1 
EVALUATE 
{ AddTax ( 10 ) } 
// Returns 11 

 

Saving to the model 

To save a UDF from DAX query view to the model: 

  • Click Update model with changes to save all UDFs in the query. 

  • Or click Update model: Add new function above the defined function to save a single UDF. 

 

ree

Using TMDL view 

You can define and/or update user-defined functions in TMDL view. 

 

General form 

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

Example 

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

 

Saving to the model 

Click the Apply button at the top of the view to save all UDFs in the script to the model. 

ree

 

 

Using TMDL script in a Power BI Project 

UDFs are also included in the semantic model TMDL script when using a Power BI Project. They can be found in functions.tmdl within the definition folder. 

ree

 

 

Using Model explorer 

You can view all user-defined functions in the model from Model explorer under the Functions node. 

ree

 

In Dax query view, you can use Quick queries in the right-click menu of a UDF within Model explorer to easily define and evaluate functions. 

ree

 

In TMDL view, you can drag and drop functions into the canvas or use Script TMDL to in the right-click menu of a UDF within Model explorer to generate scripts. 

ree

 

Using a user-defined function 

Once a UDF is defined and saved to the model, you can call it from measures, calculated columns, visual calculations, and other UDFs. This works the same as calling built-in DAX functions. 


Calling a UDF in a measure 

Use a UDF in a measure to apply reusable logic with full filter context. 

Total Sales with Tax = AddTax ( [Total Sales] ) 
ree

Calling a UDF in a calculated column 

UDFs can be used in a calculated column to apply reusable logic to every row in a table. 

Sales Amount with Tax = CONVERT ( AddTax ( 'Sales'[Sales Amount] ), CURRENCY ) 
ree

 

Calling a UDF in a visual calculation 

You can use UDFs in a visual calculation to apply logic directly to the visual. 

Sales Amount with Tax = AddTax ( [Sales Amount] ) 
ree

Conclusion 


DAX user-defined functions (or UDFs) are a very valuable new addition to semantic models. They let you re-use DAX logic in your model, making your code easier to maintain and change. You can also copy these functions between models so you can re-use DAX code between projects. It’s worthwhile to evaluate whether you have redundant or repeated code in your model that you might refactor into functions. There are a lot of benefits.

 
 
 
bottom of page