Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Merging columns but ignore repeating items across a row

Hi,

this is my initial table:

 

Symbols
(*)
(^)
(!)
(~)

 

I want to create a list of all the combinations of these 4 symbols.

The order of the symbols doesn't matter, its just the combination that matters.

eg, (*)(!) = (!)(*)

also, I want to exclude repeating, eg:

(*)(*)(*)(!) should just be (*)(!)

 

So here's my code:

 

 

let
    Source = Excel.CurrentWorkbook(){[Name="Tbl_symbol"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Symbols", type text}, {"Definition", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each 1),
    #"Merged Queries" = Table.NestedJoin(#"Added Custom",{"Custom"},#"Added Custom",{"Custom"},"Added Custom",JoinKind.LeftOuter),
    #"Expanded Added Custom" = Table.ExpandTableColumn(#"Merged Queries", "Added Custom", {"Symbols"}, {"Symbols.1"}),
    #"Merged Queries1" = Table.NestedJoin(#"Expanded Added Custom",{"Custom"},#"Expanded Added Custom",{"Custom"},"Expanded Added Custom",JoinKind.LeftOuter),
    #"Expanded Expanded Added Custom" = Table.ExpandTableColumn(#"Merged Queries1", "Expanded Added Custom", {"Symbols", "Symbols.1"}, {"Symbols.2", "Symbols.1.1"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Expanded Added Custom",{"Definition", "Index", "Custom"})
in
    #"Removed Columns"

 

 

 

And this is what I get:

I'd like to merge the columns now, but I'm not sure how to ONLY include the column if it is distinct from the existing items. Also, this may not manage the whole (*)(!) being the same as (!)(*). One step at a time, perhaps! My initial merge queries could be optimised for a better outcome?

Thanks so much in advance for your help.

  • It sounds like you want the set of all possible subsets of these four symbols.

     

    If that's correct, you may be interested in my comment on smpa01's combinatorics blog post. In particular, you can generate the list of combinations as follows:

    let
      Source = {"(*)", "(^)", "(!)", "(~)"},
      N = List.Count(Source),
      Subsets =
          List.Transform(
              {0..Number.Power(2, N)-1},
              (i) => List.Transform(
                         {0..N-1},
                         (j) => if Number.Mod(Number.IntegerDivide(i, Number.Power(2, j)), 2) = 1
                                then Source{j}
                                else null
                     )
          ),
      Concatenate = List.Transform(Subsets, each Text.Combine(List.RemoveNulls(_), ""))
    in
      Concatenate

     

    Output:

4 Replies

  • It sounds like you want the set of all possible subsets of these four symbols.

     

    If that's correct, you may be interested in my comment on smpa01's combinatorics blog post. In particular, you can generate the list of combinations as follows:

    let
      Source = {"(*)", "(^)", "(!)", "(~)"},
      N = List.Count(Source),
      Subsets =
          List.Transform(
              {0..Number.Power(2, N)-1},
              (i) => List.Transform(
                         {0..N-1},
                         (j) => if Number.Mod(Number.IntegerDivide(i, Number.Power(2, j)), 2) = 1
                                then Source{j}
                                else null
                     )
          ),
      Concatenate = List.Transform(Subsets, each Text.Combine(List.RemoveNulls(_), ""))
    in
      Concatenate

     

    Output:

    • Anonymous's avatar
      Anonymous
      Not applicable

      Excellent! Thank you so very much.

  • Hmmmm ... perhaps if you showed/provided us the Source and the desired outcome, we may be able to assist you quicker?