Forum Discussion
Counting Unique Values Across Multiple Columns
Hello,
I am trying to count unique values across multiple columns. I am exporting student activities from my work database. You can see from my example how the database exports this information. I would like to be able to count how many people participated in each activity. I have attached an example of the data and also my desired outcome. Thank you in advance!
- Device and OS platform - PC/Windows 10
- Excel product name and version number – Microsoft 365 Apps for enterprise
11 Replies
- PeterBartholomew1Silver Contributor
For what it is worth, I have got a 365 solution working with beta release functions, so it is probably a little early to be useful.
The main formula is
= LET( fullList, REDUCE("",ActivityGrid, LAMBDA(acc,val,APPENDλ(val,acc))), SORT(UNIQUE(FILTER(fullList, LEN(fullList)>1))))which runs across each row adding the value to a list. Then it filters out the blanks, removes duplicates and sorts the list to alphabetic order. It does however call a second Lambda function APPENDλ which I defined to be
= LAMBDA(value,list, LET( n, COUNTA(list), k, SEQUENCE(n+1), IF(k<=n,list,value) ) )Just for the hell of it, I turned the first formula into a named Lambda function so the two formulas for the result were
= UNPIVOTλ(ActivityGrid) = COUNTIFS(ActivityGrid, UNPIVOTλ(ActivityGrid))This is all to be standard Excel, just not quite as we have known it over the years!
- Riny_van_EekelenPlatinum Contributor
OliviaBurglund As a variant, perhaps you want to consider PowerQuery. Connect to the table with activities, flatten it with "unpivot" and group (count) by activity. Just a few clicks. No complicated formulae needed. See attached.
- OliviaBurglundCopper ContributorCould you provide step by step instructions for this?
- Riny_van_EekelenPlatinum Contributor
OliviaBurglund If you are totally new to PowerQuery, the link below could be a good start. Chapter 13 deals with unpivoting data. In Chapter 19, you'll learn about grouping and summarising.
It's a bit of a learning curve, but it's worth it. I promise.
- PeterBartholomew1Silver Contributor
Unpivot other columns is tailor-made for the job! The grouping also works effectively.
I will have to think further on the questions of 'Does a complicated formula cease to be complicated merely because one can select it at the click of a button?' I can see arguments both ways!
I actually have Charles Williams's FastExcel add-in, so I could have gone for
= LET( fullList, INDEX(UNPIVOT(data,,1,,,0),,3), COUNTIFS(ActivityGrid, SORT(UNIQUE(fullList))))Despite that, I think I have yet to find the definitive dynamic array solution for unpivotting that does not rely upon add-ins. Playing with indices works but is somewhat inelegant.
- PeterBartholomew1Silver Contributor
A 365 solution will no pretence at being interested in legacy Excel!
= LET( padded, ActivityGrid&REPT(" ", 10-LEN(ActivityGrid)), concatenated, CONCAT(padded), k, SEQUENCE(20,1,1,10), fullList, TRIM(MID(concatenated,k,10)), list, SORT(UNIQUE(FILTER(fullList, LEN(fullList)>0))), IF({1,0}, list, COUNTIFS(ActivityGrid, list))) See the attached version. The formulas will work in older versions too.