How to Shade between 2 Lines in Tableau

I have been playing around with Polygons a lot more in Tableau lately, and am starting to really like them. Polygons are very valuable, they are often used with Maps, or for sankeys/area charts, but they are also beneficial for emphasizing gaps between two lines.



This example shows how to create a Polygon to shade between two lines. This method uses no data prep, and only 1 calculation. This is an alternative to the Area Chart version, and I think slightly cleaner.

To begin, I am going to load up Superstore Dataset. I'm going to look at the spread difference from Running Profit between Furniture and Technology Sales by Month. My goal is to create a shaded area between the two lines.


In order to create a Polygon in Tableau, I need to define a Path that start from left to right for one dim, and then continues left to right from the other dim. So I want to figure out how to count up from one dim until the line ends, and then continue that iteration for the second line in the opposite direction.


This calculation is figuring out whatever the Max category is on the chart. It doesn't matter which one it is, I just want it to be dynamic in case I filter to two different categories. For the "Max" category, I take the index and count up from left to right, then on the second category, I take the Total Marks (Size) for both dimensions and subtract the INDEX for the second dimension, finally add the SIZE() again.

Place this calc on the Detail shelf and configure like so.



As you can see, Tableau connects the End Lines together. From here simply change the mark type to Polygon, and do some cleanup.


Next, Hold Control and Drag the Running Sum of Profit so it creates a Second Axis. Change the Mark Type to Line, remove the LinePath calc, and put category on the Color Shelf.


Finally, Dual Axis and Synchronize the Axis. And that's it! Add labels as wanted, and do any additional cleanup.



Update Bonus!

SirVizAlot Matt Chambers shared with me this blog post http://sirvizalot.blogspot.com/2017/02/how-to-shaded-slope-charts-in-tableau.html on how to create the shade using Area Charts.



I really like how it shows the other category, so I'm going to show how to change some calcs from above to resemble this one.

We need the Polygon to only create marks for the "Primary" categories we want to highlight, so we are going to create this calc in place of the "Category" field on the Polygon Mark.


Unfortunately, we can't just do this, we also have to NULL out the Values for the other Categories, so we are going to create a new Y measure specifically for the Polygon.


Now that we have this, we can simply substitute Categories with PrimaryCategories on the Polygon Mark, and the Running Profit with the Y1 calc and now there you have it!



UPDATE 2 for Steve Wexler!

Steve asked me on Twitter is this could be used for Measure Names/Values such that you could shade the Delta between Profit and Sales (for example). My initial response was "Not without Pivoting the Measures"...but as I got to thinking about it, that not exactly true. In fact, as long as your Data has column with at least 2 distinct values, you can do this, but not exactly with Measure Names/Values.

The how is actually kind of Simple. We are going to use the same method from before, we are just going to add some prep-calcs. What we want to do is create a Dimension that can serve as our "Measure Names" and Measure that will, well server as our Measure. So we'll start with the "Measure Names"


Here, all I'm doing is essentially creating an "Alias" dimension, I'm doing this for simplicity because when I go to build the chart, I want to drag this onto the Filter Shelf and remove the Null Values. Aka, I don't want to hunt through an ever changing list to figure out what to filter out. I could have used an ELSE statement for everything else, but this just makes sense to me.

Now we need to create the Measure. Behold LoDs!



BIG NOTE* on this method. You want to use a dim that is present in every Month/Year/Whatever you are using on the X Axis, or else this won't work. So to use this method, you want to be weary of "missing" dimensions. Not 100% but it probably will work with a lot of datasources without doing padding.

Now that we have those Calculations, we just repeat the same methods from the top, we simply use the Fake Dims and the Measure calc in substitution. NOTE* since we use LoDs for the Measure, you'll want to use MIN/MAX instead of SUM, in order to avoid incorrect values.



Hope this helps!

Rody
Previous
Next Post »
0 Comment