Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 35 ยท Advanced Analytics

Clustering and Statistics in Tableau

Group marks with Tableau's cluster feature, understand k-means, standardization and the Calinski-Harabasz score, and reproduce a clustering with NumPy.

Sometimes you do not know the categories in advance. Which customers behave alike? Which stores form a natural group? Clustering finds groups in your data by similarity, without you defining the rules. Tableau builds it into the Analytics pane: drag Cluster onto a scatter plot and the marks are coloured by group within seconds. It is quick, but it is not magic, so it pays to know what the algorithm does and what it needs from you.

Tableau runs as a desktop and server application, so the steps here are instructions for it. The code is a stand-in: a small k-means written with NumPy that follows the same recipe (standardize, assign, update) and runs in your browser. Tableau's own implementation is more refined, for example in how it picks starting centres and the best number of clusters, and the lesson says where.

Creating clusters

  1. Build a view with at least two measures, typically a scatter plot such as Sales on Rows and Profit on Columns with Customer Name (or any dimension) on Detail so each customer is one mark.
  2. Open the Analytics pane and drag Cluster into the view (or right-click a mark area > Cluster).
  3. Tableau adds a Clusters field to the Color shelf and shows a dialog. By default it chooses the number of clusters automatically. You can type a number instead, and drag extra Variables into the dialog so more fields take part.
  4. Right-click the Clusters pill > Describe Clusters to see a summary: the number of items per cluster, the average of each variable, and diagnostics such as the sum of squares between and within groups.
  5. To reuse the result, drag the Clusters pill to the Data pane. It becomes a saved group field that you can filter, put on a dashboard or use on another sheet.

Clustering in Tableau

Two measures Build a scatter plot with a detail dimension.
Drag Cluster From the Analytics pane onto the view.
Read the colours Marks with the same colour are alike.
Save it Drag the pill into the Data pane.
1 / 2

What k-means does

Tableau uses the k-means algorithm. The idea in four steps:

  1. Choose k starting centres (the points that will represent each cluster).
  2. Assign every mark to its nearest centre.
  3. Update each centre to the average position of the marks assigned to it.
  4. Repeat 2 and 3 until nothing changes.

Two details deserve attention. First, standardization. Distance is only meaningful if the variables are on similar scales. Sales in dollars (thousands) next to a discount rate (0 to 1) would let sales dominate completely. Tableau therefore standardizes each variable: subtract the mean and divide by the standard deviation (a z-score), so every variable has mean 0 and spread 1. Second, starting centres matter: a poor start can give a poor result. Tableau picks starting points in a smart way and runs the algorithm repeatedly, which a hand-written version often does not.

A k-means stand-in

The data below is eight customers with annual spend (in dollars) and orders per year. To keep the result reproducible, the stand-in picks starting centres deterministically: first the point farthest from the overall mean, then repeatedly the point farthest from the centres chosen so far.

Python K-means with NumPy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You get three groups: small, mid-sized and heavy customers. The cluster numbers are arbitrary (cluster 0 is not "first" or "best"); Tableau labels them 1, 2, 3 and the numbers can change when the data refreshes. Never build logic on a cluster number, but name the groups yourself after you inspect their averages (for example "Occasional", "Regular", "Power users").

Choosing the number of clusters

If you leave the number blank, Tableau tests several values of k and chooses the one with the best Calinski-Harabasz index. This index compares how far apart the clusters are with how tight each cluster is: a higher value means well-separated, compact groups. You can compute it yourself.

Python Calinski-Harabasz for k = 2, 3, 4

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The k with the highest score wins, and here that is 4 (415.7 against 217.6 for k = 3). With only eight points, splitting further almost always tightens the groups, which is a reminder to treat the score as a hint on small data. The score favours tidy, separated groups, but it does not know your business: a k that scores best may still be useless if the groups do not map to an action you can take. When that happens, set k manually.

Cluster results depend on the variables and the marks in the view. Change the level of detail (customers vs. orders), filter some marks away, or add another variable, and the clusters are recalculated. If the clusters are filtered by a normal dimension filter, they are recomputed on the remaining marks. Use a context filter or save the cluster as a group first if the groups should stay fixed.

Describing clusters

Describe Clusters has two tabs:

  • Summary shows the model: the variables used, the number of clusters, the sum of squares between groups and within groups and the total. A high between-groups share means well-separated clusters.
  • Models shows, for each cluster, the number of items and the centroid (average) of each variable in original units, which is what you need to name the groups.

The centroid table is the most valuable thing the dialog gives you. Read it like a persona sheet: "Cluster 3: 40 orders a year, $2,900 spend".

Other statistical helpers

Besides clustering, the Analytics pane's Model section includes Average with 95% CI (the mean of the marks with a confidence interval) and the Trend Line covered in Trend Lines and Forecasting. For richer statistics (regression with several predictors, hypothesis tests), connect Tableau to R or Python through TabPy or Analytics Extensions and call a script from a calculated field, or run the analysis in pandas and bring the result back as a data source. Statistics belong where you can inspect and test them; Tableau is an excellent place to show them.

Common mistakes

  • Clustering on one variable. A single measure gives a simple split into ranges; use a bin or a set instead.
  • Highly correlated variables. If two variables say the same thing, that dimension counts double.
  • Reading causes into clusters. Groups describe similarity, not why.
  • Using clusters as permanent IDs. Save as a group and name them; do not rely on the number.
  • Outliers. One extreme customer can claim a whole cluster for itself. Check with a box plot first (reference lines).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does Tableau standardize the variables before clustering?

    Distances depend on scale. Z-scores give every variable the same footing.
  2. 2. You leave the number of clusters empty. How does Tableau decide?

    Tableau tests several values of k and keeps the one with the highest Calinski-Harabasz index.
  3. 3. What is the best way to keep a clustering stable for a dashboard?

    Saving the pill as a group freezes the membership and lets you give meaningful names.
  4. 4. A single measure with extreme outliers is clustered together with others. What is the likely effect?

    K-means is sensitive to outliers; an extreme point can capture a cluster or pull a centre.

Summary

  • Drag Cluster from the Analytics pane onto a view with two or more measures.
  • Tableau uses k-means on standardized variables and picks k with the Calinski-Harabasz index unless you set it.
  • Read Describe Clusters to name the groups; save them as a group to keep them stable.
  • The NumPy stand-in follows the same steps but with simpler starting centres.
  • Watch for outliers, correlated variables and filter changes.

Next: Stories and Word Clouds