Forum Discussion

crossover's avatar
crossover
Advocate I
5 years ago
Solved

Good practice for splitting long text strings

I have a column [text1] with strings of various lenghts anywhere between 10 to 300+ characters. I need to break this text down to 4 columns - knowing that a single column fits up to 70 characters and...
  • Jakinta's avatar
    5 years ago

    Hello,

     

    try this.

    I have added paramater NumOfChar so you can input desired number of characters to split.

     

     

    let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Added Custom" = Table.AddColumn(Source, "Custom", each Text.PositionOfAny([Column1],{" "},Occurrence.All
    )),
    Custom = #"Added Custom"{0}[Custom],
    #"Converted to Table" = Table.FromList(Custom, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Column1", Int64.Type}}),
    #"SPLIT (parameter)" = Table.AddColumn(#"Changed Type", "Custom", each if [Column1] < NumOfCh then 0
    else if ([Column1] >= NumOfCh and [Column1] < NumOfCh*2) then 1
    else if ([Column1] >= NumOfCh*2 and [Column1] < NumOfCh*3) then 2
    else if [Column1] >= NumOfCh*3 then 3 else null),
    #"Grouped Rows" = Table.Group(#"SPLIT (parameter)", {"Custom"}, {{"Gr", each _, type table [Column1=nullable number, Custom=number]}}),
    #"Added Custom2" = Table.AddColumn(#"Grouped Rows", "Custom.1", each Table.AddIndexColumn([Gr], "Index")),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Position", each [Gr][Column1]{0}),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom3",{"Custom", "Gr", "Custom.1"}),
    Positions = List.Combine ({{0}, List.RemoveFirstN(#"Removed Columns"[Position], 1)}),
    FINAL = Table.SplitColumn(Source, "Column1", Splitter.SplitTextByPositions(Positions) )
    in
    FINAL