Tabellab

Building a Cross-Tab from Raw Rows with COUNTIFS

Lesson 1 of 20Lab

All lessons
  1. Building a Cross-Tab from Raw Rows with COUNTIFSLabFree
  2. Row %, Column % and Total %: Asking the Right QuestionLab Locked
  3. Expected Counts: What Independence Would Look LikeLab Locked
  4. Chi-Square Test of Independence: by Hand and with CHISQ.TESTLab Locked
  5. Cramér's V: How Strong Is the Association?Lab Locked
  6. 2 × 2 Tables: Risk Difference, Relative Risk and Odds Ratio with a CILab Locked
  7. Small Samples: Expected Counts Below 5, Combining Categories & Fisher's Exact TestLab Locked
  8. McNemar's Test for Paired Yes/No Answers + a 100% Stacked ChartLab Locked
  9. Goodness of Fit: Do Weekday Rentals Match the Fleet Plan?Lab Locked
  10. Which Cells Drive the Result? Adjusted Residuals & a Bonferroni Cut-OffLab Locked
  11. 2 × 2 Tables: the Two-Proportion z-Test, z² = χ² and Yates' CorrectionLab Locked
  12. Confidence Intervals for the Risk Difference and Relative Risk + Number Needed to TreatLab Locked
  13. The Likelihood-Ratio G-Test: an Alternative to Pearson's Chi-SquareLab Locked
  14. Simpson's Paradox: When the Pooled Table Points the Wrong WayLab Locked
  15. Mantel-Haenszel: One Adjusted Odds Ratio Across Strata + the CMH TestLab Locked
  16. Ordered Categories: a Chi-Square Test for Trend Across Age GroupsLab Locked
  17. Comparing Many Proportions: the 2 × k Test and Marascuilo Pairwise RangesLab Locked
  18. Beyond Cramér's V: Phi, the Contingency Coefficient and Goodman-Kruskal LambdaLab Locked
  19. Square Tables: Cohen's Kappa for Agreement and Bowker's Test for ChangeLab Locked
  20. Final Project: Categorical Dashboard for a Climbing Gym SurveyLab Locked

You are not signed in — the lab works, but your progress is not saved. Sign up

Lesson 1 of 20 is free. Unlock all 20 hands-on labs, the final exam and your certificate.

Buy course — $9.90
Loading the spreadsheet…

Two columns of labels, one question: are they related?

A surprising amount of business data is not numbers at all. It is labels: the plan a customer is on, the channel an order came from, the region of a store, the answer to a survey question, “yes” or “no” to a reminder email. As soon as you have two such columns, the natural question is whether they are connected. Do annual subscribers use the product for different reasons than casual users? Do customers from one channel complain more often? Is a reminder linked to more renewals?

This lesson is for analysts, marketers, product managers, students writing a thesis with survey data, and anyone who has been handed a long export of rows and asked “so what does it show?”. The answer always starts with the same tool: the cross-tab, also called a contingency table or a two-way frequency table. Before any chi-square test, any percentage or any chart, you need a clean table of counts that you can trust. Building that table with one transparent formula, and proving that no row went missing, is what the free lab above is about.

We use Larkfield Bikes, a fictional city bike-share that asked its riders two questions: which plan they are on (Casual, Monthly or Annual) and what the purpose of their last trip was (Commute, Leisure or Errand). The lab shows the first 40 survey rows. Later lessons in the course work with the full 600-rider survey.

What a cross-tab is, in plain words

A variable is categorical when its values are labels rather than measurements. You can count how often each label occurs, but adding or averaging the labels makes no sense. A cross-tab puts one categorical variable in the rows and another in the columns, and every cell holds the number of cases that have that row label and that column label at the same time. The row totals tell you how many riders are on each plan, the column totals how many trips had each purpose, and the bottom-right corner is the grand total.

Excel can build such a table with a PivotTable, and that is often the right choice for a quick look. But a formula-based cross-tab has three advantages that matter for analysis. You can see exactly what each cell counts. It updates instantly when the raw data change, without pressing Refresh. And it can sit next to other formula blocks (percentages, expected counts, residuals) that refer to it cell by cell, which is how the rest of this course builds the chi-square test.

The function that does the work is COUNTIFS. It counts the rows that satisfy several conditions at once: COUNTIFS(range1, criterion1, range2, criterion2, …). For a cross-tab you need exactly two pairs: the plan column must equal the row label, and the purpose column must equal the column label.

The trick: one formula with mixed references

The cross-tab sits in A1:E5. The row labels Casual, Monthly and Annual are in A2:A4, the column labels Commute, Leisure and Errand in B1:D1. The raw data are in A12:D51: ID, plan, purpose and trips per month. The formula in B2 is:

=COUNTIFS($B$12:$B$51,$A2,$C$12:$C$51,B$1)

Read it from the inside out. $B$12:$B$51 and $C$12:$C$51 are fully locked, because every cell of the table must look at the same data. $A2 locks the column but not the row: when you copy the formula to the right it still reads the plan label in column A, and when you copy it down it moves to A3 and A4. B$1 does the opposite: the row is locked to the header row, the column moves along with the formula. The result is one formula you write once and copy into all nine cells, and every cell counts its own combination. This pattern, a mixed reference for the row label and another for the column label, works for any two-way table in Excel, from cross-tabs to price grids and multiplication tables.

Here is the finished table for the 40 rows in the lab, with the totals that the sheet already contains (=SUM(B2:D2) for each row, =SUM(B2:B4) for each column):

Cross-tab of the first 40 Larkfield Bikes survey rows (fictional data)
Plan × purposeCommuteLeisureErrandTotal
Casual18211
Monthly45514
Annual93113
Total1416838

Even before any test, the pattern is visible. Nine of the 13 Annual riders were commuting, while eight of the 11 Casual riders were out for leisure. Monthly riders are spread across all three purposes. A careful analyst still asks whether such a pattern could be chance, and that question is answered by the chi-square test in lesson 4. With only 38 riders some cells are very small, which is exactly why the course moves to the full 600-rider survey for the tests.

Larkfield Bikes: riders by plan and purpose (n = 38)0246810182Casual455Monthly931AnnualRidersCommuteLeisureErrand2 riders gaveno purpose
Figure 1. The cross-tab as a grouped column chart. Each group is a plan, each colour a purpose. The opposite shapes of Casual and Annual are the association the rest of the course measures.

Reconcile: did every row make it into the table?

The grand total in E5 is 38, but the sheet has 40 rows of data. Two riders are missing, and finding out why is not a formality. A cross-tab that silently drops rows can bias every percentage built on top of it. The lab asks you to prove the reconciliation with two formulas:

  • Rows in the data (B7): =COUNTA(A12:A51) counts the non-empty ID cells. COUNTA is needed because the IDs (R001, R002, …) are text; COUNT only counts numbers and would return 0.
  • Rows missing from the cross-tab (B8): =B7-E5, or directly =COUNTBLANK(C12:C51). Both give 2: rider R014 (Monthly) and rider R030 (Casual) left the purpose blank.

Now you can write an honest note under the table: “38 of 40 riders; 2 gave no purpose.” If the difference had been caused by a typo such as “Comute” or “commute ” with a trailing space, COUNTIFS would also have skipped those rows, and the reconciliation would have caught it. Matching in COUNTIFS ignores upper and lower case, but it does not ignore extra spaces, so TRIM on imported labels is a good habit.

From raw rows to a reconciled cross-tab40 raw rowsID · plan · purpose· trips/monthOne COUNTIFS$A2 = plan labelB$1 = purpose labelCross-tab 3 × 3grand total 38Not in the table2 blank purposesCheck: COUNTA(IDs) 40 = grand total 38 + 2 rows with no purpose.If the two sides differ, a label is misspelled or a category is missing from the table.
Figure 2. The reconciliation logic. Every raw row must end up either inside the table or in an explained remainder.

More than counts: a mean per cell with AVERAGEIFS

A cross-tab can hold any summary, not only counts. The survey also recorded trips per month, and the lab's last task asks for the average of Annual riders whose purpose is Commute. AVERAGEIFS takes the value range first, then the condition pairs: =AVERAGEIFS($D$12:$D$51,$B$12:$B$51,A4,$C$12:$C$51,B1). The nine matching riders made 21, 18, 23, 19, 23, 19, 21, 24 and 20 trips, so the result is 188 ÷ 9 = 20.89 trips per month.

With mixed references ($A2 and B$1 again) the same formula fills a full table of means:

Mean trips per month per cell, built with AVERAGEIFS, with the cell count next to it
Avg trips/monthCommuteLeisureErrand
Casual16.0 (n = 1)3.1 (n = 8)4.5 (n = 2)
Monthly18.5 (n = 4)5.6 (n = 5)8.8 (n = 5)
Annual20.9 (n = 9)10.7 (n = 3)14.0 (n = 1)

Always show the count next to a mean per cell. The Casual × Commute mean of 16 trips is one single rider; it says nothing about casual commuters in general. And if a combination has no riders at all, AVERAGEIFS returns #DIV/0!. Wrap it as =IFERROR(AVERAGEIFS(…),"–") to show a dash instead.

Step by step in Excel

  1. Check the raw data. One row per respondent, one column per variable, a header row on top. Use Data › Sort & Filter › Filter and open the filter list of each label column to see every distinct value. Misspellings and blanks show up immediately.
  2. Type the labels. Put the row categories in A2:A4 and the column categories in B1:D1, spelled exactly as in the data. In Excel 365 you can generate them with =SORT(UNIQUE(B12:B51)) and =TRANSPOSE(SORT(UNIQUE(C12:C51))), but remember that UNIQUE also returns a blank if there is one.
  3. Write the first formula. Click B2 and type =COUNTIFS($B$12:$B$51,$A2,$C$12:$C$51,B$1). While editing, put the cursor in a reference and press F4 repeatedly (Fn+F4 on many laptops, Cmd+T on a Mac) to cycle through B12 → $B$12 → B$12 → $B12.
  4. Copy it to the block. Fastest: select B2:D4 first, type the formula (it goes into B2) and press Ctrl+Enter, which enters it into every selected cell with the references adjusted. Alternatively select B2:D2 and press Ctrl+R to fill right, then select B2:D4 and press Ctrl+D to fill down.
  5. Add totals. Select B2:E5 and press Alt+= (AutoSum) to create row and column totals in one go, or type =SUM(B2:D2) and =SUM(B2:B4) by hand.
  6. Reconcile. In B7 type =COUNTA(A12:A51), in B8 =B7-E5. If B8 is not 0, filter the raw data for blanks or unexpected labels and write down what you found.
  7. Add a mean per cell. In B9 type =AVERAGEIFS($D$12:$D$51,$B$12:$B$51,A4,$C$12:$C$51,B1). Format it with one decimal via Ctrl+1 › Number.
  8. Chart it. Select A1:D4 (labels and counts, not the totals) and choose Insert › Charts › Clustered Column. If the plans appear as the legend instead of the axis, use Chart Design › Switch Row/Column.
Change one plan in the raw data, for example R001 from Monthly to Annual, and watch the table, the totals and the reconciliation update at the same moment. That is the advantage of formulas over a static PivotTable.

Cross-tabs and chi-square analysis of categorical survey data in Excel
The course goes from raw survey rows to percentages, expected counts, the chi-square test, effect sizes and paired yes/no data.

A first look at the question behind the test

Counts are hard to compare when the rows have different sizes: 14 Monthly riders, only 11 Casual. Dividing each cell by its row total gives the row percentages, the share of each purpose within one plan (=B2/$E2). For the 38 riders here, 69% of Annual riders were commuting compared with 9% of Casual riders, and 73% of Casual riders were on a leisure trip. Lesson 2 shows when row, column or total percentages answer your question, and why choosing the wrong one is the most common error in survey reports.

Purpose mix within each plan (row %)Casual9%73%18%Monthly29%36%36%Annual69%23%8%CommuteLeisureErrand
Figure 3. Row percentages of the 38 riders as 100% stacked bars. Each bar adds up to 100%, so plans of different sizes become comparable. Percentages from such small rows are fragile; the course uses 600 riders for the real analysis.

With 38 riders, several expected counts would be below 5 (for example Errand riders: 8 in total). The usual chi-square approximation is unreliable in that situation. Lesson 7 shows how to check this rule, combine categories and use Fisher's exact test instead.

Common mistakes and how to spot them

  • Forgetting the $ signs. The formula looks right in B2 but the copied cells count the wrong ranges, or the label references drift to empty cells. Spot it: the grand total is not equal to the number of non-blank rows. Fix: $A2 for the row label, B$1 for the column label, fully locked data ranges.
  • Using COUNTIF with one condition. =COUNTIF($B$12:$B$51,$A4) counts every Annual rider whatever the purpose, so each row shows the same number three times. You need COUNTIFS with both pairs.
  • Data ranges of different lengths. COUNTIFS(B12:B51,…,C12:C50,…) returns #VALUE!. All ranges in COUNTIFS and AVERAGEIFS must have the same size.
  • Labels that almost match. “Annual” and “Annual ” (trailing space) are different for Excel. Spot it with the filter drop-down or =LEN(B12); fix with TRIM or Find & Replace.
  • Ignoring the missing rows. A table of 38 presented as “all riders” hides two non-responses. Always reconcile with COUNTA and report the difference.
  • Counting with COUNT instead of COUNTA. COUNT ignores text, so =COUNT(A12:A51) returns 0 for text IDs. Use COUNTA for text, COUNT for numbers.
  • Reading a mean per cell without its count. A mean based on one rider is an anecdote, not a statistic. Put the n next to each mean.

Quick reference: functions and shortcuts

Excel functions and shortcuts for cross-tabs
Function / shortcutWhat it doesExample
COUNTIFSCounts rows that meet all conditions=COUNTIFS($B$12:$B$51,$A2,$C$12:$C$51,B$1)
AVERAGEIFSMean of a value column for rows that meet all conditions=AVERAGEIFS($D$12:$D$51,$B$12:$B$51,A4,$C$12:$C$51,B1)
COUNTACounts non-empty cells (text and numbers)=COUNTA(A12:A51)
COUNTBLANKCounts empty cells=COUNTBLANK(C12:C51)
SUMRow and column totals=SUM(B2:D2)
UNIQUE / SORTList the distinct labels (Excel 365)=SORT(UNIQUE(B12:B51))
TRIMRemoves extra spaces from labels=TRIM(C12)
IFERRORReplaces #DIV/0! in empty cells=IFERROR(AVERAGEIFS(…),"–")
CHISQ.TESTp-value of the chi-square test (lesson 4)=CHISQ.TEST(observed,expected)
F4Cycle absolute/mixed references$B$12 → B$12 → $B12
Ctrl+D / Ctrl+RFill down / fill rightCopy B2 to the whole block
Alt+=AutoSum for totalsSelect B2:E5, press Alt+=

Real-world uses

  • Customer surveys: satisfaction level by store, age group or product line. A cross-tab with row percentages shows which segment is unhappy, and the chi-square test tells you whether the difference is larger than chance.
  • Marketing: conversion (yes/no) by channel or campaign variant. This is a 2 × k cross-tab, and lesson 6 adds the relative risk and the odds ratio that marketers report.
  • HR and people analytics: department by exit reason, or job level by training participation. Reconciliation matters here, because blank answers are often the most informative ones.
  • Quality and operations: defect type by production line or shift. A cross-tab quickly shows whether one line produces a different mix of defects.
  • Research and theses: any questionnaire with categorical questions. Supervisors expect the counts, the percentages, the chi-square result and an effect size such as Cramér's V, which is exactly the path of this course.

What you will build in the rest of this course

Lesson 1 is free. The other 19 lessons belong to the paid course (one-time $9.90). Each is a hands-on lab with the Larkfield Bikes data, automatic checking and hints, and together they take you from a table of counts to a complete, defensible analysis of categorical data.

Row %, Column % and Total %: Asking the Right Question. You turn the full 600-rider cross-tab into all three kinds of percentages, compare plans in percentage points and chart the row percentages. You learn which denominator answers which question, so your report never says the wrong thing with the right numbers.

Expected Counts: What Independence Would Look Like. You build the table of counts you would expect if plan and purpose were unrelated (row total × column total ÷ grand total), measure how far each cell deviates with standardised residuals and check the minimum expected count.

Chi-Square Test of Independence: by Hand and with CHISQ.TEST. You compute the chi-square statistic cell by cell, convert it to a p-value with CHISQ.DIST.RT and confirm it with CHISQ.TEST, so you understand every number in the output instead of trusting a black box.

Cramér's V: How Strong Is the Association?. A tiny p-value does not mean a strong relationship. You turn chi-square into Cramér's V and watch the p-value collapse with ten times more riders while the effect size stays exactly the same.

2 × 2 Tables: Risk Difference, Relative Risk and Odds Ratio with a CI. From a reminder experiment you calculate the three effect measures every A/B test and medical study uses, and put a 95% confidence interval around the odds ratio on the log scale.

Small Samples: Expected Counts Below 5, Combining Categories & Fisher's Exact Test. You fix a sparse table by merging categories and compute a one-sided Fisher exact p-value for a tiny pilot with COMBIN, the honest answer when the chi-square approximation breaks down.

McNemar's Test for Paired Yes/No Answers + a 100% Stacked Chart. When the same riders answer before and after a campaign, the ordinary chi-square test is wrong. You run McNemar's test in its chi-square and exact form and present the shift in a 100% stacked column chart.

Summary

Every analysis of categorical data starts with a cross-tab you can trust. One COUNTIFS formula with mixed references ($A2 for the row label, B$1 for the column label) fills the whole table; COUNTA and a simple subtraction prove that every row is accounted for; and AVERAGEIFS turns the same layout into a table of means. For the 40 Larkfield Bikes rows, that gives 38 riders in the table, 2 explained blanks, and a clear first pattern: Annual riders commute, Casual riders ride for leisure.

Scroll up and build it yourself in the free lab: the nine-cell cross-tab, the reconciliation and the average trips of Annual commuters. It takes about 15 minutes, and the Coach checks each formula as you go.

Frequently asked questions

How do I make a cross-tab in Excel without a PivotTable?

Type the row labels in a column and the column labels in a row, then use =COUNTIFS(rowdata,$A2,coldata,B$1) in the first cell and copy it across the table. The mixed references make every cell count its own combination.

What is the difference between COUNTIF and COUNTIFS?

COUNTIF accepts one range and one condition, COUNTIFS accepts several range/condition pairs and counts only rows that meet all of them. A two-way cross-tab needs two conditions, so it needs COUNTIFS.

Why does my cross-tab total not match the number of rows?

Some rows match none of the labels in the table, usually because of blank answers, misspellings or extra spaces. Compare =COUNTA of the ID column with the grand total and filter the data for the difference.

What is a contingency table?

A contingency table, or cross-tab, shows the counts of two categorical variables at the same time: one variable in the rows, one in the columns, and the number of cases with each combination in the cells. It is the input for the chi-square test of independence.

Can I put averages instead of counts in a cross-tab?

Yes. Use AVERAGEIFS with the value column first and the same two conditions, for example =AVERAGEIFS($D$12:$D$51,$B$12:$B$51,$A2,$C$12:$C$51,B$1). Show the count next to each mean, because some cells may contain only one or two cases.

When can I use the chi-square test on a cross-tab?

When the observations are independent and the expected counts are large enough; a common rule is that all expected counts should be at least 5. If they are not, combine categories or use Fisher's exact test.

Keep going: Cross-Tabs & Chi-Square: Analysing Categorical Data in Excel

This free lab is lesson 1 of 20. Unlock the other 19 hands-on labs, the final exam and your certificate for a one-time $9.90.

  1. Row %, Column % and Total %: Asking the Right Question
  2. Expected Counts: What Independence Would Look Like
  3. Chi-Square Test of Independence: by Hand and with CHISQ.TEST
  4. Cramér's V: How Strong Is the Association?
  5. 2 × 2 Tables: Risk Difference, Relative Risk and Odds Ratio with a CI
  6. Small Samples: Expected Counts Below 5, Combining Categories & Fisher's Exact Test
  7. McNemar's Test for Paired Yes/No Answers + a 100% Stacked Chart
  8. Goodness of Fit: Do Weekday Rentals Match the Fleet Plan?
  9. Which Cells Drive the Result? Adjusted Residuals & a Bonferroni Cut-Off
  10. 2 × 2 Tables: the Two-Proportion z-Test, z² = χ² and Yates' Correction
  11. Confidence Intervals for the Risk Difference and Relative Risk + Number Needed to Treat
  12. The Likelihood-Ratio G-Test: an Alternative to Pearson's Chi-Square
  13. Simpson's Paradox: When the Pooled Table Points the Wrong Way
  14. Mantel-Haenszel: One Adjusted Odds Ratio Across Strata + the CMH Test
  15. Ordered Categories: a Chi-Square Test for Trend Across Age Groups
  16. Comparing Many Proportions: the 2 × k Test and Marascuilo Pairwise Ranges
  17. Beyond Cramér's V: Phi, the Contingency Coefficient and Goodman-Kruskal Lambda
  18. Square Tables: Cohen's Kappa for Agreement and Bowker's Test for Change
  19. Final Project: Categorical Dashboard for a Climbing Gym Survey

Buy course — $9.90 See the full course