In this tutorial, we will learn to count unique values in a pivot table.
Using a Data Model with a Pivot Table
The data model is yet another thing I love about the newer versions of Microsoft Excel. If you are using Excel for Microsoft 365, Excel 2019, Excel 2016, and Excel 2013, you have the access to Data Model.
- To start with, click on any cell in the data and go to the “Inset” tab in your ribbon.
- Here click on the pivot table and a dialogue box appears.
- Now tick mark the box at the bottom of the dialogue box, “Add this data to the Data Model” and hit OK.
- After this, you will get a usual pivot table and arrange your data in the pivot table fields as you did earlier. This will give you the same pivot table you had earlier, but the pivot table fields look a little different.
- Here’s the trick: click on the small arrow next to “Count of Service provider” in the Pivot Table fields.
- After this click on the “Value Field Settings”.
- Now scroll down to the end to get “Distinct Count” and click OK.
- Here we go: you have a Distinct/Unique Count for each region in the pivot table.
Therefore, we have only 18 unique service providers in the country.
Using the Function COUNTIF
Another approach to calculating the Unique entries is simply using the formula COUNTIF in your Datasheet.
- Let’s start by adding a column to your data with a header of your choice. Here we will call it “Count No.”
- Add this formula (=IF (COUNTIF ($B$2: B2, B2)>1,0,1)) to the cell D2 and drag it till the end.
How this Formula Works??
First of all, we have fixed the start point of the range, also called Absolute i.e $B$2. This means it will not change even if you drag your formula downwards. Now when you drag the formula downwards to D3, this formula becomes IF(COUNTIF($B$2:B3,B3)>1,0,1)
Read it like
Countif ($B$2:B3,B3) will give you the number of times B3 exists between the range $B$2:B3. IF function is used to add a condition: IF ((the number of times B3 Exists in a given range) is greater than 1, then give 0 else return 1)
Now, if the name in the given column exits more than 1 time, the formula will return you 0 else you will get 1. Therefore, for al those repeated names you will get 0 in the column Count no.
- Now create a Pivot Table with your data.
- Here you need to add Location to the ROWS and Count No to the values.
- Boom!! The Pivot Table is ready with unique entries in each Pivot Table.
Use Power Pivot to Count Unique Values
Here comes the most powerful method to identify the unique entries; Power Pivot. Make sure that you got the Power Pivot tab in your ribbon. If you cannot find the tab, check out this tutorial.
- As said earlier, first of all, make sure that the Power Pivot tab is enabled.
- After that go to the Data model and click on the Manage button.
- Here you will get a window opened, which surely will be blank in case this is the first time you are importing the data.
- Click on Home → Get External Data
- Here you will find multiple options and sources available to upload the data. But we need to upload a simple excel. So follow the steps and screenshots and click on “From Other Sources”.
- Now you will again get a dialogue box opened. Scroll to the end to get the option of Excel File and click Next.
- Here you can rename the connection from the default name “Excel”. Click on Browse to choose a path to your data file.
- Also, if you want the top column to be the header row, tick mark the option “Use the first row as column header” and hit Next.
- In the end, the file is imported to the data model and click on finish.
- Here we go: that’s a success with all the 28 rows imported. Now, hit on close.
- Now, this is how it looks like.
- From here we will create a Pivot Table by Home → Pivot Table
- Since we have the data in sheet 1, we will expand the columns by clicking on the small triangle next to it.
- Now, place the Location on the Rows and the Service providers on the values as we did earlier. This will give a simple Pivot Table with the total number of service providers.
- Here’s the trick. Now go to the PowerPivot window and click on Measure to get the option of New measure.
- Now add a description of the desired name and start typing the formula in the formula section.
- As you start typing, you will get the suggestions automatically. Here we need the function of distinct count. Select the distinct count function.
- After this, press the tab button or start a ( bracket and select the column for which we need the distinct count. Like here we need the distinct count of Service providers. Hence, our formula will look like this =DISTINCTCOUNT(Sheet1[Service Provider]))
- In the end, select the category. Since we are finding out the unique count of the service providers, we will select the category as “Numbers”.
- Change the Format to “Whole number” and hit OK. Another column will be added to the Pivot Table which will give you the unique entries.
More on Pivot Table
Sort a Pivot Table | Refresh a Pivot Table | Pivot Table Keyboard Shortcuts | Pivot Table Formatting |Move a Pivot Table | Filter a Pivot Table | Change Pivot Table Data Source| Add or Remove Grand Total in a Pivot Table | Add Ranks in Pivot Table | Insert Calculation in Pivot Table | Refresh All Pivot Tables | Automatically Update a Pivot Table | Running Total in a Pivot Table | Conditional Formatting to a Pivot Table | Pivot Table from Multiple Worksheets | Group Dates in a Pivot Table | Connect a Slicer with Multiple Pivot Tables | Pivot Table Timeline