Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Sheets/Excel Formula Countifs Distinct Lead IDs by Name

I have a dataset which has various names which had tied associated Lead IDs. In this dataset there are records the name will be matched with the same Lead ID multiple times for example:

Dataset Example

Does anyone have suggestions how a single excel formula could return results such as this for Google Sheets/Excel?

Dataset Result Expectation

like image 746
OwlKnowledge Avatar asked Feb 04 '26 04:02

OwlKnowledge


2 Answers

Within sheets you may try:

=map(unique(tocol(A2:A,1)),lambda(Σ,{Σ,countuniqueifs(B:B,A:A,Σ)}))

enter image description here

like image 193
rockinfreakshow Avatar answered Feb 05 '26 21:02

rockinfreakshow


Not using LAMBDA Helper functions and using COUNTIFS:

=LET(A,A2:A10, B,B2:B10, uxA,UNIQUE(A), uxB,UNIQUE(B),
 VSTACK({"Name","Total Distinct Lead ID"}, 
  HSTACK(uxA,MMULT(N(COUNTIFS(A,uxA,B,TOROW(uxB))>0),SEQUENCE(ROWS(uxB),,,0)))))

Here is the output: output

COUNTIFS counts per unique values (uxA) of name A the unique values (uxB) of name B, i.e. on cell i,j returns the total counts for Lead ID of uxB at column j for uxA at row i. Here the output of COUNTIFS for the input data:

2   1   0   0   0   0
0   0   1   0   0   0
0   0   0   2   2   1 

Since we are interested only in distinct values, we use N() function to replace the count values with 1 where it is greater than 0, otherwise return 0. In order to have the total distinct count we use MMULT to sum the total number of ones on each row. SEQUENCE builds the row array of 1's to do the multiplication. Finally, we use VSTACK to generate the header and HSTACK to put the output in the desired format.

If you prefer to use a Lambda Helper function instead of MMULT, you can try the following:

=LET(A,A2:A10, B,B2:B10, uxA,UNIQUE(A), uxB,UNIQUE(B),
 VSTACK({"Name","Total Distinct Lead ID"},
  HSTACK(uxA,BYROW(COUNTIFS(A,uxA,B,TOROW(uxB)),LAMBDA(x,SUM(N(x>0)))))))

or just the following taking a different approach:

=LET(A,A2:A10, B,B2:B10, uxA,UNIQUE(A),
 VSTACK({"Name","Total Distinct Lead ID"},
   HSTACK(uxA,BYROW(uxA,LAMBDA(x,ROWS(UNIQUE(FILTER(B,A=x))))))))
like image 43
David Leal Avatar answered Feb 05 '26 19:02

David Leal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!