M365con.net Microsoft Community Conference 2027
Aug. 28, 2026

Demystifying DAX UDFs: VAL vs. EXPR Explained

Welcome back to the podcast companion blog! In our latest episode, we took a deep dive into the powerful world of DAX User-Defined Functions (UDFs). If you have been experimenting with UDFs in Power BI or Analysis Services, you likely know that they are absolute game-changers for eliminating code duplication and centralizing business logic. However, diving into advanced modeling without understanding the nuances of parameter modes is a recipe for silent bugs, unexpected totals, and agonizing query timeouts. To catch the full audio breakdown and hear real-world examples of these concepts in action, make sure to listen to the companion episode over at DAX UDF VAL vs EXPR: Prevent Timeouts and Wrong Totals.

In this post, we are going to expand on the core differences between the VAL and EXPR parameter modes. By the time you finish reading, you will know exactly when to capture a static value versus when to deploy a dynamic, context-aware expression to keep your semantic models running fast and accurate.

DAX UDFs Overview

What Are DAX UDFs?

DAX user-defined functions (UDFs) let you create custom calculations that you can reuse across your data models. Instead of writing the same formula multiple times, you define it once as a function. Then, you call that function wherever you need it. This approach helps you keep your work organized and consistent.

Think of DAX UDFs as building blocks for your reports. They encapsulate logic, so you don’t have to repeat yourself. This saves time and reduces errors. You can also share these functions with your team, making collaboration easier.

Here is a quick look at what makes DAX UDFs stand out compared to other user-defined functions in data analytics:

Feature Description
Centralized Logic DAX UDFs allow for the central definition and reuse of DAX calculations, making models easier to manage and change.
Reusability They enable encapsulation and reuse of logic across different DAX expressions and semantic models.
Time Efficiency DAX UDFs save time when reusing DAX across different models and projects.
Performance Improvement They can enhance performance in scenarios with layered measure calculations, reducing formula engine overhead.
Improved Organization DAX UDFs help in organizing models and enhancing consumption in downstream calculations and reports.

Importance of DAX UDFs

Using DAX user-defined functions improves your report development in many ways. First, they make your models cleaner and easier to maintain. When you update a function, all measures that use it update automatically. This reduces the chance of mistakes and keeps your reports consistent.

DAX UDFs also boost scalability. They help your reports grow without becoming messy or slow. You can build complex calculations step-by-step, then reuse those steps in other parts of your model. This modular design makes your work more efficient.

Here are some key benefits you gain by using DAX UDFs:

  • They reduce duplication of logic, which lowers errors and saves time.
  • They allow you to update business rules in one place, reflecting changes everywhere.
  • They improve collaboration by providing a shared library of functions for your team.
  • They simplify report development, cutting down the time needed by up to 70-80%.

Compared to traditional DAX formulas, DAX UDFs can also improve performance dramatically. For example, some tests show that UDFs reduce calculation time from over 6000 milliseconds to just over 160 milliseconds — a 97% improvement.

By mastering DAX user-defined functions, you gain better control over your data models. You create reports that are faster, easier to maintain, and more reliable. This skill helps you deliver high-quality analytics that meet your business needs.

Stop Using DAX UDFs Wrong: Common Mistakes

Misunderstanding Data Types

Many users struggle with data types when working with DAX UDFs. They often overlook the importance of using the correct type for parameters. This misunderstanding can lead to unexpected results. For instance, if you pass a text value to a function expecting a number, the calculation may fail or return incorrect results.

Here are some common pitfalls related to data types:

  • Users frequently confuse numeric and text formats.
  • They may not realize that DAX treats dates as numbers, which can complicate calculations.
  • Misusing data types can degrade model performance, leading to slow reports.

Incorrect Syntax Usage

Syntax errors are another common issue when you work with DAX UDFs. A small mistake in your code can halt the execution of your calculations. When you make a syntax error, DAX displays a yellow warning symbol in the expression editor. This symbol indicates that something is wrong, but it may not always clarify the exact issue.

Here are some typical syntax mistakes to avoid:

  • Forgetting to close parentheses or brackets.
  • Using incorrect function names or misspelling them.
  • Failing to separate arguments with commas.

These errors can lead to frustration and wasted time. Always double-check your syntax before executing your DAX expressions.

Ignoring Error Handling

Effective error handling is crucial for maintaining the reliability of your DAX UDFs. Many users neglect this aspect, which can result in calculation failures. For example, if a function receives an unexpected input type, it may return an error instead of a useful result.

To prevent these issues, consider implementing the following strategies:

  • Use the IFERROR() function to provide a fallback value. This prevents errors from disrupting your visuals.
  • Utilize ISBLANK() to check for truly blank values, distinguishing them from zero or empty strings.
  • Implement COALESCE() to return the first non-blank value from a list of expressions.

You can also define functions that check input types. For instance, use ISSTRING() to verify if an input is a string. If it is not, return BLANK(). This approach ensures your calculations remain robust and reliable.

By avoiding these common mistakes, you can enhance your DAX UDFs and improve your overall data model performance. Remember, understanding how to use VAL and EXPR correctly is key to unlocking the full potential of your DAX user-defined functions.

Understanding VAL in DAX

Purpose of VAL

The VAL function in DAX serves a specific purpose: it converts a text representation of a number into an actual numeric value. This function is essential when you work with data that may come in different formats. For example, if you receive a number as text from a data source, you can use VAL to convert it into a usable numeric format. This conversion allows you to perform calculations accurately.

You should use VAL when:

  • You need to ensure that your calculations work with numeric values.
  • You want to avoid errors that arise from using text instead of numbers.
  • You are dealing with user inputs that may not always be in the correct format.

VAL Syntax

The syntax for the VAL function is straightforward:

VAL(<text>)
  • <text>: This parameter represents the text string you want to convert into a number. It can be a direct text input or a reference to a column containing text values.

Here’s a simple example of how you might use the VAL function:

SalesAmount = VAL('Sales'[SalesText])

In this example, the function converts the text in the 'SalesText' column into a numeric value, allowing you to perform calculations on it.

VAL Usage Examples

Let’s explore some practical examples of using the VAL function in DAX.

  1. Basic Conversion: Suppose you have a column named 'RevenueText' that contains revenue figures as text. You can create a measure to convert these values into numbers:

    TotalRevenue = SUMX('Sales', VAL('Sales'[RevenueText]))

    This measure calculates the total revenue by converting each text entry into a number.

  2. Handling User Input: If you have a scenario where users input numbers as text, you can use VAL to ensure your calculations remain accurate. For instance, if a user enters a sales target as text, you can convert it:

    SalesTarget = VAL(UserInput[TargetText])

    This ensures that your calculations based on the sales target are correct.

  3. Error Prevention: Using VAL can help prevent errors in your reports. If you attempt to perform calculations on text values without converting them, you may encounter issues. By using VAL, you can safeguard against these errors:

    AdjustedSales = IF(ISERROR(VAL('Sales'[SalesText])), 0, VAL('Sales'[SalesText]))

    In this example, if the conversion fails, the measure returns 0 instead of causing an error.

By understanding the purpose and correct usage of the VAL function, you can enhance your DAX user-defined functions. This knowledge allows you to handle data more effectively and ensures your calculations yield accurate results.

Understanding EXPR in DAX

Purpose of EXPR

The EXPR parameter mode in DAX plays a vital role when you want your user-defined functions to react dynamically to changes in your data model. Unlike VAL, which captures a fixed value at the time of function call, EXPR allows you to pass an expression that evaluates every time the function runs. This means your calculations can adapt to different filter contexts or row contexts automatically.

You should use EXPR when your function needs to respond to changing data conditions. For example, if you want to calculate a measure that depends on the current filter or slicer selection, EXPR lets you pass that logic as an expression. This flexibility makes your user-defined functions more powerful and context-aware.

Tip: Use EXPR to create dynamic calculations that update based on user interactions or data filters. This helps you build reports that reflect real-time insights.

EXPR Syntax

The syntax for using EXPR in DAX user-defined functions requires you to declare the parameter as an expression. Here is the general form:

DEFINE FUNCTION MyFunction(EXPR paramName AS EXPRESSION)
RETURNS <return_type>
AS
BEGIN
    RETURN <calculation_using paramName>
END
  • paramName: The name of the parameter that will receive the expression.
  • EXPRESSION: Specifies that the parameter expects an expression rather than a fixed value.

When you call the function, you pass a DAX expression instead of a simple value. The function evaluates this expression each time it runs, considering the current filter or row context.

EXPR Usage Examples

Let’s look at some practical examples to understand how EXPR works in dax user-defined functions.

  1. Dynamic Filtering Example

Suppose you want to create a function that calculates the total sales but allows you to pass a filter expression dynamically:

DEFINE FUNCTION TotalSalesWithFilter(EXPR FilterExpression AS EXPRESSION)
RETURNS DECIMAL
AS
BEGIN
    RETURN CALCULATE(SUM(Sales[Amount]), FilterExpression)
END

When you call this function, you can pass different filter expressions:

TotalSalesWithFilter(FILTER(Sales, Sales[Region] = "West"))

This call calculates total sales only for the West region. The function evaluates the filter expression each time, adapting to the context you provide.

  1. Context-Sensitive Calculation

You can also use EXPR to pass a measure or calculation that depends on the current row or filter context:

DEFINE FUNCTION CalculateMargin(EXPR SalesExpr AS EXPRESSION, EXPR CostExpr AS EXPRESSION)
RETURNS DECIMAL
AS
BEGIN
    RETURN SalesExpr - CostExpr
END

Calling this function with expressions like SUM(Sales[Amount]) and SUM(Sales[Cost]) lets you calculate margin dynamically based on the current filters.

  1. Using EXPR with CALCULATE

Always wrap EXPR parameters inside CALCULATE when you want to enforce context transitions. This ensures the expression evaluates correctly in the desired filter context.

Note: Misusing EXPR by passing static values or not wrapping expressions in CALCULATE can lead to unexpected results.

By mastering EXPR in your dax user-defined functions, you gain the ability to write flexible, context-aware calculations. This skill helps you build reports that respond accurately to user selections and data changes.

Best Practices for DAX User Defined Functions

Effective Usage Tips

To optimize your DAX user-defined functions, follow these best practices:

  • Be cautious of context transition issues when using EXPR parameters. Context transitions can lead to unexpected results if not managed properly.
  • Utilize CALCULATE for complex scenarios. This function helps manage table contexts effectively, ensuring your calculations reflect the intended logic.
  • Emphasize modular development. Break down complex logic into manageable pieces. This approach simplifies maintenance and improves performance.
  • Standardize business logic across teams. Consistency in how you define and use functions enhances collaboration and reduces redundancy.
  • Optimize calculations in one place. This practice allows you to improve performance and maintainability by updating logic without extensive searches.

Common Scenarios and Solutions

Understanding when to use VAL and EXPR can significantly enhance your DAX UDFs. Here are some common scenarios:

  • Use VAL parameters when the parameter value is static or does not depend on row context. For example, if you need a fixed threshold value, VAL is the right choice.
  • Choose EXPR parameters when the parameter needs evaluation in different row contexts. This flexibility allows for dynamic calculations, such as iterating over tables or filtering based on computed metrics.

Here are specific examples to illustrate these points:

  1. In the BestCustomers function, using EXPR parameters allows the metric to be evaluated for each customer, producing accurate results.
  2. When using VAL parameters, the function evaluates the metric only once. This can lead to incorrect or empty results if the context changes.
  3. The need for context transitions is crucial. EXPR parameters can be explicitly managed with CALCULATE to ensure correct evaluation.

By applying these best practices, you can simplify maintenance and improve performance in your DAX user-defined functions. This approach not only enhances your calculations but also contributes to long-term efficiency in your data models.


In this blog, you learned about the significance of VAL and EXPR in DAX user-defined functions. Understanding these concepts helps you avoid common mistakes and enhances your data models.

  • VAL captures fixed values, while EXPR allows for dynamic calculations.
  • Proper usage of these functions improves accuracy and performance.

By applying this knowledge, you can create more effective DAX UDFs. Start implementing these practices today to elevate your data analytics skills! πŸš€ Don't forget to check out the related episode for a full audio walkthrough and discussion on DAX UDF VAL vs EXPR: Prevent Timeouts and Wrong Totals.

FAQ

What is the difference between VAL and EXPR in DAX?

VAL captures a fixed value at the time of function invocation. EXPR evaluates an expression dynamically, adapting to changes in filter or row context.

When should I use VAL?

Use VAL when you need a constant numeric value. It’s ideal for scenarios where the input does not change based on context, like thresholds.

When is EXPR the better choice?

Choose EXPR when your calculations need to respond to changing data conditions. It allows for dynamic evaluations based on user interactions or filters.

Can I mix VAL and EXPR in the same function?

Yes, you can mix VAL and EXPR in a function. Just ensure you use them appropriately based on whether you need fixed values or dynamic expressions.

How can I improve my DAX UDF performance?

To enhance performance, minimize complex calculations within functions. Use VAL for static values and wrap EXPR evaluations in CALCULATE to manage context transitions effectively.

What common mistakes should I avoid with DAX UDFs?

Avoid misusing data types, incorrect syntax, and neglecting error handling. These mistakes can lead to inaccurate results and hinder your data model's performance.

Is there a way to test my DAX UDFs?

Yes, you can test your DAX UDFs by creating sample reports or using DAX Studio. This allows you to evaluate performance and ensure accuracy in calculations.

Where can I find more resources on DAX UDFs?

You can find additional resources on DAX UDFs in Microsoft’s official documentation, online courses, and community forums. These platforms offer valuable insights and examples.

Related Episode

Nov. 18, 2025

DAX UDF VAL vs EXPR: Prevent Timeouts and Wrong Totals

You thought VAL vs EXPR was harmless syntax sugar. In reality, it decides when your metrics are evaluated, which means the same function, same arguments can return a completely different β€œtruth” β€” clean visuals, perfect totals, and numbers that are confidently wrong.
Guest: Mirko Peters