When working with Power BI, it is common to repeat the same business logic across multiple calculations and reports. As semantic models grow, this repetition makes solutions harder to manage and increases the possibility of inconsistent results.
To address this challenge, Microsoft introduced DAX User‑Defined Functions (UDFs) as a preview feature. This capability allows developers to define reusable, parameter‑based logic directly in DAX, similar to how functions work in programming languages.
What are DAX User‑Defined Functions?
DAX User‑Defined Functions allow you to create your own custom functions in DAX. These functions accept input values, apply a calculation or business rule, and return a result.
Once defined, a DAX UDF becomes part of the semantic model and can be reused in calculated columns, measures, visual calculations, and even inside other DAX expressions. This makes models cleaner, easier to maintain, and more consistent.
Because this feature is still in preview, it must be enabled manually in Power BI Desktop.
Enabling DAX User‑Defined Functions
To enable DAX UDFs, open Power BI Desktop, go to Options → Preview Features, enable DAX user‑defined functions, and restart the application. After enabling, functions can be created using DAX Query View or the model definition view.
Why DAX User‑Defined Functions are Useful
Before DAX UDFs were introduced, reusability in DAX was mainly achieved by creating measures that referenced other measures. While this approach works, it does not support explicit parameters and can lead to complex and hard‑to‑read formulas.
DAX User‑Defined Functions help by:
- Writing calculation logic once and reusing it everywhere
- Improving readability of DAX expressions
- Centralizing business rules
- Simplifying maintenance when logic changes
Example 1: Age Classification (Used as a Calculated Column)
A common business requirement is to classify people based on their age. For example:
- Less than 18 → Minor
- Between 18 and 64 → Working Age
- 65 and above → Senior
This type of logic is well-suited for a calculated column because the result depends on a single row value.
User‑Defined Function for Age Category
DEFINE
FUNCTION AgeCategory =
(
AgeValue : INT64
) =>
SWITCH (
TRUE (),
AgeValue < 18, "Minor",
AgeValue < 65, "Working Age",
"Senior"
)
Using the Function in a Calculated Column
Age Group =
AgeCategory ( Person[Age] )
How this works
For each row in the table:
- The value from Person[Age] is passed to the function
- The function evaluates the conditions
- The appropriate age group is returned
This ensures consistent classification, avoids repeating logic, and makes it easy to update the rules later if required.
Example 2: Applying a Discount Using a DAX UDF
In many reports, there is a need to apply a discount or adjustment to different numeric values such as sales, revenue, cost, or price. Rather than repeating the same discount formula in every measure, the logic can be written once as a DAX User‑Defined Function and reused wherever needed.
Discount Function Definition
DEFINE
FUNCTION ApplyDiscount =
(
Amount : DECIMAL,
DiscountPercent : DECIMAL
) =>
Amount - ( Amount * DiscountPercent / 100 )
This function applies a percentage discount to any amount that is passed to it. The function itself is generic and not tied to any specific measure or scenario.
Using the Discount Function with a Fixed Discount
The example below shows how the function can be used with a fixed discount percentage of 10%.
Final Amount =
ApplyDiscount (
SUM ( Sales[SalesAmount] ),
10
)
How this works
First, SUM ( Sales[SalesAmount] ) calculates the total amount based on the current filters in the report.
Next, the value 10 represents a fixed discount percentage.
Both values are passed to the ApplyDiscount function, which applies the discount using the formula:
Final Amount = Amount − (Amount × Discount %)
For example, if the total amount is 6,000, the final result after applying a 10% discount is 5,400.
Reusing the Same Function for Different Calculations
Because the function is generic, it can be reused for many different calculations, such as:
- Discounted Revenue
- Adjusted Cost
- Promotional Amount
- Final Price
Adjusted Revenue =
ApplyDiscount (
SUM ( Finance[Revenue] ),
10
)
This demonstrates how a single DAX User‑Defined Function helps maintain consistent logic across different calculations without duplicating formulas.
Where DAX User‑Defined Functions Can Be Used
Once added to the model, DAX User‑Defined Functions can be used in:
- Calculated columns
- Measures
- Visual calculations
- Other DAX user‑defined functions
This makes them a powerful tool for building modular and scalable Power BI models.
Important Considerations
Since DAX User‑Defined Functions are still in preview, they should be used with care in production environments. The feature set and behavior may change before general availability, so thorough testing is recommended.
Final Thoughts
DAX User‑Defined Functions bring true reusability and parameterization into DAX. By separating repeated business logic into reusable functions, Power BI models become easier to read, easier to maintain, and more scalable.
Using calculated columns for row‑level logic and generic user‑defined functions for reusable calculations leads to well‑structured and professional Power BI solutions. As this feature matures, it is expected to become an essential part of advanced DAX development.



