Power Query Countif: Step-by-Step Guide
In this article
Power Query has no built-in COUNTIF function, but you can achieve the same result by grouping rows and counting them. The shortest working method is to load your table into Power Query, select the column you want to count by, and use Group By with the Count Rows operation. This guide walks through that process, plus how to count with conditions, handle common errors, and avoid the mistakes that make counts wrong.
What You'll Need
- Excel (Microsoft 365, Excel 2021, or Excel 2019) or Power BI Desktop. The steps below use Excel's Power Query Editor; Power BI's editor works the same way with minor label differences.
- A clean data table with headers, loaded as an Excel Table (select your data and press Ctrl+T) or an external data source.
- Basic familiarity with the Power Query Editor window: the ribbon, the Queries pane on the left, and the Applied Steps list on the right.
Menu labels can differ slightly by version, device, or region. If a button name does not match exactly, look for the closest equivalent on the same tab.
Step-by-Step
- Load your data into Power Query. Select any cell in your table, then go to Data > From Table/Range. If prompted, confirm the range and tick My table has headers. The Power Query Editor opens.
- Check the column you want to count. Click the column header for the field you want to group by (for example, Customer or Status). Make sure the data type shown in the header is correct (Text, Whole Number, and so on). If it is wrong, click the type icon and choose the right type.
- Open Group By. On the Home tab, click Group By. In Power BI the button is on the Transform tab under the same name.
- Set the grouping column. In the Group By dialog, choose your column from the first dropdown. If you want to group by more than one column, click Add grouping and add the next column.
- Add the count operation. Click Add aggregation. In the Operation dropdown choose Count Rows. Give the new column a name such as Count. Leave the Column dropdown as is; Count Rows does not need a specific column.
- Click OK. Power Query collapses the table to one row per group, with a count column. Check the row count in the bottom status bar matches your expectation.
- Rename the output column if needed. Double-click the new column header and type a clearer name, such as Orders per Customer.
- Load the result. Click Close & Load on the Home tab to send the table to a new worksheet, or Close & Load To... to choose a location such as an existing worksheet or the Data Model.
Counting with a condition (the COUNTIF equivalent)
Group By counts every row in a group, which is the equivalent of COUNTIF when you first filter the rows you want to count. To count only rows that meet a condition, add a filter step before grouping:
- Click the filter arrow on the column that holds your condition (for example, Status).
- Untick Select All, tick only the value you want to count (for example, Shipped), and click OK. Use Text Filters or Number Filters for ranges or partial matches.
- Now run Group By as described above. The count reflects only the filtered rows.
- If you need several conditions counted side by side, duplicate the query (right-click the query in the Queries pane and choose Duplicate), apply a different filter to each copy, and merge the results later.
Counting distinct values
If you want to know how many unique items appear in a column rather than how many rows, use the Count Distinct Rows operation in the Group By dialog instead of Count Rows. This is the Power Query equivalent of a distinct count and is useful for things like counting unique products per order.
Common Mistakes
- Grouping by the wrong column. If your count looks like the total row count repeated, you probably grouped by a column that is unique per row (such as an ID). Group by the category column instead.
- Forgetting the filter. Group By alone counts all rows. If you expected a conditional count, add the filter step before grouping.
- Wrong data type. Numbers stored as text can group separately from identical-looking numbers. Set the column type before grouping.
- Trailing spaces or inconsistent case. Text values like "Shipped " and "Shipped" become separate groups. Use Transform > Trim and Format > lowercase (or uppercase) before grouping to normalise them.
- Loading to the wrong place. Using Close & Load creates a new sheet each time; use Close & Load To... if you want the output in a fixed location.
Tips
- Rename each step in the Applied Steps list so the query stays readable when you come back to it.
- Use Add grouping to count by two or more columns at once, for example Region and Status.
- Keep a copy of the raw query and build filtered duplicates from it; this makes multiple conditional counts easy to maintain.
- If you need the count back on every original row (like a COUNTIF formula dragged down), merge the grouped query back to the original on the grouping column and expand the count column.
- Refresh the query after the source data changes: Data > Refresh All in Excel.
Troubleshooting
- The Group By button is greyed out. Make sure a query is selected in the Queries pane and that the table has at least one column. If the data is still loading, wait for the preview to finish.
- Counts are higher than expected. Check for duplicate rows or extra spaces in the grouping column. Use Remove Duplicates only if duplicates are genuinely unwanted; otherwise trim and normalise text first.
- Counts are lower than expected. A filter step earlier in the query may be removing rows. Review each step in Applied Steps and click the step to preview its output.
- Error values appear. Click the error cell to see the message. Common causes are mixed data types or nulls; set the column type and replace errors if appropriate.
- The loaded table does not update. Right-click the query and choose Refresh, or use Refresh All. If the source range changed size, edit the source step to point at the new range.
- You cannot find Group By. It sits on the Home tab in Excel's Power Query Editor and on the Transform tab in Power BI Desktop. If it is still missing, update your version of Excel or Power BI.
Frequently Asked Questions
Does Power Query have a COUNTIF function?
No. Power Query uses Group By with Count Rows to produce the same result. You filter for the condition first, then group and count.
Can I count based on multiple conditions?
Yes. Apply filters for each condition before grouping, or duplicate the query for each condition and combine the results. Group By also accepts multiple grouping columns.
How do I count unique values instead of rows?
In the Group By dialog, choose Count Distinct Rows as the operation. This counts unique values in the selected column.
Can I put the count back on every row?
Yes. Create the grouped query, then merge it back into the original query on the grouping column and expand the count column. Each original row then shows its group's count.
Why does my count include blank rows?
Blank cells form their own group. Filter out nulls before grouping if you do not want them counted.
Does this work in Power BI as well as Excel?
Yes. The Group By feature and Count Rows operation exist in both. The button location differs slightly, but the dialog and results are the same.