Search code examples
excelif-statementexcel-formulagroupingworksheet-function

Grouping a Table based on Text


I've used subtotals, group by, pivot tables, etc in Excel to sum columns based on an ID. I get tables in the 100's of rows each week with an ID column with duplicate values like:

table:  
ID  Location
a   Boston
a   NY
c   SF
d   LA
c   Seattle

I want to be able to group them without losing data:

output: 
ID  Location
a   Boston, NY
c   SF, Seattle
d   LA

How would I do this?

Data Sample #2 (using @pnuts formulas) - the problem here is that after deleting the TRUE rows, banner1 does not show the 1. It should be 1,2,5 in the formula1 column in the banner1 FALSE row:

id             cell formula1    formula2
banner1        1    1,          TRUE
banner1        2    2,          TRUE
banner1        5    2, 5        FALSE
banner2        3    3,          TRUE
banner2        6    3, 6        FALSE
banner4        4    4,          TRUE
banner4        7    4, 7        FALSE

Solution

  • For an infrequent requirement and assuming ID is in A1, this can be achieved by sorting on ColumnA then in C2 entering:

    =IF(A2=A3,B2&",",IF(A1=A2,C1&" "&B2,B2))  
    

    and in D2:

    =RIGHT(C2)=","  
    

    copying down to suit, copying the results and Paste Special..., Values over the top before filtering on ColumnD to select and delete "TRUE" rows and then deleting Column D.

    For frequent use something like VBA may be more practical (possibly created with turning on Record Macro before applying the above process).