Published: 29 Sep 2016 Last modified date: 20 Jul 2023
Question
How to add a label to the top of a stacked bar chart.
For example, adding the total sales to the top of a stacked bar chart where every segment is a region's sales (option 1). Or, adding a label for the percent of total central region sales to the top of the stacked bar chart (option 2).
Environment
Tableau Desktop
Answer
Option 1: Add a total reference line
These directions begin with the stacked bar already created. For full directions, see the "Option 1" worksheet in the workbook downloadable from the right-hand pane of this article
Right-click the Sales axis in the view and select Add Reference Line
In the Add Reference Line, Band or Box dialog, do the following and click OK:
For Scope, select Per Cell
For Value, select SUM(Sales) and Total
For Label, select Value
For Line, select None
Right-click one of the reference values in the view and select Format...
In the left-hand Format Reference Line pane, under Alignment, select Centre for Horizontal
Option 2: Create a dual axis graph with the stacked bar on one axis and the total/label on the other axis.
Use this option when the value to add at the top of the stacked bar chart is not the total. These directions begin with the stacked bar already created. For full directions, see the "Option 2" worksheet in the workbook downloadable from the right-hand pane of this article
Create a new calculated field called "Total Sales per Category" and use the following formula:
IF FIRST() = 0 THEN SUM({FIXED [Category]: SUM([Sales])}) END
Drag [Total Sales per Category] to the Columns shelf next to SUM(Sales)
Right-click on [Total Sales per Category] on the Columns shelf, and select Compute Using > Region
Open the [Total Sales per Category] the Marks Card and move Region from Colour to Detail
On the same Marks Card tab, change the chart type from Automatic to Gantt Bar
Right-click the NULL indicator in the view and select Hide
Create a new calculated field called "Percent of Total Label - Central" and use the following formula:
SUM( IF [Region] = "Central" THEN [Sales] END) / SUM({FIXED [Category]: SUM([Sales])})
Drag [Percent of Total Label - Central] to Label on the [Total Sales per Category] Marks Card tab
Right-click on [Percent of Total Label - Central] on the Marks card and select Format...
Change the number format to Percentage.
Right-click on [Total Sales per Category] on the Columns shelf, and select Dual Axis
Synchronise the axis
Thank you for providing your feedback on the effectiveness of the article.