Forum

Notifications
Clear all

Split and Pad

3 Posts
2 Users
0 Reactions
76 Views
(@pietc)
Posts: 88
Estimable Member
Topic starter
 

Hi,

I have a data column of customer references that comprise a (constant) letter followed by one or more digits (up to five) and I'd like to pad the digits to give the same length.

Eg A1 becomes A00001 and A44 becomes A00044.

I've done this by multiple operations and just wondered if it's possible to do it all in one - and would this be more efficient?  Should I create a function or is there a simpler way?

My current M-code is:

let
Source = Excel.CurrentWorkbook(){[Name="tblSample"]}[Content],
#"Split Column by Positions" = Table.SplitColumn(Source, "Column1", Splitter.SplitTextByPositions({0, 1}), {"First", "Rest"}),
#"Added Custom Column" = Table.AddColumn(#"Split Column by Positions", "Pad", each Text.PadStart([Rest], 5, "0"), type text),
#"Merged Columns" = Table.CombineColumns(#"Added Custom Column",{"First", "Pad"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"SplitAndPadded"),
#"Removed Columns" = Table.RemoveColumns(#"Merged Columns",{"Rest"})
in
#"Removed Columns"

Thanks in advance.

Pieter

 
Posted : 03/06/2021 6:40 am
(@mynda)
Posts: 4761
Member Admin
 

Hi Pieter,

It won't make any difference if you create this as a custom function, the steps will be the same therefore there won't be any efficiency gains. I'd be inclined to leave it as is so you can easily see the steps being applied, rather than rolling it into a function.

Mynda

 
Posted : 03/06/2021 7:30 pm
(@pietc)
Posts: 88
Estimable Member
Topic starter
 

Hy Mynda,

Great advice - many thanks.Smile

Pieter

 
Posted : 04/06/2021 7:07 am
Share: