This summary is produced by the author, and not by AI.
DAX User-Defined Functions (UDFs, which we refer to henceforth as functions) are a new feature in Power BI semantic models that let you centrally define and then re-use DAX calculations, logic and values in different places. A DAX function is an object type that lets you specify reusable code that you can use in the expressions other DAX objects, like measures.
DAX functions have a lot of benefits for new and existing semantic models:
This article serves as a brief introduction and explainer so you can get started using them. We’ve also produced a short, free course about DAX functions, including tips about how to write and maintain good functions, and some common use-cases or scenarios that you might want to explore. Check out the course on Tabular Editor Learn.
A DAX function has several parts that you should know about, depicted in the below diagram:
Type Hints are a more complex and nuanced concept. We and others will explain them in more detail, but in this simple introduction, you just need to know that they are important in three main scenarios:
Int64).\ANYREF or the ParameterMode is EXPR, then you can change the filter context, i.e. in CALCULATE or CALCULATETABLE. If not, then a SCALAR or VAL parameter will work like a variable.These are more complex concepts, and we cover them in some more detail in our introduction course on Tabular Editor Learn. However, we will also elaborate on them in other articles with specific examples.
Functions can output either scalar values or tables.
Once you create a function, you can reference it in another DAX expression, like this:
You can reference it in object types such as:
It’s easy to create a function in a semantic model, either in Power BI Desktop or in Tabular Editor. Note that your semantic model must have a compatibility level of 1702 or higher. Power BI might upgrade this for you automatically, but you could also do it manually in Tabular Editor, too.
In Power BI, you can create functions either in the TMDL view or the DAX query view. To use the TMDL view, you just have to specify a new TMDL script with the appropriate syntax for a DAX function, then review and apply the script changes. The TMDL view is ideal for copying functions between semantic models. However, if you’ll use Power BI Desktop alone to create and manage your functions, we recommend that you use the DAX query view. The DAX query view has more code assistance support, like some autocomplete and syntax highlighting. Plus, you can write queries to test your functions before you add them to your model. In addition to the DAX query view, you can also create and manage functions in Tabular Editor. In Tabular Editor, functions now have their own folder. Creating a function is as easy as adding a new DAX measure: just right-click to create a new function, writing the DAX in the expression editor, and save to view the results. That’s it!
You can also define and validate functions in DAX queries of Tabular Editor 3, as well.
DAX functions are something that we have anticipated (and wished for) for a long time. As such, we’ve had plenty of time to think about features that we’d want to support it. We’ve made sure that there is plenty of code assistance to help you write and use your functions. You can see a demonstration of this below:
Here are a few highlights:
Shift+F12 or right-click and select “Show Dependencies” to see where your functions are used in the semantic model.This is just the start, because we will also support out-of-the-box libraries from daxlib.org by SQLBI, so you can find and re-use DAX functions, easily. In future content, we’ll share some common use-cases and scenarios for DAX functions that you might find helpful.
DAX user-defined functions (or UDFs) are a very valuable new addition to semantic models. They let you reuse DAX logic in your model, making your code easier to maintain and change. You can also copy these functions between models so you can reuse 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.
Try reusable DAX logic in Tabular Editor 3.
Give Tabular Editor a spin