Adding a Header using Sub-Total in Tableau
Sub-Totals are great! Our end users agree with that. But have you ever used sub-total for adding a space or maybe a header?
I was recently working on a #Pride viz when I had a design idea to show Country name on top and the cities below it.
But I didn’t want to create an individual worksheet per Country.
So that’s what we are going to discuss in this blog today. But for the sake for simplicity, I’ll be using Sample Superstore to illustrate the process.
Basic View
Now the basic view would look like this.
But we would like to see Category on top of sub categories. Now let’s go step by step.
Add Space between each Categories
For that, let’s add a sub-total: Analysis → Totals → Add all subtotals. Also, check the “Columns Totals on Top”.
Identify the Sub-Total
Now we have the total but we don’t really want to show the bar for total.
So the first step here would be to find a way to identify the total. Now, this part was a lot of trial and error but I finally narrowed it down to two functions — Size() and Index()
Create a calculated field: Sub-Total?
INDEX()=1 and SIZE()=1
- Now using the Sub-Total? boolean field, we will modify the sub-total sales bar and also add a special category label.
Create a calculated field: Adjusted Sales
IF NOT [Sub-Total?] THEN SUM([Sales]) END
Similarly, we’ll create a label for Category and Sub-Category.
Create a calculated field: Category label
IF [Sub-Total?] THEN MAX([Category]) ENDCreate a calculated field: Sub-Category label
IF NOT [Sub-Total?] THEN MAX([Category]) END
We created two label because we want to format the category and sub-category label differently.
Final Step
- Drop category and Sub-Category on row shelf
- Bring in min(0) which would be used for label. Make this a Gantt bar and reduce the opacity to 0 since we don’t want to show the gantt. This will only act as a placeholder for adding labels.
- Adjusted Sales should be computed on Sub-Category
- Category label and Sub-Category label has to be computed on Sub-Category
That brings us to the end of this post. Hopefully, I didn’t bore you here :)
Here’s the link to this sample workbook for your reference.
Do you have any other tricks using sub-totals? Share with me in the comments sections.
I hope you have fun learning! Stay safe and happy :)