Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pivot Table - Count unique values - Excel 2010

I have a pivot table in excel 2010 based on a network output. I would like to have a count of unique values per week of users who posted on the network.

I found this topic: Simple Pivot Table to Count Unique Values which would add an extra column to my data. Problem for me is, I need unique values per week, not over all the table.

Example input:

week 1 USER_A message1
week 1 USER_B message2
week 1 USER_A message3
week 2 USER_A message4
week 2 USER_B message5
week 2 USER_C message6

What excel actually does is when I ask for a count, is it gives 3 as a count both for week 1 as for week 2. I need the count for week 1 to be 2 (as there are 2 users) and the count for week 2 to be 3 (as there are 3 users).

Anyone know how this can be done?

like image 504
NillsF Avatar asked Sep 19 '13 14:09

NillsF


People also ask

Can you count only unique values in a pivot table?

In the Value Field Settings dialog, click Summarize Values By tab, and then scroll to click Distinct Count option, see screenshot: 5. And then click OK, you will get the pivot table which count only the unique values.

Does Excel 365 have distinct count in pivot table?

To use Distinct Count option, one should add the data to Data Model. We can insert PivotTables in Excel for the web spreadsheet but to create Power Pivot data models, we will need Excel desktop application. Distinct Count option is available of Excel 365 desktop application, but not in online version.


3 Answers

You can create a new column and add the formula:

=IF(COUNTIFS($A$2:A2,A2,$B$2:B2,B2)>1,0,1)

And pivot on this column:

enter image description here

like image 150
Jerry Avatar answered Oct 23 '22 10:10

Jerry


Wouldn't another option be to concatenate the two columns and then remove duplicates? Using the image in Jerry's post, the steps would be:

  • Insert column D
  • In column D, enter the formula:

=CONCATENATE(A2," ",B2)

This should give you the result "week 1 USER_A" for row 2, "week 1 USER_B" for row 2, and so on.

  • Under the 'Data' tab, select "Highlight Duplicates"
  • Select only column D, then click OK

You should now be able to count only unique instances of columns A and B. That's what I've done in the past; hopefully I wasn't doing it entirely the wrong way! :-)

like image 20
Jay Avatar answered Oct 23 '22 11:10

Jay


Since you did not specify where your data is coming from.

I will assume your data is stored on an SQL database, one can use a partition count or a row_number partition count to achieved those results.

Completing the solution provided by Jerry, the 'Count of users' column can be achieve as follows from SQL

case when row_number() over (partition by Week order by Users) = 1 then 1 else 0 end as [Unique?]

So this actually does a little bit more, first partitions the result set by distinct Week, orders the Users column and assigns a sequential id to every row on the partitions. We basically filter out the number 1 row in every partition.

If data does not come from SQL, disregard.

like image 39
Pedro Rodrigues Avatar answered Oct 23 '22 10:10

Pedro Rodrigues