Of all the questions on the Tableau forums, one of the more common ones is that people see the Grand Total and/or Subtotal rows – the ones created by going to the Analysis Menu then choosing Totals->Row/Column Grand Totals – not providing the results that they’d expect. And then, of course they’d like to have the total rows provide the desired results. For example, the measure might be a Count Distinct or an average, and the desired total is the sum of all the returned values and not what Tableau generates.
This post goes through the how and why Tableau calculates the totals that it does, and then provides a couple of workarounds.
We’ll start with the Superstore Sales sample data, and a basic cross tab of average sales per year per category:
As noted in the online documentation, when using the Average aggregation Tableau is first calculating the average for each row, then for the Grand Total performs the average at a higher level
, i.e. the average of the averages. A longer description is to say that the Grand Total is calculated by performing the same aggregate calculation as the measure, only taking out whatever dimensions are on Rows or Columns.
Therefore if there is only one dimension like Category in the above example, then Tableau computes the AVG([Sales]) for each member of Category, then for the Grand Total computes the AVG([Sales]) for the whole dataset (having removed Category from the calculation). In the case of other aggregations, Tableau does the same: for example, for SUM([Sales]), Tableau computes the sum of sales for each member of Category, and then in the Grand Total does the SUM([Sales]) across the entire dataset.
In the case of subtotals, Tableau increases the level of aggregation by one dimension at a time and then computes the necessary subtotals for each pane.
The Problem, and Some Solutions
What if the goal is to show the total of the averages in the Grand Total instead of the average? Using the example, we’d want $6602 for the Grand Total instead of $1770. Tableau does not offer any controls for choosing a different aggregation for the Grand Total. However, like most issues in Tableau, there are a variety of workarounds:
- Use a dashboard with two worksheets: one that displays the detail rows, a second that displays the Grand Total row.
- Use a formula to identify whether the row being displayed is a detail row or a Grand Total Row, and either use that formula within a calculated field to return the desired result in each case, or use it as a flag to set a variety of calculated fields. I’ll cover the three formulas I know of in Part 2.
Here’s how you create a dashboard to show a custom grand total:
- Create a worksheet with the data.
- Duplicate the worksheet to create the grand total worksheet.
- If you still need the dimension(s) in order to make the calculations work correctly, drag them from Rows and/or Columns to the Level of Detail Shelf on the Marks Card:
- Either use an existing calculated field or create a custom calculated field to generate the desired total. In this case, since we want the Grand Total to show the sum of an average, I used the following formula:
IF FIRST()==0 THEN WINDOW_SUM(AVG([Sales])) END
The IF FIRST()==0 part uses a technique developed by Richard Leeke and demonstrated in this fabulous post at Clearly and Simply on table calculations. The goal is to reduce the number of rows returned by the table calculation, so that we don’t have overlapping text.
- Create another calculated field – I called it Fake GT Header – that has “Grand Total” as the formula. Drag that onto the Rows Shelf.
- Now to set up the formatting. Right-click on the Grand Total and format the header to be Bold.
- Click on the Borders icon to set the Row Divider->Pane to None.
- Right-click on the blue YEAR(Date) pill on the Columns shelf and uncheck Show Header.
- Now the dashboard is ready to be set up. Create a new dashboard using the Dashboard->New Dashboard menu.
- Drag a Vertical Layout Container onto the dashboard from the Layout Containers section on the left.
- Drag the original data worksheet onto the dashboard from the Dashboard window on the left.
- Drag the grand total worksheet onto the dashboard from the Dashboard window on the left and place it just below the original data worksheet.
- Click on the drop-down arrow in the upper right of the grey worksheet container to uncheck Title, and you’re all set. Here’s how it looks in Tableau Desktop:
This is the approach that Tableau support recommends. It gets the job done, but has some issues:
- There’s unnecessary white space in the view that can’t be removed.
- Any filters or parameter-based calculations will need to be duplicated in both worksheets.
- Any selections in the data worksheet to exclude marks won’t be reflected on the grand total worksheet.
- If you’re using a hierarchy, expanding the hierarchy in the data worksheet will have no effect on the grand total worksheet, leading to an ugly view.
The alternative is to get more friendly with how Tableau aggregates data and computes Grand Totals, and more intimate with table calculations. If that sounds like fun to you, look for Part 2 and Part 3. And, here’s the workbook on Tableau Public: