Creating All Combinations of Data

griffo

Board Regular
Joined
Apr 19, 2004
Messages
142
Hi there - I'm sure this would have been answered before, but I can't find the right search terms to find my answer.

For the data in the image, I want to create a list of each combination per person of the "classes" where there is a 1 in the cell.

The output would look something like the "Desired Output" image.

Thanks
Aaron
 

Attachments

  • All data combos.png
    All data combos.png
    29 KB · Views: 17
  • Desired Output.png
    Desired Output.png
    7.1 KB · Views: 14

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try the following:

Power Query:
let
    // I used sample binary data. Import the Source with the actual table below
    // Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("tZU9DoMwDIWvUmVmwCYpcIvuiLEbolKr3r9TK7fNj58TBhSEnM+P59hZFne53h+3/USuc+9nf25bYaHoq1zW7sPmaFQyE8QecsGkoiXZPukJ4FSCHTBp5YSCfW6lV+wX+LEdN4afFOcC+PJrzmw+6CrvqQf/HKwtRc50dQUknw32AtWlIR1mbgHJ9yX9dX1LoQaq0P/fvCV/oHlJI8zH/J9UirDkkj8fW1/uFaIq7hTO7m2gn3Nq0cJG+NkL1zQmvuYDe5N+PT8coX99AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Row Labels" = _t, #"Class 1" = _t, #"Class 2" = _t, #"Class 3" = _t, #"Class 4" = _t, #"Class 5" = _t, #"Class 6" = _t, #"Class 7" = _t, #"Class 8" = _t, #"Class 9" = _t, #"Class 10" = _t, #"Class 11" = _t, #"Class 12" = _t, #"Class 13" = _t]),
    ChangeTypes = Table.TransformColumnTypes(Source,{{"Row Labels", type text}, {"Class 1", Int64.Type}, {"Class 2", Int64.Type}, {"Class 3", Int64.Type}, {"Class 4", Int64.Type}, {"Class 5", Int64.Type}, {"Class 6", Int64.Type}, {"Class 7", Int64.Type}, {"Class 8", Int64.Type}, {"Class 9", Int64.Type}, {"Class 10", Int64.Type}, {"Class 11", type text}, {"Class 12", type text}, {"Class 13", Int64.Type}}),

    UnpivotColumns = Table.UnpivotOtherColumns(ChangeTypes, {"Row Labels"}, "Attribute", "Value"),
    FilterRows = Table.SelectRows(UnpivotColumns, each ([Value] = 1)),
    RemoveColumn = Table.RemoveColumns(FilterRows,{"Value"}),
    GroupRows = Table.Group(RemoveColumn, {"Row Labels"}, {{"Classes", each _, type table [Row Labels=text, Attribute=text]}}),
    IndexClasses = Table.TransformColumns(GroupRows, 
                    {"Classes", (r)  => Table.AddIndexColumn(r, "Index", 1)}
        ),
    GroupClasses = Table.TransformColumns(IndexClasses, 
                    {"Classes", (r)  => Table.AddColumn(r, "Classes", each Table.RemoveRows(r, 0, [Index]))}
        ),
    ExpandClasses = Table.ExpandTableColumn(GroupClasses, "Classes", {"Attribute", "Classes"}, {"Classes.Attribute", "Classes.Classes"}),
    ExpandSubClasses = Table.ExpandTableColumn(ExpandClasses, "Classes.Classes", {"Attribute"}, {"Classes.Classes.Attribute"}),
    RemoveNulls = Table.SelectRows(ExpandSubClasses, each ([Classes.Classes.Attribute] <> null)),
    CombineClasses = Table.AddColumn(RemoveNulls, "Custom", each [Classes.Attribute] & " & " & [Classes.Classes.Attribute]),
    Result = Table.RemoveColumns(CombineClasses,{"Classes.Attribute", "Classes.Classes.Attribute"})
in
    Result
 
Upvote 0
Here's a way using a recursive function call:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Unpivot = Table.UnpivotOtherColumns(Source, {"Name"}, "Class", "Value"),
    Filter = Table.SelectRows(Unpivot, each ([Value] = 1)),
    RemoveCol = Table.RemoveColumns(Filter,{"Value"}),
    fnPairwise = (lst as list) => let lc = List.Count(lst), ls = List.Skip(lst) in 
                            if lc > 2 then List.Accumulate(ls, {}, (s,c)=> s & {lst{0}  & " & " & c}) & @fnPairwise(ls)
                            else if lc = 2 then {lst{0} & " & " & lst{1}} 
                            else {lst{0}},
    Group = Table.Group(RemoveCol, {"Name"}, {{"Combination", each fnPairwise(_[Class])}}),
    Result = Table.ExpandListColumn(Group, "Combination")
in
    Result

Book1
ABCDEFGHIJKL
1NameClass1Class2Class3Class4Class5Class6Class7NameCombination
2Person11111Person1Class1 & Class3
3Person21Person1Class1 & Class4
4Person311Person1Class1 & Class6
5Person4111Person1Class3 & Class4
6Person511111Person1Class3 & Class6
7Person6111Person1Class4 & Class6
8Person71111Person2Class3
9Person8111Person3Class1 & Class5
10Person4Class1 & Class5
11Person4Class1 & Class6
12Person4Class5 & Class6
13Person5Class1 & Class2
14Person5Class1 & Class3
15Person5Class1 & Class4
16Person5Class1 & Class5
17Person5Class2 & Class3
18Person5Class2 & Class4
19Person5Class2 & Class5
20Person5Class3 & Class4
21Person5Class3 & Class5
22Person5Class4 & Class5
23Person6Class2 & Class3
24Person6Class2 & Class4
25Person6Class3 & Class4
26Person7Class1 & Class3
27Person7Class1 & Class4
28Person7Class1 & Class7
29Person7Class3 & Class4
30Person7Class3 & Class7
31Person7Class4 & Class7
32Person8Class1 & Class2
33Person8Class1 & Class5
34Person8Class2 & Class5
35
Sheet1
 
Upvote 0
Solution
Hi everyone

Thank you - both solutions have worked exactly as expected.

Really appreciate your help, this is for a small sporting association (NFP) and will make the lives of our volunteers significantly easier.
 
Upvote 0

Forum statistics

Threads
1,223,945
Messages
6,175,555
Members
452,652
Latest member
eduedu

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