A Gentle Introduction to DAX Queries: How they’re different from DAX functions, how to write them, and how they look vs. SQL queries
By: Allen Tseng
Tags: Data Analytics, Dax, Business Intelligence, Power Bi, Analyitcs

If you’ve been using PowerBI (or PowerPivot, or SSAS in Tabular mode) for any amount of time, you will inevitably come across the need to write DAX (Data Analysis eXpressions) to create the custom columns and measures that you need to build your model, enhance your report, and/or perform your analysis. When thinking about DAX, many people I have worked with think about it or use it as just a formula language like Excel’s but it’s much more than that. It can also be used a query language.

How are DAX queries different from DAX functions?

At its simplest — DAX functions return a single value whereas DAX queries return a table. You will notice that some DAX functions take a table as an input and/or output an entire table, like CALCULATETABLE:

CALCULATETABLE, a function that takes a table as it’s first and only required parameter, and returns a table

If the function outputs a table, then it cannot be used on its own in PowerBI’s formula bar when creating a new column or measure, it needs to be combined with another function that uses it as an input to return a single value or it will return an error, like below.

However, it can be used if you select “New table”, where it asks you to write a DAX expression to create a new table.

To see what’s going on, I encourage you to download DAX Studio, an open-source tool that specifically works with DAX queries. After doing so, connect it to your model, whether it’s in PowerBI, SSDT, PowerPivot, or a live Tabular server, and run some DAX queries against it. That brings us to our next point.

How do you write a DAX query and how does it look vs. SQL queries?

For the sake of this article, we are going to use basic examples and assume we aren’t using variables anywhere.

Much like how the most basic SQL queries begin with a SELECT statement, DAX queries begin with an EVALUATE statement. The equivalent of SELECT * FROM <table> in DAX would be EVALUATE <table>.

Here’s a quick comparison of the basic syntax of a DAX query vs. a SQL query:

The basic SQL query syntax vs. DAX query syntax

With DAX queries if you want to specify columns, filter rows, add extra columns, or group your data, you would use many different DAX functions including but not limited to: FILTER, CALCULATETABLE, SUMMARIZE, SUMMARIZECOLUMNS, ADDCOLUMNS, GROUPBY, instead of DAX statements.

Let’s go with a slightly more complicated example.

I’ve been re-watching the MCU movies during quarantine, so I’ll be using another made up dimensional data model and dataset here.

Fact table: f_kill

Dimension tables: dim_avenger, dim_location

Our little avenger kill data model

In DAX Studio: the tables and attributes, the DAX query, and the results.

DAX query code:

EVALUATE

SUMMARIZE (

CALCULATETABLE (

‘f_kill’,

‘dim_location’[location_name]

IN {

“New York”,

“Wakanda”

}

),

‘dim_location’[location],

‘dim_avenger’[avenger],

“kills”, [number_of_kills]

)

Purpose of the above DAX query: to find out the number of kills in Wakanda and New York, and group it by location and the Avenger. One thing you may notice about the above query if you understand SQL is the lack of JOINs. With DAX, it already knows the existing relationships between tables so there are no JOINs to be written :)

Let’s break down the above query from inside to out after the EVALUATE statement:

CALCULATETABLE takes a table as input, in our case the “f_kill” table, and applies filters. The filter I am applying is for the location_name to be either New York or Wakanda.

SUMMARIZE “creates a summary of the input table grouped by the specified columns”, it is similar to GROUPBY. In our case, the input table is the result of CALCULATETABLE and we are grouping our results by location name and avenger, and the expression we are summarizing is the number of kills, which is a measure I’ve created for this exercise.

The equivalent of this query written in SQL would resemble something like:

If you’d like to download the sample PowerBI file, click here.

If you want a more comprehensive guide on DAX queries, I recommend the below resources: