Debugging DAX with DAX Query View

Comments 0

Share to social media

DAX Query View was created some time ago and it was a great tool to run DAX inside Power BI environment.

However, testing a measure in DAX Query View may not be something so obvious. While the measure in a report obeys to the filter context and most of times results in a single value, the filter context doesn’t exist in Query View and the execution needs to return a table.

In this way, a simple copy/past of the measure in DAX Query View will not run. The measure needs to be adapted to be tested.

Let’s analyze one sample case to discover how this happens.

The Model

This image below illustrates the model. The image mostly speaks by itself. In our example, the challenge will be to retrieve the field CountryRegion from the table Address according to the context of SalesOrderHeader

A screenshot of a computer

Description automatically generated

The Measure

Our measure, called country, will use the following code:

The image below shows one small example of the usage of this measure.

A screenshot of a computer

Description automatically generated

Testing the Measure in DAX Query View: SELECTEDVALUE

Let’s first test the SELECTEDVALUE function to check what it’s returning.

SELECTEDVALUE depends on the context of the report and this context will not be available in the DAX Query View.

We can simulate this context using the CALCULATE function. We will need to filter the information according to one specific values, one SalesOrderId, for example.

The first part of the expression can be like this:

The next step is transforming this single value in a table. The results in Query View need to be in a table format.

We can do this by using the ROW function. The result will be like this:

“SelectedCustomer” is the name of a field, the single field of this table result.

In this way, we confirmed the CustomerID value for the SalesOrderId 71856

A screenshot of a computer

Description automatically generated

Testing the expression for this CustomerId

We can change the rest of the expression to use this CustomerId in a fixed way, for testing purposes, to ensure the result this specific CustomerId provides.

The first part, the SELECTEDVALUE, can be removed. We will also need to change the result to be in a table format using the ROW function.

The expression will be like this:

A screenshot of a computer

Description automatically generated

The full expression in DAX Query View</h2>

We can join the two parts. The only difference is that we shouldn’t use ROW for the SELECTEDVALUE, only for the final result.

This is how the expression will be:

A screenshot of a computer program

Description automatically generated

Summary

In this way, we adapted a measure piece by piece, to execute tests in DAX Query View. By doing so we could identify which piece of the expression were causing trouble.

Load comments

About the author

Dennes Torres

See Profile

Dennes Torres is a Data Platform MVP and Software Architect living in Malta who loves SQL Server and software development and has more than 20 years of experience. Dennes can improve Data Platform Architectures and transform data in knowledge. He moved to Malta after more than 10 years leading devSQL PASS Chapter in Rio de Janeiro and now is a member of the leadership team of MMDPUG PASS Chapter in Malta organizing meetings, events, and webcasts about SQL Server. He is an MCT, MCSE in Data Platforms and BI, with more titles in software development. You can get in touch on his blog https://dennestorres.com or at his work https://dtowersoftware.com