The goal of this article is to help someone learn how to compute the difference between two measure fields in reference to a dimension using Tableau. A good business question would be, computing the difference (gap) between demand and supply over different months of year.
Using Traffic Incidences data sets, we’ll recreate the chart below showing the existing gap between the number Injured and Deaths recorded for different months.
Note: This article is for learning purpose, but you’ll find applicable scenarios to use this technique in real life.
Create a side–by–side bar chart
Once connected to the data set in the link above;
Change the String dimension field Date YMD data type to Date.
Drag now Date dimension field Date YMD to the columns shelf, choose level of details as month.
Drag both measure fields Deaths and Injured to the rows shelf.
Change the chart to side-by-side bars by selecting side-by-side bars under Show Me tab.
Create calculated field – Gap - to compute the difference between Injured and Deaths.
Create a calculated field – Gap condition – to inform how the gap will be interpreted.
Create a calculated field – Absolute gap – to eliminate negative values in the gap.
Create a calculated field – Main gap – to determine where the gap will begin from.
Note: The implication to this calculation is, if SUM(Injured) is greater than SUM(Deaths) then let the gap begin at SUM(Deaths) else let the gap begin at SUM(Injured).
Add calculated field -Main gap – to the view.
Drag the measure field Main gap to the rows shelf.
Make the chart dual and synchronize the axis.
On the legend, sort the fields by dragging field Main gap to the center.
Change the second chart - Main gap – to a Gantt Bar chart.
Fill the gap.
To fill the gap between the two measure field.
While still at the second chart Gantt Bar drag calculated field - Absolute gap – to the size shelf.
Add visual cue to differentiate the nature of gap by dragging the calculated field – Gap condition - to the color shelf of the second chart and customize colors appropriately.
Label the gaps
While at the second chart, drag calculated field – Absolute gap – to the label shelf, align the label and make any other customization you wish.
I hope this article was helpful in your journey to Tableau mastery.
Thanks for reading.