DAX SUMMARIZE vs GROUPBY: Grouping Data and Calculating Tax
In this article, we will learn how to group data by a column and calculate an aggregated value using SUMMARIZE and GROUPBY in DAX. We will use the Sales table and calculate 18% Tax for each Product Category.
📥 Practice Data:
Download Practice Data Free
🎥 Prefer learning through video?
DAX YouTube Playlist
What Are SUMMARIZE and GROUPBY in DAX?
SUMMARIZE and GROUPBY are table functions used to create grouped tables from an existing table. They are especially useful when you want to group rows by one or more columns and calculate additional values for each group.
In this example, we will group the Sales table using Product[Category] and calculate the tax amount using an 18% tax rate.
1. Using SUMMARIZE to Group Sales by Category
Query
EVALUATE
SUMMARIZE(
Sales,
'Product'[Category],
"Total Sales", SUM(Sales[Sales Amount] * (18/100))
)
Corrected Query
EVALUATE
SUMMARIZE(
Sales,
'Product'[Category],
"Total Tax", SUMX(
Sales,
Sales[Sales Amount] * (18/100)
)
)
Step-by-step explanation
Step 1: EVALUATE
EVALUATE returns the result of the table expression as the output of the DAX query.
Step 2: SUMMARIZE
SUMMARIZE creates a grouped table based on the table and grouping columns supplied to it.
Step 3: Sales
Sales is the source table from which the grouped result is created.
Step 4: ‘Product'[Category]
This column defines the grouping. The result will contain one row for each Product Category visible through the relationship between the Product and Sales tables.
Step 5: “Total Tax”
This is the name of the calculated output column.
Step 6: SUMX()
SUMX evaluates the tax expression row by row and then adds the results together.
The calculation is:
Sales[Sales Amount] * (18/100)
For example, if a sales row has a Sales Amount of 1,000, the tax is:
1,000 * 18% = 180
What will you get?
The result is a grouped table containing the Product Category and the calculated tax for that category.
| Category | Total Tax |
|---|---|
| Category A | Calculated Tax |
| Category B | Calculated Tax |
| Category C | Calculated Tax |
The category names and tax values above are illustrative. Your actual result depends on the data in your Sales and Product tables.
2. Using GROUPBY to Group Sales by Category
Query
EVALUATE
GROUPBY(
Sales,
'Product'[Category],
"Total Tax",
SUMX(
CURRENTGROUP(),
Sales[Sales Amount] * (18/100)
)
)
Step-by-step explanation
Step 1: EVALUATE
EVALUATE returns the table produced by the GROUPBY expression.
Step 2: GROUPBY
GROUPBY groups the rows of the source table based on the grouping column.
Step 3: Sales
Sales is the source table that contains the sales transactions.
Step 4: ‘Product'[Category]
This is the grouping column. The query creates a separate group for each Product Category.
Step 5: “Total Tax”
This defines the name of the calculated column in the output table.
Step 6: CURRENTGROUP()
CURRENTGROUP() returns the rows belonging to the current group being evaluated by GROUPBY.
Step 7: SUMX(CURRENTGROUP(), …)
SUMX iterates through the rows in the current group and calculates 18% tax for every Sales Amount before adding the values together.
Sales[Sales Amount] * (18/100)
What will you get?
The result will contain one row for each Product Category and the total tax calculated from the sales rows belonging to that group.
| Category | Total Tax |
|---|---|
| Category A | Calculated Tax |
| Category B | Calculated Tax |
| Category C | Calculated Tax |
3. SUMMARIZE vs GROUPBY
| Feature | SUMMARIZE | GROUPBY |
|---|---|---|
| Purpose | Creates a grouped summary table | Creates a grouped table with group-level calculations |
| Grouping | Uses grouping columns | Uses grouping columns |
| Calculated expression | Can add named expressions | Uses expressions based on the current group |
| CURRENTGROUP() | Not used | Used to access rows in the current group |
| Iterator example | SUMX can be used for row-by-row calculations | SUMX with CURRENTGROUP() can calculate within each group |
4. Understanding CURRENTGROUP()
The most important difference in this example is how GROUPBY works with CURRENTGROUP().
Suppose the current group is a particular Product Category. CURRENTGROUP() represents the rows belonging to that category. SUMX can then iterate through those rows and calculate the tax.
GROUPBY(
Sales,
'Product'[Category],
"Total Tax",
SUMX(
CURRENTGROUP(),
Sales[Sales Amount] * (18/100)
)
)
This pattern can be useful when you need to perform a calculation specifically over the rows in each group.
5. Why SUMX Is Required for the Tax Calculation
The expression:
Sales[Sales Amount] * (18/100)
is not simply a column reference. It is an expression that must be evaluated for each row. This is why SUMX is appropriate.
The general pattern is:
SUMX(
Table,
Expression
)
SUMX evaluates the expression for each row of the supplied table and then returns the sum of those results.
📝 Practice Exercise
Try modifying both queries to calculate a 12% discount amount instead of 18% tax.
Then try calculating the net sales after discount for each Product Category.
Think about which function you need when the calculation must be evaluated row by row.
Conclusion
In this article, we learned how SUMMARIZE and GROUPBY can be used to group sales data by Product Category and calculate a value for each group.
- SUMMARIZE creates a grouped summary table.
- GROUPBY creates groups that can be processed using CURRENTGROUP().
- SUMX is useful when the calculation contains a row-by-row expression.
- CURRENTGROUP() provides the rows belonging to the current group inside GROUPBY.
- An expression such as Sales[Sales Amount] * 18% should be evaluated with an iterator such as SUMX.
Understanding these table functions is important for building more advanced DAX queries, especially when you need to create grouped tables and perform calculations over each group.
📥 Practice Data:
Download Practice Data Free
🎥 Prefer learning through video?
DAX YouTube Playlist