Forum Discussion
DanHuber
Dec 29, 2023Iron Contributor
PQ: How to split a large column into 50 characters columns but split only at whitespace?
I have a large table (>80'000 rows) with a column (max width = 163) that I need to resize into smaller columns. The rules are: - split only at whitespaces - column size max 50 characters - 2 colu...
DanHuber
Dec 29, 2023Iron Contributor
Lorenzo , is there a specific reason why your file is using an external data connection?
Lorenzo
Dec 29, 2023Silver Contributor
is there a specific reason why your file is using an external data connection?
Nope. That's the way it is for a moment with 365. TBH I never tried to figure out the reason
Query code is, assuming data formatted as table named Table, with your data in [Column1]:
let
Source = Excel.CurrentWorkbook(){[Name="Table"]}[Content],
PartOne = Table.AddColumn(Source, "Part1", each
let
range = try Text.Range([Column1], 0, 50)
in
if range[HasError] then [Column1]
else if Text.At(range[Value], 49) = " " then range[Value]
else Text.BeforeDelimiter(range[Value], " ", {0,RelativePosition.FromEnd}) & " ",
type text
),
PartTwo = Table.AddColumn(PartOne, "Part2", each
let
after = Text.AfterDelimiter([Column1], [Part1]),
range = try Text.Range(after, 0, 50)
in
if range[HasError] then after
else if Text.At(range[Value], 49) = " " then range[Value]
else Text.BeforeDelimiter(range[Value], " ", {0,RelativePosition.FromEnd}) & " ",
type text
),
PartThree = Table.AddColumn(PartTwo, "Part3", each
Text.AfterDelimiter([Column1], [Part1] & [Part2])
),
KeptParts = Table.SelectColumns(PartThree, {"Part1", "Part2", "Part3"}),
FixedNullLength = Table.ReplaceValue(KeptParts, each [Part1], null,
(x,y,z) as nullable text => if Text.Length(x) = 0 then null else x,
{"Part2", "Part3"}
)
in
FixedNullLength
NB: Last step (FixedNullLength) isn't necessary if you don't mind having non-null cells ("") in the output