Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
crossover
Advocate I
Advocate I

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 line breaking should be done nicely with a space (e.g. not to break up words - this is where my M gets annoyingly extensive). The fourth column can contain whatever is left from the broken down string.

 

While I can eventually get it done, the reality is that I have 2 columns that need this and my M syntax got really long and I wanted to check if there's a better alternative to Split column at 70 characters + split again at right-most space and do A LOT of replacing and custom columns.

 

Is there some good practice or M text manipulation functions that are well suited for this task? A minor example below of the expected outcome by splitting up to 20 characters per column. Any relevant blog posts or M functions that might be useful in this case? No good hits so far when googling around aside of basic split-by-delimiter stuff. Feels like someone must have surely tackled this issue before...

 

split.png

 

 

1 ACCEPTED SOLUTION
Jakinta
Solution Sage
Solution Sage

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

 

 

Jakinta_0-1619312155889.png

 

 

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

here you can find all the functions available to act on the texts

 

I propose a draft resolution. See if it comes close to what you are looking for.

 

 

Anonymous
Not applicable

a version that takes into account, if I understand what you mean, the limit of 70 characters for the first three columns.

Jakinta
Solution Sage
Solution Sage

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

 

 

Jakinta_0-1619312155889.png

 

 

Thanks, I wasn't able to implement this directly, but was able to pick up some methods.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors