BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
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!
- Writing DAX queries
- DAX and SQL compared
- The EVALUATE command in the DAX query language
- Using SUMMARIZE to group or aggregate DAX query data (this blog)
- Filtering in DAX queries using CALCULATETABLE or FILTER
- Adding columns in a DAX query using ADDCOLUMNS
- Combining the results of two or more tables
- 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.
Using SUMMARIZE to group or aggregate DAX query data
It's rare that you'll want to list all of the rows in a table: more commonly, you'll want to summarise data.
The DAX SUMMARIZE function is so similar to the concept of SELECT ... GROUP BY in SQL that you may wonder why Microsoft couldn't have merged the two language features!
Syntax of the SUMMARIZE command
In DAX you can summarise by one or more fields in a table, and then show an aggregation for each unique combination of values. Here's the basic syntax:
SUMMARIZE(
Table whose columns you want to display,
First column you want to aggregate or group by,
...,
Last column you want to aggregate or group by,
Name of first aggregated expression,
Expression formula to use for this,
...,
Name of last aggregated expression,
Expression formula to use for this
)
By far the easiest way to understand this is to look at the examples which follow below.
Summarising by a single column
This query would show the total quantity sold by species:
-- show total quantity by species
EVALUATE
SUMMARIZE (
Species,
Species[SpeciesName],
"Quantity sold",
SUM ( 'Transaction'[Quantity] )
)
This query should give the following results:

The total quantity column has been renamed to Quantity sold, as requested.
Summarising by more than one column
The following query would show the number of transactions and quantity sold by species, animal and product:
-- show two statistics
-- summarised by 3 columns
EVALUATE
SUMMARIZE (
Product,
Species[SpeciesName],
Animal[AnimalName],
Product[ProductName],
"Number of transactions",
COUNTROWS ( 'Transaction' ),
"Quantity sold",
SUM ( 'Transaction'[Quantity] )
)
Here's what you should get when you run this query:

The query groups by 3 columns, and shows two statistics.
Using ROLLUP to get all possible subtotal combinations
Just as in SQL, you can use the ROLLUP function to force DAX to calculate all subtotals and totals. For example:
-- show total sales for each
-- year and species
EVALUATE
SUMMARIZE (
'Transaction',
ROLLUP (
Calendar[Year],
Species[SpeciesName]
),
"Total qty",
SUM ( 'Transaction'[Quantity] )
)
Here's what this would show:

You can include as many or as few columns as you like in the ROLLUP function.
You can use the ISSUBTOTAL function with ROLLUP to determine for any column whether it is being used for grouping, subtotalling or totalling (although you'd have to care a lot more than this owl about the roll-up feature to do so).
So far we've been considering all the rows in tables; what I'll do in the next part of this blog is to show how to filter data.
- Writing DAX queries
- DAX and SQL compared
- The EVALUATE command in the DAX query language
- Using SUMMARIZE to group or aggregate DAX query data (this blog)
- Filtering in DAX queries using CALCULATETABLE or FILTER
- Adding columns in a DAX query using ADDCOLUMNS
- Combining the results of two or more tables
- Other useful DAX functions
How would I summarize by date? For example, if I want to sum all sales up till beginning of this month, then sum by every day of the current month - how would I do that?