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 on the selected column and choose Group By.
How to Count Rows in Power BI
  1. In the Group By window, keep the Basic option selected. Set Group by to Product. In the New column name, enter Distinct Count. In the Operation dropdown, select Count Distinct Rows. Click OK.
Group Rows and Get Counts Using Power Query in Power BI
  1. Power Query will now create a new column that shows the distinct count for each product.
Add a Distinct Count Rows Column in Power BI Power Query

You can add a distinct count rows column in Power BI Power Query.

Count a Specific Value in Power BI Power Query

For this example, we are using the table shown below. The table contains Product Key, Date, and Sold columns.

Add a row to count rows in power query

Now, the requirement is to count how many times a specific value (YES) appears in the Sold column for each Product Key.

To achieve this in Power Query, follow the steps below:

  1. In Power BI Desktop, go to the Home tab and click Transform data to open the Power Query Editor.
  2. In Power Query Editor, select the Sold column.
Add column in PowerBI powerQuery
  1. Click the filter icon and select only the value YES.
Count Rows Of Data With Power Query
  1. Now select the Product Key column. Right-click on the column and choose Group By.
Count Rows Using Power Query or Get & Transform
  1. In the Group By window, keep the Basic option selected. Set Group by to Product Key. In the New column name, enter YES Count. From the Operation dropdown, select Count Rows, and then click OK.
How to Count Rows in Power Bi Table
  1. After clicking OK, Power Query will create a new table that shows each Product Key and the total count of YES values.
Count a Specific Value in Power BI Power Query

In this article, I explained how to use Power Query in Power BI to count rows, count distinct values, and count a specific value like YES using the Group By feature. I showed how to group data by a column, add a count rows column, create a distinct count column, and filter data to count only specific values.

You may also like:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App