How to search on one column and then count distinct entries in another column (Excel)

Copper Contributor

I am trying to compare sales data from two tables. The first table shows product info (Location, product code etc.), and the second table shows all transaction data (Location, product code, customer info for each transaction).

I want to look at each product+location in the first table, then find all instances of it in the second table, and count the number of distinct customers 

 

Example data:

 

Product Data Table

LocationProduct CodeDescription
Location AProduct 1Car
Location BProduct 1Car
Location AProduct 2Bike
Location BProduct 2Bike

 

Order Data Table

LocationProduct CodeOrder number

Customer

Location AProduct 101234Customer X
Location A Product 101235Customer X
Location A Product 101236Customer Y
Location AProduct 201237Customer X
Location BProduct 101238Customer X

 

In this example,  I want to know the number of distinct customers who ordered Product 1 from Location A, which is 2.

 

I would appreciate any help I can get!

4 Replies

@Rachel1994 

=COUNTA(UNIQUE(FILTER($I$2:$I$6,$G$2:$G$6=A9)))

Maybe with this formula for the layout of the attached file. 

@Rachel1994 

Slightly modified formula suggested by @OliverScheurich if use structured tables

image.png

which is

=LET(
  f,  FILTER(
         Orders[Customer],
        ( Orders[Location] = [@Location] ) *
        ( Orders[Product Code] = [@[Product Code]] ) ),
  COUNTA( UNIQUE( f ) ) )

@Rachel1994 

 

_Screenshot.png

in E3:

=LET(
    filtered, FILTER(OrderData[Customer],
                     (OrderData[Location]=[@Location]) *
                     (OrderData[Product Code]=[@[Product Code]]),
                     0
    ),
    IF(COUNT(filtered), 0, COUNTA(UNIQUE(filtered)))
)
Thanks so much for your help, this worked perfectly!