Counting Unique Values Across Multiple Columns

%3CLINGO-SUB%20id%3D%22lingo-sub-2758096%22%20slang%3D%22en-US%22%3ECounting%20Unique%20Values%20Across%20Multiple%20Columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2758096%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20count%20unique%20values%20across%20multiple%20columns.%20I%20am%20exporting%20student%20activities%20from%20my%20work%20database.%20You%20can%20see%20from%20my%20example%20how%20the%20database%20exports%20this%20information.%20I%20would%20like%20to%20be%20able%20to%20count%20how%20many%20people%20participated%20in%20each%20activity.%20I%20have%20attached%20an%20example%20of%20the%20data%20and%20also%20my%20desired%20outcome.%20Thank%20you%20in%20advance!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3E%3CSTRONG%3EDevice%20and%20OS%20platform%3C%2FSTRONG%3E%20-%20PC%2FWindows%2010%3C%2FLI%3E%3CLI%3E%3CSTRONG%3EExcel%20product%20name%20and%20version%20number%3C%2FSTRONG%3E%20%E2%80%93%20Microsoft%20365%20Apps%20for%20enterprise%3C%2FLI%3E%3C%2FUL%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2758096%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2758173%22%20slang%3D%22en-US%22%3ERe%3A%20Counting%20Unique%20Values%20Across%20Multiple%20Columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2758173%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1158237%22%20target%3D%22_blank%22%3E%40OliviaBurglund%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESee%20the%20attached%20version.%20The%20formulas%20will%20work%20in%20older%20versions%20too.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2758481%22%20slang%3D%22en-US%22%3ERe%3A%20Counting%20Unique%20Values%20Across%20Multiple%20Columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2758481%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1158237%22%20target%3D%22_blank%22%3E%40OliviaBurglund%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EA%20365%20solution%20will%20no%20pretence%20at%20being%20interested%20in%20legacy%20Excel!%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3D%20LET(%0A%20%20padded%2C%20ActivityGrid%26amp%3BREPT(%22%20%22%2C%2010-LEN(ActivityGrid))%2C%0A%20%20concatenated%2C%20CONCAT(padded)%2C%0A%20%20k%2C%20SEQUENCE(20%2C1%2C1%2C10)%2C%0A%20%20fullList%2C%20TRIM(MID(concatenated%2Ck%2C10))%2C%0A%20%20list%2C%20SORT(UNIQUE(FILTER(fullList%2C%20LEN(fullList)%26gt%3B0)))%2C%0A%20%20IF(%7B1%2C0%7D%2C%20list%2C%20COUNTIFS(ActivityGrid%2C%20list)))%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2759341%22%20slang%3D%22en-US%22%3ERe%3A%20Counting%20Unique%20Values%20Across%20Multiple%20Columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2759341%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1158237%22%20target%3D%22_blank%22%3E%40OliviaBurglund%3C%2FA%3E%26nbsp%3BAs%20a%20variant%2C%20perhaps%20you%20want%20to%20consider%20PowerQuery.%20Connect%20to%20the%20table%20with%20activities%2C%20flatten%20it%20with%20%22unpivot%22%20and%20group%20(count)%20by%20activity.%20Just%20a%20few%20clicks.%20No%20complicated%20formulae%20needed.%20See%20attached.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2759485%22%20slang%3D%22en-US%22%3ERe%3A%20Counting%20Unique%20Values%20Across%20Multiple%20Columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2759485%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403176%22%20target%3D%22_blank%22%3E%40Riny_van_Eekelen%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUnpivot%20other%20columns%20is%20tailor-made%20for%20the%20job!%26nbsp%3B%20The%20grouping%20also%20works%20effectively.%3C%2FP%3E%3CP%3EI%20will%20have%20to%20think%20further%20on%20the%20questions%20of%20'Does%20a%20complicated%20formula%20cease%20to%20be%20complicated%20merely%20because%20one%20can%20select%20it%20at%20the%20click%20of%20a%20button%3F'%26nbsp%3B%20I%20can%20see%20arguments%20both%20ways!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20actually%20have%20Charles%20Williams's%20FastExcel%20add-in%2C%20so%20I%20could%20have%20gone%20for%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3D%20LET(%0A%20%20fullList%2C%20INDEX(UNPIVOT(data%2C%2C1%2C%2C%2C0)%2C%2C3)%2C%0A%20%20COUNTIFS(ActivityGrid%2C%20SORT(UNIQUE(fullList))))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EDespite%20that%2C%20I%20think%20I%20have%20yet%20to%20find%20the%20definitive%20dynamic%20array%20solution%20for%20unpivotting%20that%20does%20not%20rely%20upon%20add-ins.%26nbsp%3B%20Playing%20with%20indices%20works%20but%20is%20somewhat%20inelegant.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2759681%22%20slang%3D%22en-US%22%3ERe%3A%20Counting%20Unique%20Values%20Across%20Multiple%20Columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2759681%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1158237%22%20target%3D%22_blank%22%3E%40OliviaBurglund%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20what%20it%20is%20worth%2C%20I%20have%20got%20a%20365%20solution%20working%20with%20beta%20release%20functions%2C%20so%20it%20is%20probably%20a%20little%20early%20to%20be%20useful.%3C%2FP%3E%3CP%3EThe%20main%20formula%20is%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3D%20LET(%0A%20%20fullList%2C%20REDUCE(%22%22%2CActivityGrid%2C%0A%20%20%20%20LAMBDA(acc%2Cval%2CAPPEND%CE%BB(val%2Cacc)))%2C%0A%20%20SORT(UNIQUE(FILTER(fullList%2C%20LEN(fullList)%26gt%3B1))))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Ewhich%20runs%20across%20each%20row%20adding%20the%20value%20to%20a%20list.%26nbsp%3B%20Then%20it%20filters%20out%20the%20blanks%2C%20removes%20duplicates%20and%20sorts%20the%20list%20to%20alphabetic%20order.%26nbsp%3B%20It%20does%20however%20call%20a%20second%20Lambda%20function%26nbsp%3BAPPEND%CE%BB%20which%20I%20defined%20to%20be%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3D%20LAMBDA(value%2Clist%2C%0A%20%20%20%20LET(%0A%20%20%20%20%20%20n%2C%20COUNTA(list)%2C%0A%20%20%20%20%20%20k%2C%20SEQUENCE(n%2B1)%2C%0A%20%20%20%20%20%20IF(k%26lt%3B%3Dn%2Clist%2Cvalue)%0A%20%20%20%20)%0A%20%20)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EJust%20for%20the%20hell%20of%20it%2C%20I%20turned%20the%20first%20formula%20into%20a%20named%20Lambda%20function%20so%20the%20two%20formulas%20for%20the%20result%20were%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3D%20UNPIVOT%CE%BB(ActivityGrid)%0A%0A%3D%20COUNTIFS(ActivityGrid%2C%20UNPIVOT%CE%BB(ActivityGrid))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EThis%20is%20all%20to%20be%20standard%20Excel%2C%20just%20not%20quite%20as%20we%20have%20known%20it%20over%20the%20years!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2760698%22%20slang%3D%22en-US%22%3ERe%3A%20Counting%20Unique%20Values%20Across%20Multiple%20Columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2760698%22%20slang%3D%22en-US%22%3ECould%20you%20provide%20step%20by%20step%20instructions%20for%20this%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2760731%22%20slang%3D%22en-US%22%3ERe%3A%20Counting%20Unique%20Values%20Across%20Multiple%20Columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2760731%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1158237%22%20target%3D%22_blank%22%3E%40OliviaBurglund%3C%2FA%3E%26nbsp%3BIf%20you%20are%20totally%20new%20to%20PowerQuery%2C%20the%20link%20below%20could%20be%20a%20good%20start.%20Chapter%2013%20deals%20with%20unpivoting%20data.%20In%20Chapter%2019%2C%20you'll%20learn%20about%20grouping%20and%20summarising.%3C%2FP%3E%3CP%3EIt's%20a%20bit%20of%20a%20learning%20curve%2C%20but%20it's%20worth%20it.%20I%20promise.%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fexceloffthegrid.com%2Fpower-query-introduction%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fexceloffthegrid.com%2Fpower-query-introduction%2F%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2761244%22%20slang%3D%22en-US%22%3ERe%3A%20Counting%20Unique%20Values%20Across%20Multiple%20Columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2761244%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403176%22%20target%3D%22_blank%22%3E%40Riny_van_Eekelen%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20the%20link%20to%20%22Off%20the%20Grid%22%20material.%26nbsp%3B%20It%20looks%20well%20presented%20with%20good%20material.%26nbsp%3B%20I%20am%20reasonably%20familiar%20with%20the%20PQ%20user%20interface%20and%20can%20modify%20the%20M-code.%26nbsp%3B%20Despite%20that%2C%20I%20have%20the%20feeling%20that%20I%20am%20merely%20paddling%20in%20the%20shallows.%26nbsp%3B%20There%20seem%20to%20be%20whole%20raft%20of%20list%20and%20table%20methods%20available%20through%20the%20use%20of%20the%20Advanced%20Editor%20that%20I%20have%20only%20the%20sketchiest%20idea%20of%20how%20they%20may%20be%20exploited.%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

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

@OliviaBurglund 

See the attached version. The formulas will work in older versions too.

@OliviaBurglund 

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)))

@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.

@Riny_van_Eekelen 

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. 

 

@OliviaBurglund 

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!

 

Could you provide step by step instructions for this?

@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.

https://exceloffthegrid.com/power-query-introduction/ 

@Riny_van_Eekelen 

Thanks for the link to "Off the Grid" material.  It looks well presented with good material.  I am reasonably familiar with the PQ user interface and can modify the M-code.  Despite that, I have the feeling that I am merely paddling in the shallows.  There seem to be whole raft of list and table methods available through the use of the Advanced Editor that I have only the sketchiest idea of how they may be exploited.

@Peter Bartholomew Hi Peter, it's one of many sites I found just over a year ago. And it helped me to get started with PQ. I moved to an iMac over 10 years ago, because I was frustrated by the many performance issues I experienced on PC's. Start-up a Mac and it works. ALL the time.

 

Though, I did miss PQ's introduction in 2013, but now run W10 and Excel on a 9 years old MacBook Pro via Parallels. Just great.

 

Am amazed that PQ isn't more widespread after 8 years. So many out (t)here think that VBA is the only way out of "complex" problems. True, VBA can do much, but in most cases one can do without. And VBA is difficult for most to become really good at, whereas PQ is easy to become reasonably good at without really scratching the surface of M. And I understood that it can do magic once you really know what you are doing. Am not there yet myself, I'm afraid.

 

At the moment, when I face a somewhat complex problem, my initial thought is "can I solve it with PQ?". If not, I try harder to find a PQ  / M solution anyway. If I still can't solve it, only then I will revert to traditional or the more modern Excel functions and perhaps VBA.

 

@Riny_van_Eekelen 

Like you, I to tend to restrict VBA to things that worksheet formulae cannot address, such as turning data into sketches or event handling.  Nobody ever trusted me with corporate data, so I had little need to analyse pre-existing data.  Hence I have tended to follow the development of dynamic arrays more closely.

 

Re: A statement of truth or a wasted opportunity. Lambda helper functions reluctant to return 2D arr...

 

The changes being introduced there are just about as profound as PQ, I can write solutions with little attention to legacy techniques such as relative referencing or the use of grid coordinates.  If I may, I will continue to follow your venture into PQ (maybe M and DAX) and I hope I cause you no offence if I sometimes offer DA alternatives.  At present, only a minority of users are in a position to use array methods and, of those who could, many have been put off by the days of CSE.

@Peter Bartholomew 

By the way, M and modern Excel functionality have many common. For example, if you remember Fibonacci lambda

FIB= LAMBDA(n,
    IF(n<=2, SEQUENCE(n,,0),
        LET(b, FIB(n-1),
            IF( SEQUENCE(n)<n,
            b,
            INDEX(b,n-1)+INDEX(b,n-2))
    ))
);

same FIB in M looks similar

( n ) =>
[ fn=(j) => let b = @fn(j-1) + @fn(j-2)
            in if j < 2 then j else b,
  res = List.Transform({0..(n-1)}, fn )
][res]

 

DAX in particular and tabular modeling in general is another world, at least from my point of view.