Forum Discussion

OliviaBurglund's avatar
OliviaBurglund
Copper Contributor
Sep 16, 2021

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

  • 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!

     

    • PeterBartholomew1's avatar
      PeterBartholomew1
      Silver Contributor

      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 

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

Resources