Identify which members coexist within groups

mr-tom

New Member
Joined
Apr 25, 2007
Messages
18
Hi,

I've got an interesting problem, which has stumped me for a week now.

I have a dataset that lists customer IDs and the product IDs for the products they currently own.

Simple example:
Tom, iphone
Tom, ipad
Tom, Minecraft Lego
Emma, iphone
Emma, ipad
Emma, guillotine
Emma, Ham sandwich

So the returned results would look like:

Iphone. Ipad. Etc
iphone
ipad
Minecraft lego
Guillotine
Ham sandwich

Where products coexist within a customer, just return a 1, but where they don't, return a zero.

I.e:
Ham sandwich and guillotine coexist within a customer, so their intersection would contain a 1
Iphone and ipad coexist within at least one customer, so their intersection would contain a 1
Ham sandwich and minecraft lego never coexist within the same customer, so their intersection would contain a zero.

I appreciate this is one of those things that may work better within SQL, but my client doesn't allow query analyser for their "normal" staff, (or even Access), so to be able to re-run this in the future, it's going to need to happen in excel.

Any ideas appreciated. :)

Tom.

(Edit) The actual data set is around 250k records once deduplicated across the combination of group and product
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
How and where exactly do you want the results to appear? What is the current layout of the data? Try using the ForumTools add-on (link in my sig line) to provide some data in the right format.
 
Last edited:
Upvote 0
Apologies - I'm on the iPad this weekend, so can't use the add in, but it's a fair comment - I'd not realised the forum had trimmed out the excess spaces I'd put in. Below is a link to a spreadsheet that illustrates what I'm trying to do.

Forgive me if the link doesn't work - I'm not 100% sure that the forum will like me posting a link to an XLS so sorry if that doesn't go well.

https://1drv.ms/x/s!AuOYpBUi2pMwkUHF9LR8380v7r_q
 
Upvote 0
The actual data set is around 250k records ...
In that case I think that you are going to need a macro so I hope your client allows that. :)

Assuming your data is in columns B:C, starting at row 3 and your possible products are already listed in column E and row 1 as shown below**, then you could try this macro on a copy of your data.

** If your unique products are not already listed as described, the macro could be altered to do that too.

Rich (BB code):
Sub Coexist()
  Dim d As Object
  Dim a As Variant, b As Variant, itm As Variant
  Dim i As Long, j As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  a = Range("B3", Range("C" & Rows.Count).End(xlUp)).Value
  b = Range("E1").CurrentRegion.Value
  For i = 1 To UBound(a)
    d(a(i, 1)) = d(a(i, 1)) & "|" & a(i, 2) & "|"
  Next i
  For i = 2 To UBound(b, 1)
    For j = 2 To UBound(b, 2)
      If i = j Then
        b(i, j) = "x"
      Else
        b(i, j) = 0
        For Each itm In d.Items
          If InStr(1, itm, "|" & b(i, 1) & "|", 1) > 0 Then
            If InStr(1, itm, "|" & b(1, j) & "|", 1) > 0 Then
              b(i, j) = 1
              Exit For
            End If
          End If
        Next itm
      End If
    Next j
  Next i
  Range("E1").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub

The above code has produced the results shown below in F2:J6


Excel 2010 32 bit
BCDEFGHIJ
1ResultsPRODAPRODBPRODCPRODDPRODE
2PRODAx1110
3123PRODAPRODB1x111
4123PRODAPRODC11x11
5123PRODBPRODD111x1
6234PRODCPRODE0111x
7234PRODB
8234PRODD
9234PRODE
10345PRODA
11456PRODA
12456PRODB
13456PRODC
14456PRODB
15456PRODD
Coexist
 
Upvote 0
Thanks Peter,

I'll try this shortly. It looks perfect and yep, I do have the unique product lists exactly in the way you described! :D

I'll let you know how it goes.
 
Upvote 0
OK. Look forward to hearing how it goes. :)
 
Upvote 0
Works a treat on a small set of data. I'll leave it runnning on the 250k later. May be one for overnight!

Thanks again. :D
 
Upvote 0

Forum statistics

Threads
1,215,490
Messages
6,125,096
Members
449,205
Latest member
ralemanygarcia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top