Forum Discussion

paul_cranberry's avatar
paul_cranberry
Frequent Visitor
2 years ago
Solved

Splicing together text string "pairs" from adjacent columns

I have data that looks like this...

NameColorsFruit
BobYellow; Red; Green; BlueBanana; Apple; Grape; Blueberry
KarenGreen; Blue; BrownGrape; Blueberry; Kiwi

 

How can I get a new column that concatenates text strings from "Colors" with the matching text string in "Fruit"? For example...

NameMatched Pairs
BobYellow - Banana; Red - Apple; Green - Grape; Blue - Blueberry
KarenGreen - Grape; Blue - Blueberry; Brown - Kiwi

 

Note: The number of text strings in "Colors" is always equal to the number of text strings in its adjacent "Fruit" cell.

 

What I've tried:

I tried adding an index column and then splitting "Colors" and "Fruit" by delimiter (to rows), but I get a huge number of new rows and duplicate information that I don't know how to work my way out of...

IndexNameColorsFruit
0BobYellowBanana
0BobYellow

Apple

0BobYellowGrape
0BobYellowBlueberry
0BobRedBanana
0BobRedApple
0BobRedGrape
0BobRedBlueberry
etc.   
  • HI paul_cranberry 

    Here's one way of handling it.

    Requires writing a little M code rather than pure interface.

    Is this the sort of thing you're looking for?

    let
      Source = #table(
        type table [Name = text, Colors = text, Fruit = text],
        {
          {"Bob", "Yellow; Red; Green; Blue", "Banana; Apple; Grape; Blueberry"},
          {"Karen", "Green; Blue; Brown", "Grape; Blueberry; Kiwi"}
        }
      ),
      #"Split Text into Lists" = Table.TransformColumns(
        Source,
        {
          {"Colors", Splitter.SplitTextByDelimiter("; "), type {text}},
          {"Fruit", Splitter.SplitTextByDelimiter("; "), type {text}}
        }
      ),
      #"Zip Lists" = Table.CombineColumns(
        #"Split Text into Lists",
        {"Colors", "Fruit"},
        each List.Zip({_{0}, _{1}}),
        "Matched Pairs"
      ),
      #"Combine Each Pair Hyphen" = Table.TransformColumns(
        #"Zip Lists",
        {{"Matched Pairs", each List.Transform(_, Combiner.CombineTextByDelimiter(" - "))}}
      ),
      #"Combine Pairs Semicolon" = Table.TransformColumns(
        #"Combine Each Pair Hyphen",
        {{"Matched Pairs", Combiner.CombineTextByDelimiter("; ")}}
      )
    in
      #"Combine Pairs Semicolon"

     

1 Reply

  • HI paul_cranberry 

    Here's one way of handling it.

    Requires writing a little M code rather than pure interface.

    Is this the sort of thing you're looking for?

    let
      Source = #table(
        type table [Name = text, Colors = text, Fruit = text],
        {
          {"Bob", "Yellow; Red; Green; Blue", "Banana; Apple; Grape; Blueberry"},
          {"Karen", "Green; Blue; Brown", "Grape; Blueberry; Kiwi"}
        }
      ),
      #"Split Text into Lists" = Table.TransformColumns(
        Source,
        {
          {"Colors", Splitter.SplitTextByDelimiter("; "), type {text}},
          {"Fruit", Splitter.SplitTextByDelimiter("; "), type {text}}
        }
      ),
      #"Zip Lists" = Table.CombineColumns(
        #"Split Text into Lists",
        {"Colors", "Fruit"},
        each List.Zip({_{0}, _{1}}),
        "Matched Pairs"
      ),
      #"Combine Each Pair Hyphen" = Table.TransformColumns(
        #"Zip Lists",
        {{"Matched Pairs", each List.Transform(_, Combiner.CombineTextByDelimiter(" - "))}}
      ),
      #"Combine Pairs Semicolon" = Table.TransformColumns(
        #"Combine Each Pair Hyphen",
        {{"Matched Pairs", Combiner.CombineTextByDelimiter("; ")}}
      )
    in
      #"Combine Pairs Semicolon"