All combinations in power query with dynamic column numbers

gargamalebarbosa

Board Regular
Joined
Aug 4, 2022
Messages
118
Office Version
  1. 365
Platform
  1. Windows
I have a table consisting of 4 columns. I want to find all possible combinations of the desired columns using Power Query. For example, the A-B-D column, the A-B column, or the A-B-C-D columns.
I will write the combinations of the desired columns into a list, and Power Query will generate all the combinations based on that list.
For example, there will be a list in column F, and Power Query will create the combination table based on the column names in that list. The values in the resulting table will be unique

Thank you,


1683915891966.png
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try the following:

Main Table - Name = Data
1236980.xlsx
ABCD
1ABCD
2A1B1C1D1
3A2B2C2D2
4A3B3C3D3
5A4B4D4
6A5D5
7D6
Sheet1

Combinations Table - Name = Combinations
1236980.xlsx
F
1Combinations
2A-B-D
3A-B
4A-B-C-D
5C-B-A
Sheet1

Query:
Power Query:
let
    Data = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    Combinations = Excel.CurrentWorkbook(){[Name="Combinations"]}[Content],
    Options = Table.ToColumns(Combinations){0},

    TableList = List.Accumulate(Options,
            {},
            (state, current) => 
                let 
                    Options = Text.Split(current, "-"),
                    Table = Table.SelectColumns(Data, Options),
                    
                    Combine = List.Accumulate(Options, 
                                Table.SelectColumns(Table, Options{0}),
                                (state, current) => 
                                    let
                                        AddTable = if current = Options{0} 
                                                    then state 
                                                    else Table.ExpandTableColumn(Table.AddColumn(state, current, each Table.SelectColumns(Table, current)), current, {current}),
                                        RemoveNulls = Table.SelectRows(AddTable, each Record.Field(_, current) <> null)
                                    in
                                        RemoveNulls

                            ),
                    Result = Table.ToColumns(Table.CombineColumns(Combine, Options, Combiner.CombineTextByDelimiter("-", QuoteStyle.None), Text.Combine(Options, "-"))) 

                in
                    state & Result
            ),
    Result = Table.FromColumns(TableList, Options)
in
    Result
Result:
A-B-DA-BA-B-C-DC-B-A
A1-B1-D1A1-B1A1-B1-C1-D1C1-B1-A1
A1-B1-D2A1-B2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-B1-C3-D3
A5-B1-C3-D4
A5-B1-C3-D5
A5-B1-C3-D6
A5-B2-C1-D1
A5-B2-C1-D2
A5-B2-C1-D3
A5-B2-C1-D4
A5-B2-C1-D5
A5-B2-C1-D6
A5-B2-C2-D1
A5-B2-C2-D2
A5-B2-C2-D3
A5-B2-C2-D4
A5-B2-C2-D5
A5-B2-C2-D6
A5-B2-C3-D1
A5-B2-C3-D2
A5-B2-C3-D3
A5-B2-C3-D4
A5-B2-C3-D5
A5-B2-C3-D6
A5-B3-C1-D1
A5-B3-C1-D2
A5-B3-C1-D3
A5-B3-C1-D4
A5-B3-C1-D5
A5-B3-C1-D6
A5-B3-C2-D1
A5-B3-C2-D2
A5-B3-C2-D3
A5-B3-C2-D4
A5-B3-C2-D5
A5-B3-C2-D6
A5-B3-C3-D1
A5-B3-C3-D2
A5-B3-C3-D3
A5-B3-C3-D4
A5-B3-C3-D5
A5-B3-C3-D6
A5-B4-C1-D1
A5-B4-C1-D2
A5-B4-C1-D3
A5-B4-C1-D4
A5-B4-C1-D5
A5-B4-C1-D6
A5-B4-C2-D1
A5-B4-C2-D2
A5-B4-C2-D3
A5-B4-C2-D4
A5-B4-C2-D5
A5-B4-C2-D6
A5-B4-C3-D1
A5-B4-C3-D2
A5-B4-C3-D3
A5-B4-C3-D4
A5-B4-C3-D5
A5-B4-C3-D6
 
Upvote 0
Solution

Forum statistics

Threads
1,223,948
Messages
6,175,566
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