Software

Office Q&A: Conditionally count unique lists in Excel

This month, Susan Harkins helps readers conditionally count unique lists in Excel with two user-friendly solutions: a simple matrix and a PivotTable.

Office Q&A

Usually, I divide this monthly question and answer column between two or three questions. This month, I'm devoting the entire column to one reader's question: Ashish wants to conditionally count unique values.

You can use a complex array formula, but they're hard to manage if you don't know what you're doing. If you prefer an array, use your favorite search engine to look for "conditional count of unique values in Excel." You'll find plenty of discussions on the topic. However, I'd like to offer two user-friendly solutions: a simple matrix and a PivotTable.

I'm using Excel 2013 on Windows 7. For your convenience, you can download the .xlsx file. There's no .xls file, because neither solution is supported by Excel 2003 or earlier.

The data

Figure A shows a simple data set. As you can see, each record contains a name, a code, and a group. Ashish needs to extract the number of customer codes billed by each executive. An executive can repeat the same code, but the code number should be counted only once. In other words, he wants the number of unique customer codes for each executive.

Figure A

Figure A

We'll count unique customer codes for each executive.

The matrix

This first solution counts the unique customer codes by executive by evaluating the executives against a unique list of customer codes in the form of a matrix. The first thing we need is a list of unique customer codes. If you have a stable list of unique values, use it. If not, you can extract a unique list of values from the natural data by using the Advanced Filter feature as follows:

  1. Click the Data tab.
  2. Click Advanced in the Sort & Filter group.
  3. In the resulting dialog, click the Copy to another location option.
  4. Click inside the List range control and then select B1:B14 in the sheet.
  5. Click inside the Copy to control, and then select any cell in the worksheet to anchor the unique list's range. I selected E2.
  6. Check the Unique records only option (Figure B).
    Figure B
    Figure B
  7. Click OK to generate the unique list of customer code values (Figure C). It won't matter that the codes aren't in any meaningful order.
    Figure C
    Figure C

After generating a custom list of customer codes, enter each executive (matching the names exactly) in F2:I2. Next, use the COUNTIFS() function to return the number of customer code values for each executive. To do so, enter the following formula in cell F3 and copy it to the rest or the matrix, as shown in Figure D:

=COUNTIFS($A$2:$A$14,F$2,$B$2:$B$14,$E3)

Figure D

Figure D

This matrix counts customer codes by executive values.

This function evaluates the unique values in column E and row 2 against the data in columns A and B.

We still need a unique count though. To complete the solution, add a totaling row to the bottom of the matrix that counts the unique customer values. In this case, that means excluding the 0 values. Specifically, enter the following function in F8 and copy it to I8, as shown in Figure E:

=COUNTIF(F$3:F$7,"<>"&0)

Figure E

Figure E

Exclude 0s from the count.

Ashish has five records but only four unique customer codes. The others all have the same number of unique codes as they have records.

This solution has its limitations. Adding new records won't update the matrix. If you're using Excel 2007 or later, convert your data set into a Table object by clicking inside the data set, clicking the Data tab, and then clicking Table in the Tables group. Confirm whether the data set has headers, and click OK. As you can see in Figure F, the matrix automatically updates when referencing a Table data source.

Figure F

Figure F

The matrix updates if you use a Table to control data entry.

A second problem isn't as easy to solve. If you add a new customer code value to the data set (Table), you must also add it to the matrix. The count shown in Figure F changed, because the code in the new record is unique for Amit and the code is already in column E of the matrix.

The PivotTable

If you're using Excel 2013, you can use the PivotTable feature's new Distinct Count option as follows:

  1. Click inside the data set, and then click Insert.
  2. Click PivotTable in Tables group.
  3. In the resulting dialog, be sure to check the Add this data to the Data Model option (Figure G), and click OK. Table2 in the Table/Range control is a reference to the data range. Remember, we converted the data to a Table object.
    Figure G
    Figure G
  4. Drag the Executive Name field to the Rows section.
  5. Drag the Customer Code field to the Values section.
  6. Click the Sum of Customer Code drop-down in the Values section and choose Value Field Settings (Figure H).
    Figure H
    Figure H
  7. Choose Discount Count from the Summarize value field by options (Figure I).
    Figure I
    Figure I
  8. Click OK to see the results (Figure J).
    Figure J
    Figure J

To hide the grand total row, click the contextual Design tab, and then choose Off for Rows and Columns from the Grand Totals drop-down in the Layout group.

A PivotTable is much simpler, but you must remember to refresh it when you update the data source. Earlier versions of Excel support the PivotTable feature, but Excel 2013 is the first to offer the Discount Count option.

Choices

There are probably many ways to count unique values, conditionally, without using an array. I've shown you two that are easy to implement. What advice would you give Ashish? Please share your ideas in the comments below.

Send me your question about Office

I answer readers' questions when I can, but there's no guarantee. When contacting me, be as specific as possible. For example, "Please troubleshoot my workbook and fix what's wrong" probably won't get a response, but "Can you tell me why this formula isn't returning the expected results?" might. Please mention the app and version that you're using. I'm not reimbursed by TechRepublic for my time or expertise, nor do I ask for a fee from readers. You can contact me at susansalesharkins@gmail.com.

Also see

About Susan Harkins

Susan Sales Harkins is an IT consultant, specializing in desktop solutions. Previously, she was editor in chief for The Cobb Group, the world's largest publisher of technical journals.

Editor's Picks

Free Newsletters, In your Inbox