WiseOwl Training - Established 1992 Wise Owl Training

Established May 1992
Wise Owl Training
See 520 reviews for our classroom and online training
If you found this blog useful and youâ€™d like to say thanks you can click here to make a contribution. Thanks for looking at our blogs!

BLOGS BY TOPIC

BLOGS BY AUTHOR

BLOGS BY YEAR

How to write queries in DAX to interrogate SSAS tabular models
Part five of an eight-part series of blogs

As well as creating measures to aggregate data in tabular models using DAX, you can also write queries to extract data - this blog shows you how!

1. Writing DAX queries
2. DAX and SQL compared
3. The EVALUATE command in the DAX query language
4. Using SUMMARIZE to group or aggregate DAX query data
5. Filtering in DAX queries using CALCULATETABLE or FILTER (this blog)
7. Combining the results of two or more tables
8. Other useful DAX functions

This blog is part of our online SSAS Tabular tutorial; we also offer lots of other Analysis Services training resources.

Posted by Andy Brown on 12 February 2016

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

# Filtering in DAX queries using CALCULATETABLE or FILTER

Just like for measures, you can filter data using either CALCULATETABLE or FILTER

Wherever the DAX query syntax calls for a table, you can instead supply a filtered set of rows instead. The syntax of the CALCULATETABLE function is usually easier to understand than the FILTER function syntax.

## An example using the CALCULATETABLE function

This query uses the CALCULATETABLE function to show total sales for products which are birds:

EVALUATE

SUMMARIZE (

-- the table we're summarising

-- (transactions for birds)

CALCULATETABLE (

'Transaction',

Species[SpeciesName] = "Bird"

),

-- field to group by

Product[ProductName],

-- statistic to show

"Quantity sold",

SUM ( 'Transaction'[Quantity] )

)

The query only shows total sales for bird products:

The query shows total sales by product, but restricts itself to those transactions where the corresponding species name is Bird.

Unlike in SQL, DAX queries know about the underlying relationships between tables, and so automatically link the species, product and transaction tables correctly.

## An example using the FILTER function

This query uses the FILTER function to show the average square metre area and number of retail units by town, but only for shopping centres in the North-West of England:

EVALUATE

SUMMARIZE (

-- show only for North-West

FILTER (

Centre,

RELATED ( Region[RegionName] ) = "North West"

),

-- group by each town

Town[TownName],

-- show average size in two different ways

"Average sq metres",

AVERAGE ( Centre[SquareMetres] ),

"Average units",

AVERAGE ( Centre[NumberUnits] )

)

Here's what this would display:

Note for readers outside the UK (and those living south of Watford in the UK): these towns are all in the North-West of England.

So far we've just used existing columns; the next part of this blog shows how you can use ADDCOLUMNS to create new ad-hoc columns in a query.