Forum Discussion
DrExcel_Excel_MVP
Nov 20, 2023Copper Contributor
Leading and Trailing Zero in Excel
How to Remove Leading and Trailing Zeros in Excel We have a set of product codes like this : 000P2I290002M900 we want to make it like this P2I290002M9 which means removing all leading and trai...
Lorenzo
Nov 20, 2023Silver Contributor
Power Query for the record
// Table
let
Source = Excel.CurrentWorkbook(){[Name="Table"]}[Content],
Fixed = Table.ReplaceValue(Source, each [Product code], "0",
(x,y,z)=> Text.Trim(y,z),
{"Product code"}
)
in
Fixed
SergeiBaklan
Nov 20, 2023Diamond Contributor
(x,y,z)=> Text.Trim(y, z) shall work, isn't it?
- LorenzoNov 20, 2023Silver Contributor
Good point SergeiBaklan + Thanks. Previous post revised
- SergeiBaklanNov 20, 2023Diamond Contributor
I'm sure lambda equivalent was done by someone, here is quickly done variant
TextTrim = LAMBDA(txt, [ChrToTrim], LET( chr, IF(ISOMITTED(ChrToTrim), " ", ChrToTrim), IF( LEFT(txt)=chr, TextTrim( RIGHT( txt, LEN(txt)-1), chr), IF( RIGHT(txt) <> chr, IF( txt = "", "", txt), TextTrim( LEFT( txt, LEN(txt)-1), chr) ) )) )
- djclementsNov 21, 2023Silver Contributor
SergeiBaklan and DrExcel_Excel_MVP here's another recursive lambda method that uses fixed-point combinator logic to define and use the custom function directly in a cell:
=LET( F, LAMBDA(X,s,c, LET( a, IF(LEFT(s)=c, RIGHT(s, LEN(s)-1), s), b, IF(RIGHT(a)=c, LEFT(a, LEN(a)-1), a), IF(OR(LEFT(b)=c, RIGHT(b)=c), X(X, b, c), b))), F(F, B6:B14, "0"))
...where s is the string, cell or range of values to be cleaned, and c is the character to be removed (leading/trailing).
Example: In-Cell Recursive Lambda Function