Forum Discussion
How to extract 5/6 length ID numbers from a text string?
- 7 years ago
Anonymous
Try this..
I think it will be close.
Please see the attached file as wellTricks I learnt from ImkeF. :smileywink:
But I believe she still would have a better solutionlet Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W0i9LTE4sKS1K1Tc0MDc0NdXNTU1JLS/KTi2yjyk1MDBONjI0NiwCM1ON9YxLS42MTdLS4u2VYnWQNcN1QU0hTzNCmxFMm76hhaGJqbE+adrhtupCtOuSq93YxAhFL8R7SrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Page = _t]), ChangedType = Table.TransformColumnTypes(Source,{{"Page", type text}}), CharactersToReplace = List.RemoveItems(List.Transform({1..126}, each Character.FromNumber(_)),{"0".."9"}), CharactersToReplaceWith=List.Repeat({"|"},List.Count(CharactersToReplace)), MyList=List.Zip({CharactersToReplace,CharactersToReplaceWith}), Convert=Table.AddColumn(ChangedType, "Custom1", each Text.Combine( List.ReplaceMatchingItems (Text.ToList([Page]), MyList) )), Convert2=Table.AddColumn(Convert, "Custom2", each Text.Combine( List.Select( List.RemoveItems( Text.Split([Custom1],"|") , {null,""}) , each Text.Length(_)>=5) , "," )) in Convert2 - 7 years ago
That's a pretty nifty solution Zubair_Muhammad!
Using Text.SplitAny, the code can be shortened considerably:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W0i9LTE4sKS1K1Tc0MDc0NdXNTU1JLS/KTi2yjyk1MDBONjI0NiwCM1ON9YxLS42MTdLS4u2VYnWQNcN1QU0hTzNCmxFMm76hhaGJqbE+adrhtupCtOuSq93YxAhFL8R7SrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Page = _t]),
ChangedType = Table.TransformColumnTypes(Source,{{"Page", type text}}),
CharactersToReplace = Text.Combine(List.RemoveItems(List.Transform({1..126}, each Character.FromNumber(_)),{"0".."9"})),
Convert=Table.AddColumn(ChangedType, "Custom1", each Text.SplitAny(
[Page],
CharactersToReplace)
),
Convert2=Table.AddColumn(Convert, "Custom2", each List.Select(
[Custom1],
(li) => Text.Length(li) >=5){0}
)
in
Convert2
That's a pretty nifty solution Zubair_Muhammad!
Using Text.SplitAny, the code can be shortened considerably:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W0i9LTE4sKS1K1Tc0MDc0NdXNTU1JLS/KTi2yjyk1MDBONjI0NiwCM1ON9YxLS42MTdLS4u2VYnWQNcN1QU0hTzNCmxFMm76hhaGJqbE+adrhtupCtOuSq93YxAhFL8R7SrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Page = _t]),
ChangedType = Table.TransformColumnTypes(Source,{{"Page", type text}}),
CharactersToReplace = Text.Combine(List.RemoveItems(List.Transform({1..126}, each Character.FromNumber(_)),{"0".."9"})),
Convert=Table.AddColumn(ChangedType, "Custom1", each Text.SplitAny(
[Page],
CharactersToReplace)
),
Convert2=Table.AddColumn(Convert, "Custom2", each List.Select(
[Custom1],
(li) => Text.Length(li) >=5){0}
)
in
Convert2
This solution is great.. I am using it for a different application.
I have one column containing 4 and 5 digit numbers that I need to grab and add to a new column. This same column also has 10 digit numbers (in the same column as the 4 and 5 digit numbers) that I need to also grab and add to another column.
So, current column contains 4/5 digits as well as 10 digit numbers.... 4/5 digits I want to extract into a column.... 10 digits I want to extract into another column.
The solution you posted above works well in extracting them, except they are all in the same column... I can then do another transform from there. However, if the list doesn't contain either... I get "Errors" --- what is the best way for me to replace such cases with "null" if no match is found for >=4 ?