Forum Discussion

Eizenman's avatar
Eizenman
Copper Contributor
Nov 20, 2019

Splitting a row with multiple lines of text to different rows

Hello all,

 

I have an excel sheet in which some rows have multiple lines of text and some have only one line.

I want to convert all those rows which have multiple lines to separate rows.

See example below.

 

Original table:

OrderLineDateSupplier
1
2
1
2
1/12/2020
1/25/2020
Supplier1
Supplier2
3311/17/2019Supplier3

 

Converted table:

OrderLineDateSupplier
111/12/2020Supplier1
221/25/2020Supplier2
3311/17/2019Supplier3

 

Any ideas on how to achieve that? preferable without a macro.

 

Thanks,

Liron.

7 Replies

  • JKPieterse's avatar
    JKPieterse
    Silver Contributor

    Eizenman I'm guessing the data is pasted into Excel from elsewhere, perhaps it is easier to try a different way to get that data into Excel? What is the source of this table?

    • Eizenman's avatar
      Eizenman
      Copper Contributor

      I don't have a way to change how I get the data

      • SergeiBaklan's avatar
        SergeiBaklan
        Diamond Contributor

        Eizenman 

        One more variant with Power Query. Not everything from user interface, the script is

        let
            Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
            UnpivotColumns = Table.UnpivotOtherColumns(
                Source,
                {},
                "Attribute", "Value"
            ),
            DateToText = Table.ReplaceValue(
                UnpivotColumns,
                each [Value],
                each
                    if Type.Is(Value.Type([Value]),DateTime.Type)
                    then Date.ToText(DateTime.Date([Value]),"yyyy-MM-dd")
                    else [Value],
                Replacer.ReplaceValue,{"Value"}
            ),
            AllToText = Table.TransformColumnTypes(
                DateToText,
                {{"Value", type text}}
            ),
            TransformColumn = Table.TransformColumns(
                AllToText,
                {
                    {"Value", Splitter.SplitTextByDelimiter("#(lf)")}
                }
            ),
            ExpandToRows = Table.ExpandListColumn(
                TransformColumn,
                "Value"
            ),
            GroupRows = Table.Group(
                ExpandToRows,
                {"Attribute"},
                {{"Group", each [Value]}}
            ),
            Result = Table.FromRows(
                List.Zip(GroupRows[Group]),
                GroupRows[Attribute]
            ),
            ProperType = Table.TransformColumnTypes(
                Result,
                {
                    {"Line", Int64.Type},
                    {"Date", type date},
                    {"Supplier", type text}
                }
            )
        in
            ProperType

         

Resources