With user-defined functions, I believe we Power BI developers can finally call ourselves real programmers without hesitation. We have code (TMDL), we have functions, variables, and much, much more.
What are those UDFs?
Just the coolest feature in Power BI!
You know those measures you create to make your calculations cleaner, and you use them only to calculate other measures. You never put them in a visual. They are just there to calculate other stuff.
Now you can define functions with your DAX and use them in all your measures. And it gets better: they can take parameters. That means you can use the same function for different use cases.The best part is you get control over parameter evaluation: scalar now, or evaluated later in filter context.
Why you might want them:
- They are a new way to make your semantic models cleaner and easier to maintain.
- They can improve performance for repeated logic in complex models.
- You can reuse them in different semantic models and projects.
How to define UDFs and where they live
There are many ways to define UDFs.
Option 1: Model view
You can do it from Model view. Go to:
Data -> Model -> Functions
Then click the (…) next to Functions and select New function.
This is also where the functions live and where you can find all of your functions.
MyVeryFirstFunction = (parameter1, parameter2) => parameter1 + parameter2
Option 2: DAX Query view
From the DAX Query view you can run a query like this:
DEFINE FUNCTION MyVeryFirstFunction =
(parameter1, parameter2) =>
parameter1 + parameter2
Then update the model with changes. Else the Function will only exist in the current query. Use EVALUATE to test your function.
Existing functions also come with a handy quick queries menu under “…” -> “Quick queries”, which makes it easier to work with them directly in the model. But only if you are in the DAX Query View.

Option 3: TMDL view
Or from TMDL view, by running a script like this:
createOrReplace
Function `MyVeryFirstFunction` = ```
(
parameter1,
parameter2
)
=> parameter1 + parameter2
When working with TMDL View scripts, pay close attention to tabs and indentation, as small formatting inconsistencies can quickly lead to errors.
Similar to DAX Query View, in the TMDL View there are also additional actions in the functions menu.

What is a UDF made of?
FunctionName =
/// Function description
(
/// Parameter description
Parameter : Type SubType ParameterMode
)
=> FunctionBody
Here comes a description of every part, including what is required.
FunctionName
The name you give to the function.
Required.
Function description
A comment that explains what the function does.
Optional, but highly recommended once you have more than a few functions.
Parameter list
Everything inside the parentheses.
Optional. A function can have zero parameters.
If you define parameters, the parameter name is required for each parameter.
Parameter
The parameter name you reference inside the function body.
Required only if you define parameters.
Type
An optional hint that describes what kind of argument the parameter accepts.
AnyVal – Accepts either a scalar value or a table expression.
Scalar – Accepts a single value, like a number, text, date, boolean, or a scalar expression returning one value.
Table – Accepts a table expression.
AnyRef – Accepts a reference type (a generic “reference” category).
CalendarRef – Accepts a calendar reference.
ColumnRef – Accepts a column reference.
MeasureRef – Accepts a measure reference.
TableRef – Accepts a table reference.
SubType
An optional hint that applies only when Type is Scalar.
Variant
Int64
Decimal
Double
String
DateTime
Boolean
Numeric
If you specify a SubType, your input will be cast to that subtype when the function is called. That gives you more predictable behavior than relying on implicit conversions.
Here is a simple example:
DEFINE
FUNCTION AddInt64 =
(
x : SCALAR INT64,
y : SCALAR INT64
)
=>
x + y
EVALUATE
{
AddInt64 ( 3.5, 4.5 )
}
Because the parameters are declared as Int64, the inputs are cast to integers before the addition happens. In this example that means 3.5 becomes 4 and 4.5 becomes 5, so the result is 9. With the type DECIMAL it would result in 8.
ParameterMode
An optional hint that controls when an argument is evaluated.
VAL
The argument is evaluated immediately at the call site, and the function receives the resulting value. This is useful when you want to freeze the result before the function applies its own filter context.
EXPR
The argument is passed as an expression and evaluated inside the function body. This is useful when you want the function’s internal filter context to affect how the argument is calculated.
FunctionBody
The DAX expression after the arrow that returns the result.
Required.
Going deeper: questions I had when I first heard about UDFs
Ok, we covered the basics of UDFs. Let’s dig deeper. There are some questions I was interested in as I first heard of UDFs.
How to create a library for my organisation?
For now there is no tool or feature for that. The best way is to create a Git repo with TMDL snippets, so they can be copied into TMDL View or DAX Query View in Power BI.
Can I create and change UDFs in the TMDL file?
Yes. My code-loving heart made a happy little flip.
The functions in the Power BI Project are easy to find and they are all in one place. You can copy them there directly.

Can I save them in a template?
Again yes. You can create a template with your useful UDFs and have it on hand every time you create a semantic model from that template.
Can I use them when I am a report builder and cannot edit the semantic model?
Sadly, when I connected to a semantic model with a brand new report I couldn’t see any functions in the data source model. I also couldn’t use a function when I was creating a measure in a report.
I hope it will change, because I see great potential in self service scenarios for that.
So that’s it for now. If you try UDFs, I would genuinely love to hear how you use them. What patterns do you build first, and what do you wish Power BI handled better?

Like it, very clear
Do you have any further detail about @param I saw put in function description in a tutorial I can’t remind well?
Hey Marco,
yes you can use:
/// @param {ParameterType} ParameterName – ParameterDescription
to add a parameter description. Good point!