Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel Power Query: Count Occurrences Step-by-Step

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To count how many times each value appears in a column with Power Query, load your table into Power Query, select the column, and use Transform > Group By with the operation set to Count Rows. Power Query then returns one row per unique value with its occurrence count. You can also keep the original rows and add a count column by grouping in a separate query and merging it back. Nothing extra needs to be installed: Group By is built into Power Query in Excel for Microsoft 365, Excel 2021, and Excel 2019 on Windows and Mac.

What You'll Need

  • Excel with Power Query (Excel 2016 or later, or Microsoft 365). On older perpetual versions the ribbon tab may be called Data rather than Get & Transform; labels can differ slightly by version, device and region.
  • A table or range with a header row, and a column whose values you want to count.
  • Basic familiarity with the Power Query Editor window and the Applied Steps pane on the right.

Step-by-Step

  1. Format the source as a table. Select any cell in your data and press Ctrl+T (Windows) or Command+T (Mac). Confirm the range and tick My table has headers. Named tables make refreshes reliable.
  2. Load the table into Power Query. With a cell selected, go to Data > From Table/Range (on some versions it sits under Get & Transform Data). The Power Query Editor opens in a separate window.
  3. Check the column type. Click the icon at the left of the column header. If the values are text but show as Any, choose Text. Consistent types prevent duplicate-looking values such as numbers stored as text.
  4. Clean the values you are counting. Select the column and use Transform > Trim and Transform > Clean to strip stray spaces and non-printing characters. Optionally use Transform > Format > Lowercase so "Apple" and "apple" count as one value. Skip this if case and spacing matter.
  5. Group the rows. With the column selected, choose Home > Group By (also on the Transform tab). In the dialog, keep the column name in the first dropdown, set New column name to something like Occurrences, set Operation to Count Rows, and click OK. You now have one row per distinct value with its count.
  6. Sort the result if useful. Click the filter arrow on the Occurrences header and choose Sort Descending to see the most frequent values first.
  7. Load the result to a worksheet. Choose Home > Close & Load > Close & Load To..., pick Table, and select either a new worksheet or an existing one. The count table appears as a green Power Query table.
  8. Refresh when the source changes. Right-click the loaded table and choose Refresh, or use Data > Refresh All. The counts recalculate from the current source data.

Alternative: keep every original row and add a count column. Duplicate the query (right-click it in the Queries pane and choose Duplicate), group it by the value column with Count Rows as above, then in the original query use Home > Merge Queries to join the two on the value column, expand the count column, and load. This gives each row its occurrence count without collapsing the list.

Common Mistakes

  • Grouping by the wrong column. If you group by an ID or date instead of the value column, every row comes back with a count of 1. Check the dropdown in the Group By dialog before clicking OK.
  • Counting a column instead of counting rows. The Count Rows operation counts rows in each group. Using Count Distinct Rows or Count on a column with blanks gives different numbers, so pick the operation that matches what you mean.
  • Inconsistent text. Trailing spaces, mixed case, or numbers stored as text split what should be one value into several. Trim and set the type before grouping.
  • Forgetting to refresh. A loaded table does not update itself when the source changes unless you refresh it or the workbook is set to refresh on open.
  • Editing the loaded table by hand. Typing over cells in a Power Query output table is overwritten on the next refresh. Make changes in the source or in the query steps.

Tips

  • Rename the query in the Queries pane so the output table is easy to identify later.
  • Use the Applied Steps pane to review or delete a step; clicking the gear icon next to Grouped Rows reopens the dialog for edits.
  • If you need counts by category (for example, occurrences per region), add a second column to the Group By dialog by clicking + Add grouping.
  • For a quick one-off count without Power Query, COUNTIF works, but Power Query is better when the source is large, changes often, or comes from a file or folder.
  • Keep a copy of the original query before grouping if you may need the full row detail again.

Troubleshooting

  • The Group By button is greyed out. Make sure a column is selected in the data preview and that the query has loaded fully. If the query is still evaluating, wait for it to finish.
  • Counts look wrong or too high. Check for duplicate rows in the source, hidden rows in the original table, or values that differ only by spaces or case. Review the steps before Grouped Rows.
  • An error appears in a cell. Click the cell to read the message. Common causes are mixed data types in the column or a value that cannot be converted; set the column type explicitly and re-run the step.
  • The loaded table will not refresh. Confirm the source file or table still exists and has not been moved or renamed. Use Data > Queries & Connections to check the query's source settings.
  • The output table is read-only. Power Query output tables cannot be edited directly. Change the source data or the query steps, then refresh.

Frequently Asked Questions

Can Power Query count occurrences without removing the original rows?

Yes. Group a duplicate query to get counts, then merge that result back into the original query on the value column and expand the count. The original rows stay intact with a count added to each.

Does Group By count blank cells?

Count Rows counts every row in the group, including rows where the grouped column is blank, so blanks appear as their own group. Filter out nulls before grouping if you do not want them counted.

Is the count case-sensitive?

By default, text comparisons in Power Query are not case-sensitive, so "Apple" and "apple" group together. Apply a case-sensitive comparison only if you specifically need separate counts.

Can I count occurrences across multiple columns?

Yes. Add more grouping columns in the Group By dialog, or merge the columns into one key column first if you want to count combinations of values.

Will the counts update automatically?

They update when the query refreshes. Use Data > Refresh All, right-click the table and choose Refresh, or set the workbook to refresh data when it opens.

Does this work on Excel for Mac?

Yes. Power Query is included in current Excel for Mac versions, and the Group By command is in the same place. Menu names can differ slightly from the Windows version.