Scenario: The dataset that you’re working with in Tableau has several dimensions that represent different customer segments such as Married vs. Single, New vs. Returning, Customer , or whatever segments are important to your business. It’s good that these dimensions are available to slice and dice the data by, but it’d be great if you and your end users could change these on the fly, without having to drag and drop the segment of interest onto the view every time you wanted to slice the data by a different segment. It would be even more amazing if you could somehow create a segment hierarchy, allowing you to drill down to different combinations of segments (i.e. Married or Single in the first column drills down to Married or Single, plus a second column for New or Returning).
Solution: Parameters will be our solution to dynamically create and compare segments. In this case, we will create two string-based parameters that will be used to create our combinations of segments.
Step 1 – Create a parameter for segment 1
For demonstration purposes, I am going to use the dimensions available out of the box with Tableau’s Sample Superstore data. You will likely have segments that are more practical in your data, such as the examples mentioned in the opening sentence above (Marital Status, New vs. Returning, etc.).
Create a string parameter with a list of the dimensions you want to be able to use in your segmentation:
Step 2 – Create a calculated dimension for segment 1
Remember, parameters are dependent, and do nothing on their own. In order to get this parameter to eventually work in our views, we need to create a calculated field that gives Tableau instructions on how to use the parameter. To create our first segmentation, we need to tell Tableau which dimension to display based on the selection made in the Segment 1 parameter. This turns out to be a simple mapping exercise, with the following logic:
CASE [Parameters].[Segment 1]
WHEN “Segment” THEN [Segment]
WHEN “Ship Mode” THEN [Ship Mode]
WHEN “State” THEN [State]
WHEN “Category” THEN [Category]
WHEN “Ship Mode” THEN [Ship Mode]
WHEN “Sub-Category” THEN [Sub-Category]
WHEN “Region” THEN [Region]
Step 3 – Create a parameter for segment 2
If you want to have multi-level segmentation, create a second parameter for Segment 2. This can be done by right-clicking on the Segment 1 parameter and clicking “Duplicate”. Edit the copy and rename it “Segment 2”.
Step 4 – Create a calculated dimension for segment 2
Instead of retyping all of the logic used to create the Segment 1 calculated field, just right-click on the Segment 1 dimension and choose “Duplicate”. The only trick here is that you not only want to rename the copy to “Segment 2”, but you also want to change the parameter being used in the logic. To do this, right-click on the Segment 1 copy, click “Edit”, and replace the number “1” with “2” in the first line. At this point, your calculated field for Segment 2 should look like this:
We now have the components needed to create a view that allows you and your end users to choose any combination of two segments. Here is a sparklines view with both segments:
Creating sparklines is beyond the scope of this post (see How to Make Sparklines in Tableau), but the key is to right-click on the two parameters we created for segments and choose “Show Parameter Control”. This is what gives users the ability to change Segment 1 and Segment 2 on the view.
This is awesome; please tell me other ways this can be used!
Option 1 – Create a custom hierarchy with your newly created segments
By creating a custom hierarchy using the Segment 1 and Segment 2 calculated fields, you and your end users will be able to drill down to the secondary level of segmentation and back up to the primary level of segmentation:
Option 2 – Create additional levels of segmentation
You are not limited to two combinations of segments, though usability may become messy past three or so. If you would like to create more than two levels of segmentation, simply repeat steps three and four above.
Option 3 – Save a specific combination of segments for future use
If you find a combination of segments that is particularly interesting, Tableau makes it very easy to create a set straight from the view. These sets can be reused on different worksheets for deeper analysis. Let’s say we want to isolate the Ship Mode: First Class > Category: Office Supplies combination for the months on our view. Simply click on the axis at the deepest level of segmentation, and click the Venn Diagram icon to create a set:
This set will appear in your left navigation and can now be used as a dimension on views, to filter, or within calculated fields.
Segmentation is one of the best ways to create comparisons that add value to your data visualization. The next time you are mining for stories in your own data, consider leveraging parameters to create custom segmentations on the fly.
Thanks for reading,
This content is excerpted from my book Practical Tableau: 100 Tips, Tutorials, and Strategies from a Tableau Zen Master published by O’Reilly Media Inc., 2018, ISBN: 978-1491977316. Get the book at Amazon.