Excel has no built-in k-means button, but you can run a proper k-means clustering entirely inside a spreadsheet using formulas and a short manual iteration loop. The method below assigns each data point to the nearest of k centroids, recalculates each centroid as the average of its assigned points, and repeats until the assignments stop changing. It works in Excel for Microsoft 365, Excel 2021, and Excel 2019 on Windows and Mac; menu labels can differ slightly by version and region.

What You'll Need

  • A table of numeric data with one row per item and one column per feature (for example, customer ID, annual spend, visit frequency).
  • All feature columns must be numeric and on comparable scales. If one column ranges into the thousands and another stays between 0 and 1, standardize first (see Step 2).
  • Excel with the standard functions AVERAGEIF, SQRT, SUMPRODUCT, MIN, and IF. No add-in is required.
  • A decision on k, the number of clusters. Start with 2 to 5 for small datasets and compare results.
  • Optional: the Analysis ToolPak for descriptive statistics, enabled via File > Options > Add-ins on Windows or Tools > Excel Add-ins on Mac. Labels vary by version.

Step-by-Step

  1. Lay out your data cleanly. Put headers in row 1 and data starting in row 2. Remove blank rows, text notes, and merged cells. Give each feature column a short header such as Spend and Visits. Note the last data row so you can size ranges correctly.
  2. Standardize the features. In a helper area, compute each column's mean with AVERAGE and its standard deviation with STDEV.S. Create a z-score column for each feature using =(original_cell - mean)/stdev, with the mean and stdev locked as absolute references. Clustering on raw values lets large-number columns dominate the distance calculation.
  3. Choose starting centroids. Pick k actual rows spread across the data (for example the first, middle, and last rows) and copy their z-score values into a small centroid table with one row per cluster and one column per feature. Label the rows Cluster 1, Cluster 2, and so on. Random starting points are fine; you will rerun with different seeds to check stability.
  4. Calculate each point's distance to each centroid. Add one helper column per cluster. For cluster 1, the formula is =SQRT(SUMPRODUCT((B2:D2-$G$2:$I$2)^2)) where B2:D2 is the point's z-scores and G2:I2 is the centroid's values. Use absolute references for the centroid range so the formula fills down and across correctly. Repeat for each cluster column.
  5. Assign each point to its nearest centroid. Add an Assignment column using =MATCH(MIN(F2:H2),F2:H2,0), which returns 1, 2, or 3 for the closest cluster. This is the current cluster label for that row.
  6. Recalculate the centroids. In the centroid table, replace each value with an AVERAGEIF over the z-score column, conditioned on the Assignment column. For example, =AVERAGEIF($E$2:$E$100,1,B$2:B$100) averages feature B for all rows assigned to cluster 1. Fill across all features and down all clusters.
  7. Iterate until stable. Because the centroid cells changed, the distances and assignments recalculate automatically. Copy the Assignment column and paste it as values into a spare column, then compare it with the new Assignment column using a formula such as =SUMPRODUCT(--(old_range<>new_range)). When that count reaches 0, the clusters have stopped moving and the run is complete.
  8. Review and interpret. Add a summary table using COUNTIF on the Assignment column to see cluster sizes, and AVERAGEIF on the original (unstandardized) columns to describe each cluster in real units. Name the clusters in plain language, such as high-spend frequent buyers.
  9. Test alternative k values. Rerun Steps 3 to 7 with a different k and a different set of starting rows. Compare cluster sizes and average feature values. Choose the k that produces distinct, business-meaningful groups rather than the one with the most clusters.

Common Mistakes

Clustering raw values without standardizing is the most frequent error; distance is dominated by whichever column has the largest numbers. Mixing text or blanks into feature columns causes SUMPRODUCT to return errors. Using relative instead of absolute references in the distance formula breaks when you fill down. Forgetting to paste assignments as values before comparing iterations makes the stability check meaningless. Finally, treating k-means as definitive: it finds spherical, similarly sized groups and will split a single natural group if you ask for too many clusters.

Tips

Sort your data by one key feature before picking starting rows so the initial centroids are spread out. Keep the z-score helper columns on a separate sheet to keep the working area readable. Use conditional formatting on the Assignment column to see clusters at a glance. Run each k three times with different starting rows; if the cluster sizes change a lot, the result is unstable and you should prefer a simpler k. Document the mean and standard deviation used for standardization so new data can be scored consistently.

Troubleshooting

If every point lands in one cluster, your starting centroids are probably too close together; pick rows from opposite ends of the sorted data. If AVERAGEIF returns #DIV/0!, a cluster received no points, which means k is too high or the starting centroids were poorly placed. If distances return #VALUE!, check for text, spaces, or blank cells in the feature columns. If the iteration count never reaches zero, look for rows sitting exactly between two centroids; rounding differences can flip them back and forth, so stop when the count is small and stable rather than exactly zero. If results differ wildly between runs, standardize again and confirm the absolute references are correct.

Frequently Asked Questions

Does Excel have a built-in k-means tool?

No. Excel ships no k-means feature. You build it with formulas as described, or use an external statistics package and bring the results back into Excel.

How do I choose the right number of clusters?

There is no single formula. Run several k values and compare cluster sizes and average feature values; pick the grouping that is distinct and useful for your decision.

Do I have to standardize the data?

Yes, whenever features use different units or ranges. Without standardization, the largest-scale column controls the distance calculation.

Can I cluster text or categories?

Not directly. Convert categories to numeric codes or indicator columns first, and keep identifiers such as customer ID out of the feature set.

Why do my clusters change every time I rerun?

K-means depends on the starting centroids. Different starting rows can produce different groupings, especially with overlapping data. Rerun several times and keep the most stable result.

Can I score new data later?

Yes. Save the final centroid values and the standardization mean and standard deviation, then apply the same distance and MATCH formulas to the new rows.