In this post I’ll show you a few techniques for maintaining your custom color palettes in Tableau, particularly when the values in the dimension are constantly changing or unknown. The basic principle is to create a calculation that takes an list of unknown / changing possibilities into a discrete, unchanging set of values, and assign your color palette to the discrete set.
Note 2016-10-03: Starting in Tableau 10.0, when you assign a color palette, it will be recorded in the workbook and Tableau will automatically restart the cycle with colors from that palette as new items come into the dimension. So most of what lies below is unnecessary in 10.0.
However, the workbook doesn’t store this as a “palette” to use later for additional dimensions; it merely assigns a color value to the dimension values that are visible in the Assign Color menu at that given time. This means the color is locked to that exact dimension value. When the set changes, Tableau randomly assigns colors to the new dimensions that don’t have a custom color assigned, and it always uses the standard Tableau 20. This can really mess with your custom formatted, beautifully designed viz!
Match Color on a Rank
The most common solution is to limit down the total number of results you are going to show to a specific set, and then assign colors on a Ranking calculation. Here’s a quick example using Superstore data. Put Customer Name on Rows and SUM(Sales) on Columns
Duplicate the SUM(Sales) pill by doing a Ctrl-drag
Now (this is new in 9.0), Ctrl-drag the Rank calc over into the Measures pane. You should see a “Rename Field” popup — call it Rank of Sales. Now right-click and convert it to Discrete from Continuous.
You can remove the Rank of Sales from the Columns Shelf, we’re going to use the discrete one from here. Now drag the Rank of Sales from the Measure pane onto the Color shelf. You can assign your custom color palette using the color legend that appears. Assign to as many rank places as you ever expect to show (or even assign it repeating out forever as in this example, where the palette repeats after 5).
The powerful part of this is that as you filter, the order of colors remains the same
And of course you can filter down to a smaller number (by filtering on that same rank calculation or any other Top N calc).
You don’t have to use the same calculation as what you are showing; you can modify the rank calc however you’d by editing the Calculated Field. In fact, the default RANK() function will assign the same rank if the values of the measure are identical. If you want to make sure you never have the same colors next to each other, you’ll need to switch to RANK_UNIQUE()
Fixing the Color to dimension labels randomly
You might find you have a different requirement, which is that a dimension should always maintain its color, no matter where it is in the rank. If these colors need to be assigned randomly, you just need some algorithm for converting the text into a fixed set of numeric values that don’t change.
A simple example would be that gives ten options would be
LEN([Customer Name]) % 10
You could also do the first two letters and come up with a pattern. A good algorithm of this type will always result in the same number from the same text, which gives a consistent color. The tradeoff is that you might have the same colors next to each other. You have to decide what you want — alternating colors or colors that stay “fixed” to a particular label. You can’t have it both ways.
assigning dimension colors manually
If you have the need to assign particular colors to particular text labels, but also have a custom color palette assigned randomly, you can create a color table in your database that matches Dimension values to a smaller discrete set of color codes (numeric or text). You would join this extra table into your data, and then put the “Color Code” on the color shelf.
You can combine this technique with either of the other two mechanisms to randomly assign identifiers. For example, if the domain of the [Color Code] field in your in your database table is 0-9, you could do the following:
IF ISNULL([Color Code]) THEN
LEN([Customer Name]) % 10
ELSE [Color Code]
This assigns the desired code to any known value, then gives a random value to any of the additional ones.