• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Microsoft Excel Tips and Tricks - Computergaga

  • Home
  • Excel Online Courses
  • Excel Functions List
  • Free Downloads
  • Blog
  • Tutorials
  • About
Home / PivotTables / Count Unique Values in a PivotTable

Count Unique Values in a PivotTable

When using a PivotTable to summarise a large list of data, you may need to count unique values in a PivotTable for a particular field.

For example, you may have a list of data like the one below and want to count the number of different customers in each country, or the number of different products that were sold in each country.

Data set to count unique values in a PivotTable

If you added the Customer Name field to the Values area of a PivotTable and summarised it with a Count you would get the result below.

Normal count calculation in a PivotTable

However, this is counting all of the transactions in each country. We need to count the unique customers making those transactions in each country.

From Excel 2013, there is a Distinct Count option in PivotTables making counting unique values a piece of cake. In earlier versions there is a workaround.

Distinct Count in PivotTables

To count unique values in a PivotTable in Excel 2013 or later, we can use the Distinct Count function. To make this function available, you need to ensure that you add the table to the data model when inserting the PivotTable.

  1. Click the PivotTable button on the Insert tab as normal. Ensure the correct table/range is selected and that the Add this data to the Data Model box is checked.
Add data to the data model when inserting a PivotTable
  1. Build the PivotTable by moving the fields into the required areas. In this example the Country field is moved to the Rows area, and Customer Name to the Values area.
  2. Right click on one of the Customer Name values in the PivotTable and select Value Field Settings. Choose Distinct Count from the bottom of the list of functions.
Apply distinct count in Excel PivotTable
  1. The PivotTable now displays a unique count for each customer in the data source and groups them by country.
Unique count in Excel PivotTable

Watch the Video

Count Unique Values in a PivotTable Before Excel 2013

Because the distinct count option is not available in Excel 2010 and earlier you need to use a workaround.

Add a new column to the data source table using a formula like the one below.

=IF(COUNTIF($C$2:$C2,C2)>1,0,1)

In this formula, column C contains the customer names. The COUNTIF function is used to count how many times that customer name appears in the list until the current row. If it has not appeared yet a number 1 is assigned, and if it has already appeared (not unique) a 0 is assigned.

The image below shows this formula applied to the data source.

Calculated column added for a PivotTable field

This column can then be summed in the PivotTable to return a unique count for each customer by country.

Count distinct values in PivotTables in older Excel versions

Related tutorials

  • Count the unique entries in a range
  • Ranking fields in a PivotTable
  • Using Timeline Slicers in Excel 2013 PivotTables

Related Posts:

  • Stop PivotTable Columns Resizing feature
    Stop PivotTable Columns Resizing

Reader Interactions

Comments

  1. Chris says

    9 January 2018 at 2:28 am

    Is there a way to select “add this data to the data model” via VBA? I’m using 2016.

    Reply
    • computergaga says

      10 January 2018 at 7:57 am

      It will surely be possible, but is not something I have done.
      I would record the process to generate some code, and then edit the code to work the way you need it.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Excel SWITCH Function – Nested IF Function Alternative
  • Sum Formulas Only in Excel
  • How to Calculate Age in Excel
  • Custom Sort in Excel
  • Case Sensitive Lookup with XLOOKUP in Excel
Advanced Excel Success book

Footer

Contact

[email protected]

LinkedIn Twitter YouTube

Useful Links

  • Home
  • Excel Online Courses
  • Tutorials
  • Sitemap
  • Privacy Policy
  • Jobs by Jooble

Course Topics

  • Blog
  • Charts
  • Courses
  • Dashboards
  • Downloads
  • Excel Tips
  • Formulas
  • Mail Merge
  • Office
  • PivotTables
  • Power BI
  • Power Pivot
  • Power Query
  • VBA

Join Our Mailing List

© Copyright 2009 Computergaga · All Rights Reserved ·