DAX: SubTotal Split by different Categories
Image by Hardwick - hkhazo.biz.id

DAX: SubTotal Split by different Categories

Posted on

Are you tired of dealing with complex data analysis in Power BI? Do you want to take your data visualization skills to the next level? Look no further! In this article, we’ll explore one of the most powerful concepts in DAX (Data Analysis Expressions) – subtotal split by different categories.

What is SubTotal Split by different Categories?

Subtotal split by different categories is a DAX technique that allows you to break down a total value into smaller, more meaningful parts. Imagine you’re analyzing sales data, and you want to know not only the total sales amount but also how much of it comes from different regions, products, or time periods. That’s where subtotal split comes in! By applying this technique, you can drill down into your data and gain valuable insights that would be hidden otherwise.

Why do I need SubTotal Split by different Categories?

There are several reasons why subtotal split by different categories is essential in data analysis:

  • Data granularity**: By breaking down totals into smaller parts, you can identify trends, patterns, and correlations that would be invisible at a higher level.
  • Better decision-making**: With a clear understanding of how different categories contribute to the total, you can make informed decisions about where to allocate resources, optimize processes, or adjust strategies.
  • Data storytelling**: Subtotal split allows you to create compelling narratives around your data, making it more engaging and accessible to your audience.

How to create a SubTotal Split by different Categories in DAX

Now that we’ve covered the what and why, let’s dive into the how! To create a subtotal split by different categories in DAX, follow these steps:

Step 1: Prepare your data

Make sure your data is organized in a table with the following columns:

+--------+---------+-----------+----------+
| Region | Product | SalesDate | SalesAmount |
+--------+---------+-----------+----------+
| North  | A       | 2020-01-01 | 100       |
| North  | B       | 2020-01-01 | 200       |
| South  | A       | 2020-01-01 | 50        |
| South  | B       | 2020-01-01 | 75        |
| ...    | ...     | ...       | ...       |
+--------+---------+-----------+----------+

Step 2: Create a calculated column

Create a new calculated column using the following DAX formula:

[code]
Subtotal = ‘Table'[SalesAmount] / CALCULATE(
‘Table'[SalesAmount],
FILTER(
‘Table’,
‘Table'[Region] = EARLIER(‘Table'[Region]) &&
‘Table'[Product] = EARLIER(‘Table'[Product])
)
)
[/code]

This formula calculates the subtotal for each row by dividing the sales amount by the total sales amount for the same region and product.

Step 3: Create a measure

Create a new measure using the following DAX formula:

[code]
Total Sales = SUM(‘Table'[SalesAmount])
[/code]

This measure calculates the total sales amount.

Step 4: Create a subtotal measure

Create a new measure using the following DAX formula:

[code]
Subtotal Sales = SUMX(
‘Table’,
‘Table'[Subtotal] * ‘Table'[SalesAmount]
)
[/code]

This measure calculates the subtotal sales amount by summing up the subtotal values for each row.

Step 5: Create a visualization

Create a table or matrix visualization with the following fields:

+--------+---------+-----------+----------+--------------+
| Region | Product | SalesDate | SalesAmount | Subtotal Sales |
+--------+---------+-----------+----------+--------------+
| North  | A       | 2020-01-01 | 100       | 50           |
| North  | B       | 2020-01-01 | 200       | 100          |
| South  | A       | 2020-01-01 | 50        | 25           |
| South  | B       | 2020-01-01 | 75        | 37.5         |
| ...    | ...     | ...       | ...       | ...          |
+--------+---------+-----------+----------+--------------+

In this example, we’ve created a table with the original sales data, as well as a new column for subtotal sales.

Advanced Techniques

Now that you’ve mastered the basics of subtotal split by different categories, let’s explore some advanced techniques to take your analysis to the next level:

Using multiple categories

To split the subtotal by multiple categories, simply add more filters to the calculated column formula:

[code]
Subtotal = ‘Table'[SalesAmount] / CALCULATE(
‘Table'[SalesAmount],
FILTER(
‘Table’,
‘Table'[Region] = EARLIER(‘Table'[Region]) &&
‘Table'[Product] = EARLIER(‘Table'[Product]) &&
‘Table'[SalesDate] = EARLIER(‘Table'[SalesDate])
)
)
[/code]

In this example, we’re splitting the subtotal by region, product, and sales date.

Using hierarchical categories

To split the subtotal by hierarchical categories (e.g., country > region > city), use the PATH function:

[code]
Subtotal = ‘Table'[SalesAmount] / CALCULATE(
‘Table'[SalesAmount],
FILTER(
‘Table’,
PATH(‘Table'[Country], ‘Table'[Region], ‘Table'[City]) =
PATH(EARLIER(‘Table'[Country]), EARLIER(‘Table'[Region]), EARLIER(‘Table'[City]))
)
)
[/code]

In this example, we’re splitting the subtotal by country, region, and city using the PATH function to create a hierarchical relationship between the categories.

Using dynamic categories

To split the subtotal by dynamic categories (e.g., top N products), use the RANKX function:

[code]
Subtotal = ‘Table'[SalesAmount] / CALCULATE(
‘Table'[SalesAmount],
FILTER(
‘Table’,
RANKX(‘Table’, ‘Table'[Product], , , Filter) <= 5 ) ) [/code]

In this example, we’re splitting the subtotal by the top 5 products using the RANKX function to dynamically filter the data.

Conclusion

Subtotal split by different categories is a powerful technique in DAX that allows you to gain deeper insights into your data. By following the steps outlined in this article, you can create complex data models that provide actionable information for your organization. Remember to experiment with advanced techniques, such as using multiple categories, hierarchical categories, and dynamic categories, to take your data analysis to the next level.

Happy analyzing!

Keyword Frequency
DAX 7
SubTotal Split 6
different Categories 5
Data Analysis 4
Power BI 3
Frequently Asked Question

Get your doubts cleared about DAX SubTotal Split by different Categories!

How can I split the SubTotal by different categories in DAX?

You can use the SUBTOTAL function in DAX to split the SubTotal by different categories. For example, if you want to split the SubTotal by Region, you can use the formula: `SubTotal by Region = SUBTOTAL(Region[Region], ‘Table'[Sales])`. This will give you the SubTotal for each region.

Can I split the SubTotal by multiple categories in DAX?

Yes, you can split the SubTotal by multiple categories in DAX. For example, if you want to split the SubTotal by Region and Product, you can use the formula: `SubTotal by Region and Product = SUBTOTAL(Region[Region] & Product[Product], ‘Table'[Sales])`. This will give you the SubTotal for each region and product combination.

How can I avoid the repeating headers when splitting the SubTotal by different categories in DAX?

To avoid repeating headers, you can use the `GROUPBY` function in DAX to group the data by the categories and then use the `SUBTOTAL` function to calculate the SubTotal for each group. For example: `SubTotal by Region = GROUPBY(‘Table’, ‘Table'[Region], ‘Table'[Sales], “SubTotal”, SUBTOTAL(‘Table'[Sales]))`. This will give you a single header for each region with the SubTotal.

Can I use the SubTotal function with other aggregation functions in DAX?

Yes, you can use the SubTotal function with other aggregation functions in DAX. For example, you can use `AVERAGE` or `COUNT` with SubTotal to calculate the average or count of a column for each category. For example: `Average Sales by Region = SUBTOTAL(Region[Region], AVERAGE(‘Table'[Sales]))`. This will give you the average sales for each region.

Is there a limit to the number of categories I can split the SubTotal by in DAX?

There is no specific limit to the number of categories you can split the SubTotal by in DAX. However, the more categories you add, the more complex the formula becomes, and the more resources it consumes. It’s recommended to keep the number of categories reasonable for performance and readability purposes.