Live Developer Training: next batch coming soon. Be the first to know.Live training: next batch soonJoin the waitlist →Join →

Best Way to Count Rows and Distinct Values in Power BI Power Query

Last week, one of my subscribers asked me a question about Power Query in Power BI. He was working with a Product table that has three columns: Product Key, Date, and Sold. For one Product Key, there are many records with different dates.

The requirement was simple. He wanted to count how many times the value “YES” appears in the Sold column for each Product Key.

To do this, Power Query provides a Group By option that lets us count rows and perform a distinct count with just a few clicks.

In this tutorial, I will show you how to add a column to count rows and how to add a column for distinct count using Power Query. You will see that this can be done in just four clicks by using the Group By feature in Power BI Power Query.

Count Rows and Distinct Values in Power BI Power Query

For this example, I am using the Financials table that comes with Power BI. In the screenshot below, you can see the sample data loaded into Power BI.

power query count rows with specific value

Add a Count Rows Column in Power BI Power Query

In this section, we will use the Group By option in Power Query to add a new column that shows the total number of rows for each group.

To do this, follow the steps below:

  1. In Power BI Desktop, under the Home tab, click on Transform data to open the Power Query Editor.
Power Query Customize column that count number of rows for each ROW
  1. Once the Power Query Editor opens, select the column you want to group by. In this example, select the Product column.
Power Query Row Count Tricks You Should Know
  1. Right-click on the selected column (Product) and select Group By from the menu.
Add Running Count Column in Power BI Power Query
  1. This will open the Group By window. Keep the Basic option selected, choose Product as the Group by column, enter Count as the new column name, select Count Rows as the operation, and then click OK.
How to Countrows of a Dataset in Power Bi power query
  1. After clicking OK, Power Query will create a new table that shows each Product and the total number of rows for that Product.
Add a Count Rows Column in Power BI Power Query

This way, you can add a count rows column in Power BI Power Query.

Add a Distinct Count Rows Column in Power BI Power Query

In some scenarios, you may want to count only unique values instead of counting all rows. For this, Power Query provides the Distinct Count option in the Group By feature.

To add a Distinct Count column, follow the steps below:

  1. In Power Query Editor, select the column you want to group by. For example, select the Country column.
Power Bi Power Query Custom Column to count number
  1. Right-click