Table of Contents

DAX Debugger

It is no secret that DAX is a relatively complex language which is difficult to master. Most data model developers have probably experienced a situation, where the DAX code did not return the expected result. In this situation, it is helpful to break down the code, variable by variable and function call by function call, to better understand what is going on.

Until now, this "breakdown" of the code was a tedious and time consuming task, which often involved capturing DAX queries executed by client tools, in order to break them down and execute smaller pieces of the queries in DAX Studio or SQL Server Management Studio.

Tabular Editor 3 introduces the concept of the DAX debugger, which is a tool that makes the process of stepping into the DAX code of your model, much, much easier. At a conceptual level, the debugger is similar to traditional IDE debuggers, such as the one found in Visual Studio when developing C# applications.

Prerequisites

The DAX debugger analyses the DAX code in your model and generates suitable DAX queries for evaluating sub expressions, row contexts, etc., allowing you to step through the code in an interactive fashion.

In order for this to work, Tabular Editor 3 must operate in connected or workspace mode, such as when loading model metadata directly from Power BI Desktop or any other instance of Analysis Services.

Getting started

While Tabular Editor 3 is connected to an instance of Analysis Services, the debugger can be started in one of two different ways:

  • Through a Pivot Grid
  • Through a DAX query

Once the debugger is started, you are presented with a number of new views that provide contextual information about the code being debugged. The portion of code currently debugged is highlighted in a debug script view when it belongs to the model, and in the DAX query when the query defines it.

Tip

Before starting a debugging session, consider formatting your DAX code to make the code easier to read.

Debugging through a Pivot Grid

  1. Create a new Pivot Grid (File > New > Pivot Grid)
  2. Add the measure you wish to debug to the Pivot Grid. You can either:
  • Drag a measure from the TOM Explorer, or
  • Right-click on a measure in the TOM Explorer and choose Add to pivot grid, or
  • Select the measure from the Pivot Grid field list (Pivot Grid > Show fields)
  1. (Optional) Add one or more columns or hierarchies to the Pivot Grid in the Filter area, Columns area or Row area.
  2. Right-click on the value cell within the Pivot Grid and choose Debug this value.

Debug From Pivot

Debugging through a DAX Query

  1. Create a new DAX query (File > New > DAX Query).
  2. Type in or paste the DAX query. This should typically be a query made up of a SUMMARIZECOLUMNS call with one or more (explicit) measures, such as the one generated by visuals in Power BI. The measures can be model measures or measures the query defines with DEFINE MEASURE.
Tip

You can use the Performance Analyzer in Power BI Desktop to capture the query generated by visuals.

  1. Hit F5 to execute the query within Tabular Editor 3. Locate the value you want to debug, right-click the cell and choose Debug cell.

Right-click menu on a DAX query result cell, showing Debug cell

Debug views

The debugger provides the following views (if they are hidden, they can be accessed through the Debug > Windows menu).

  • Locals
  • Watch
  • Evaluation Context
  • Call Tree

Locals

This view lists the columns, measures and variables within the current scope of execution and displays their values. It also displays the value of the current subexpression being debugged. Values in this list are updated automatically when stepping to a different subexpression, or when the evaluation context is changed. Local values are always evaluated at the currently selected item of the call tree.

Locals

You can inspect a locals value by clicking on the magnifying glass button within the Value column. This will bring up a popup dialog showing the value in more details. This is especially useful if the inspected value is a table.

Inspect locals value

If you prefer to inspect the locals value in a separate DAX query window, you can toggle off the Use popup inspector option under Tools > Preferences > Tabular Editor > DAX Debugger > Locals.

DAX Debugger preferences page with the Locals, Watch and DAX queries sections

Watch

This view allows you to enter any DAX expression, which will be calculated within the current evaluation context. You can enter scalar as well as table expressions and you can use all DAX functions available and refer to variables within the current evaluation scope. Watch values are automatically updated when stepping to a different subexpression, or when the evaluation context is changed. Watch values are always evaluated at the scope of the currently selected item in the evaluation context stack.

Watch

To quickly add a variable, measure or subexpression to the Watch view, simply highlight a portion of code and drag it into the Watch view. You can also place the cursor over the expression you want to add, then right-click and choose Watch this expression:

Quick Add To Watch

To add, duplicate or delete Watch expressions, use the right-click context menu of the Watch view:

Watch Context Menu

The Generate query option is identical to the magnifying glass button within the Value column, highlighted in the screenshot below. By clicking this, the debugger will open a new DAX query document, that defines the context of the calculation as well as the calculation itself, allowing you to inspect the results in more details. This is particularly useful when the watch expression is a table expression, as shown below:

Inspect Watch

Tip

What's the difference between the Locals view and the Watch view?

  • Locals shows the values of columns, measures, variables and other relevant sub-expressions, within the current scope of execution, including the value of the currently selected subexpression in the call tree.
  • Watch allows you to enter any DAX expression, which will be calculated within the current evaluation context.

Evaluation Context

This view provides information about the DAX evaluation context of the current subexpression. For example, a CALCULATE expression might perform a context transition or add a filter to the evaluation context, or a SUMX iterator might add a row context.

Evaluation Context

You can double-click on an item in the Evaluation Context stack, to bring the focus to that item. This will cause all Watch expressions to be reevaluated in the new context (that is, all contexts from the bottom of the stack up to and including the currently focused item). This is illustrated in the animation below. Notice also how you can inspect the value of individual columns in the active row context by paging through rows within any active iterations:

Call Tree

You can also toggle individual filters from the outer filter context (for example, grouping columns on the SUMMARIZECOLUMNS call that generated the query or filters specified in a Pivot Grid). This is illustrated in the animation below. Filters toggled this way will apply to both Watch and Locals.

Call Tree

Lastly, you can browse the first 1000 rows of any iterator, setting the current row context to a specific row within those first 1000, by clicking on the Zoom button within the Row column.

Browse Row Contexts

Call Tree

This view provides an outline of the entire calculation and lets you easily navigate between subexpressions by double-clicking (you can also use shortcut keys for navigation). The tree also provides information about context transitions, iterations and row contexts. Branches of code that will not be executed (for example in an IF or SWITCH call, or when an iterator is empty) are striked out.

Call Tree

As you navigate between items in the call tree, the debug DAX script will highlight the code corresponding to the call tree item, while also indicating (with a gray background) the path taken to reach the highlighted code, as shown below:

Call Tree

Notice how the values in the Locals view are updated as the tree is navigated. You can also navigate to a subexpression by placing the cursor over the expression, right-click and choose the Step into selection option (Ctrl+B).

Step into selection

Functions and query-defined code

The debugger steps into DAX User-Defined Functions (DAX UDFs) and into measures and functions that a DAX query defines.

Stepping into functions

You step into a DAX UDF the same way you step into a measure reference. This works for model functions and for functions the DAX query defines with DEFINE FUNCTION.

In the Call Tree, a function call has a child node for each argument passed to a VAL parameter, followed by a Function F(params) node, such as Function Scale(x, f), with the function body below it. Arguments passed to EXPR and reference-type parameters appear in the body, at each place the body uses the parameter. A node that refers to a parameter shows the parameter name with the argument as written at the call site, such as x: [Sales Amount], on one line and cut off at 60 characters. If the call omits an optional argument, the node shows the parameter's default value.

Call Tree with a function call node, its Function Scale(x, f) node and parameter nodes, next to Locals showing the parameters

While the current item is inside a function body, Locals lists the function's parameters, each with the argument the call passes to it, and hides the variables of the calling code. A VAL parameter shows the value computed at the call site, and an EXPR or reference-type parameter shows its argument evaluated in the current evaluation context. When the current item is inside an argument passed to an EXPR parameter, Locals shows the scope where the argument is written: the variables of the calling code, and the parameters of the enclosing function if the call is inside another function.

Code defined in the DAX query

When you debug a cell from a DAX query, measures and functions that the query defines with DEFINE MEASURE or DEFINE FUNCTION are highlighted in the DAX query itself. The debug script view holds the model measures, followed by the model functions, and opens only when the debugged code uses model measures or functions. As you step between query code and model code, the view that holds the current item comes to the front. Right-click code in the DAX query to use Step into selection (Ctrl+B) and the watch commands, as in the debug script view.

If the query redefines a model measure or function, the redefinition applies to the query's own code only. Model measures and functions that use the redefined object keep the model's definition, as they do when the query runs.

DAX query being debugged, with the function body highlighted in the query and the Stop debugging button on the Debug toolbar

The DAX query is read-only while you debug it, and auto-complete doesn't open in the query or in the debug script view. If you type, paste or delete text in the query, the Stop debugging? prompt appears with the message "A DAX query cannot be edited while it is being debugged. Do you want to stop debugging to edit the query?":

  • OK stops debugging. The edit isn't applied, so repeat it once the query is editable.
  • Cancel keeps debugging and leaves the query unchanged.

Select Don't ask again before you select OK to stop debugging without the prompt on later edits. To get the prompt back, select Ask before editing a DAX query that is being debugged under Tools > Preferences > Tabular Editor > DAX Debugger > DAX queries.

Stop debugging? prompt with the Don't ask again check box

If you close the DAX query or the debug script view while you debug, a Stop debugging? prompt appears. Select OK to stop debugging and close the document.

Stop debugging

Select Stop debugging on the Debug toolbar or in the Debug menu to end the session. The DAX query becomes editable again, and the debug script view and the Locals, Watch, Evaluation Context and Call Tree windows close. Closing the debugged document or the model also ends the session and closes the windows. The next session opens the windows again where you left them, with the same watch expressions. Stop debugging has no default keyboard shortcut; assign one under Tools > Preferences > Tabular Editor > Keyboard.

Scalar predicates

Scalar predicates used in filter arguments of the CALCULATE or CALCULATETABLE functions are handled in a special way, in the Locals view.

For example, the following measure uses a scalar predicate, to display only the sales made in USA or Canada.

CALCULATE(
    [Total Sales],
    Geography[Country Region Code] = "US" || Geography[Country Region Code] = "CA"
)

At first glance, the expression on line 3 looks like it would return a scalar value (true/false). However, in DAX, filters are tables. In reality, the scalar predicate is converted to a table expression using the FILTER function, as shown below:

CALCULATE(
    [Total Sales],
    FILTER(
        ALL(Geography[Country Region Code]),
        Geography[Country Region Code] = "US" || Geography[Country Region Code] = "CA"
    )
)

The FILTER function is an iterator that iterates over the table ALL(Geography[Country Region Code]), that is - all the unique values of the "Country Region Code" column in the "Geography" table. Iterators generate a filter context for every row in the iteration. The scalar predicate is then evaluated in each such row context. In the case of the FILTER function, only the rows for which the predicate evaluates to TRUE are kept. In this example, the FILTER function would output a table with 1 column ("Country Region Code"), and 2 rows ("US" and "CA").

When debugging a scalar predicate, the Locals view will show two special items, (Current expression) and (Filter expression). These are described below:

Debug Scalar Predicates

In the above screenshot:

  1. This is the scalar predicate currently being debugged. Even though this sub-expression looks like one that should return a scalar value (true/false), in reality, it returns a table.
  2. (Current expression): This is the scalar value of the predicate, when evaluated within the current row context generated by the FILTER function as described above. In the screenshot, the scalar value evaluates to False, because the value of [Country Region Code] in the current row context is "AU", as can be seen in the Watch view, (4). We can use the Evaluation Context view (5), to scroll through the rows of the iteration one by one.
  3. (Filter expression): This is the table expression generated by the FILTER function, as described above. In the screenshot, this is a 1x2 table containing the values "US" and "CA". Clicking the magnifying glass button will open a popup that displays the table values in a grid.
  4. We can use the Watch window to evaluate any DAX expression within the current evaluation context. In this case, since we have an active row context, we can directly refer to columns in the row context, such as Geography[Country Region Code]. We can see that the current value of this column is "AU", which is why the scalar predicate (2) evaluates to False.
  5. We can use the Evaluation Context view to scroll through the rows of the iteration, one by one. This will update the values in the Locals view, as well as the Watch view, to reflect the values in the current row context.

Keyboard shortcuts

Use the following keyboard shortcuts to quickly navigate the call tree:

  • Step in (F11) - steps into the first child of the current item in the call tree. If there are no more children, jumps to the next sibling.
  • Step out (Shift-F11) - steps out to the parent of the current item in the call tree.
  • Step over (F10) - jumps to the next function parameter, the next subexpression of an arithmetic operation, or steps into the current function call (if it is a non-trivial function).
  • Step back (Shift-F10) - jumps to the previous function parameter, the previous subexpression of an arithmetic operation, or steps out to the parent of the current item if there are no parameters or subexpressions before the current item.
  • Step into selection (Ctrl-B) - jumps to the expression under the cursor. If multiple paths lead to the same expression (for example, when a measure is referenced by multiple measures and these measures), a dialog will prompt you to choose the path.
  • Next row (F9) - shifts the row context of the innermost iteration to the next row of the iterator.
  • Previous row (Shift-F9) - shifts the row context of the innermost iteration to the previous row of the iterator.

Stop debugging has no default shortcut. See Stop debugging.

Limitations and known issues

The DAX debugger has the following limitations:

  • Only a subset of DAX table expressions are supported when debugging a DAX query (for example, queries that rely on SUMMARIZECOLUMNS can be debugged, while other table functions aren't supported). Queries that have been generated by Power BI (which can be captured through the Power BI Desktop Performance Analyzer) are generally supported.
  • Only cells that return a measure, either a model measure or one the query defines with DEFINE MEASURE, can be debugged. Cells of inline expression columns, such as "Twice", Twice([Sales Amount]) in a SUMMARIZECOLUMNS call, and implicit measures can't be debugged.
  • Watch expressions that reference a query-scoped measure or a function parameter aren't supported.
  • When browsing the first 1000 rows of an iterator that arises out of a filtered table expression, the selected row in the browse window may not always correspond to the current row context in the evaluation context stack (type CALCULATETABLE('<table name>') in the Watch window to inspect the current row context).
  • The debugger only allows debugging DAX expressions on measures.
  • Cells that rely on query-scoped columns can't be debugged. This includes visual calculations, which are defined using query-scoped columns.
  • If a measure is modified by a calculation item in the filter context, partial results shown in the Watch / Locals view of the debugger may be incorrect.

If you encounter an issue with the debugger, other than those listed above, please post it to our issue tracker on the TE3 Community Support GitHub site.

Roadmap

We plan to add many additional features to the DAX debugger over time, to address the issues above, and to make the tool even more capable. As always, feedback is more than welcome. Please use the Discussions area for feature requests and general discussions.

Happy debugging!