Dynamic cohort analysis in Tableau…in one chart

Screenshot of design team mockup, to be implemented in Tableau

The challenge

Our design team recently came up with an analysis sheet that enabled users to define any cohort and compare to any other cohort they like. On the surface it may not sound challenging: create a visualization for each cohort and give each visualization its own set of filters.

Not so fast, though! Both cohorts are in the same visualization (ex., Cohort 1, Cohort 2, Delta, Delta %, …), which is more challenging to implement in Tableau. This ruled out two different sets of filters because there is only one set of filters for the viz. You may also consider the common workaround of overlaying two similar charts, but that still didn't enable us to calculating deltas between the cohorts or even keep the axes in sync with unpredictable cohort sizes.

The last material requirement was that the cohorts must be completely independent. We were comparing two sets of data that could overlap, not in the cohort to out of the cohort, as in many examples I found.

The solution: Set Controls

I had a sense that Sets would help, if I could make it easy to govern what went into the sets. Fortunately, Set Controls were introduced in 2020. Set Controls look like filters but really facilitate creating arrays of values that make up a Set. Making selections in Set Controls, i.e., changing Set membership, does not affect any visualization unless you explicitly refer to the Sets in calculations.

Set Controls (they just look like filters)

This KB article ("Create a Multiple Value Selection Parameter"?) seeded the solution in my mind, which shows an example of how to do this using one field. In my case, we needed to account for multiple filters you may use to define the Sets, so it is a slight extension of the concept.

Follow along

Here are the steps to implement, using the 2022.4 version of Superstore. 

Create a new Worksheet and select the Sample - Superstore data source, which contains Profit and a handful of interesting filters we can use to define our cohorts. We are going to compare the Profitability between two dynamic cohorts.

Decide what filters you would like to make available to define the cohorts. We'll pick Region, Category, and Subcategory.
Create Sets for those filters. For each of those fields, create two sets: one for cohort A and one for cohort B. Right-click the field name, then Create, Set. You will do this a total of six times. (Technically it will be faster to create Sets for cohort A, duplicate them, then rename for cohort B.)
Create Set using Region

Rename for Cohort A (optionally, click All)

Create Calculated Fields for cohort membership to use in all subsequent calculations, returning a binary true/false. The Set references themselves are also evaluated as a binary true/false. The reason for creating this is to centralize what Fields/Sets we consider for cohort membership, so we don't have to retype the list of Sets in other calculations. (Again, it will likely be faster to create a field for cohort A, then duplicate and edit.)
Repeat this for cohort B, as well

Create measures for each Cohort. You can create any measure you like, following this pattern. I created Profit, Sales, and Profit % (i.e., Profit / Sales) for each cohort.
Create these for cohort A, then duplicate and update for cohort B

Lastly, create a delta Profit % measure, to compare the cohorts.
Now we need to show the Set Controls so an end user can define the cohorts. For each of the six Sets we created, right-click and Show Set. They appear on the right, just as filters would.

Build some visuals using the cohort-specific measures. I created a bar chart, then duplicated the Worksheet and made a pivot table, but go wild. Note that right now, the measure values are identical because the Set membership identical.