Forum Discussion
Extract 5 Consecutive Numbers from Text String
- 4 years ago
Hi Anonymous ,
Paste this code over the default code in a new blank query to see the steps I took to complete this:
let removeChars = List.Transform({65..90, 97..122}, each Character.FromNumber(_)), Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("HY1LCgIxEETv0uuhMP2JcZ2QqAnMAYYsBHHlzvuDmd71q1dFHwdlvTBev/fnCxVLDKaN/KK5HVSVpSAisoaE8dQUdRUcvTDKXjtWfBUYeqv7uT/RdVMTBA2RccMovT6WdnadTRjBxLTds792oDn/", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Starting String" = _t, Extract = _t]), chgTypes = Table.TransformColumnTypes(Source,{{"Starting String", type text}, {"Extract", Int64.Type}}), addCalcOutput = Table.AddColumn(chgTypes, "calcOutput", each Text.Remove([Starting String], removeChars)), splitByDelimiter = Table.ExpandListColumn(Table.TransformColumns(addCalcOutput, {{"calcOutput", Splitter.SplitTextByDelimiter(".", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "calcOutput"), filterLengthFive = Table.SelectRows(splitByDelimiter, each Text.Length([calcOutput]) = 5) in filterLengthFiveSummary:
1) Create list of all letters, lower and upper case.
2) Use list to remove all text characters from starting string.
3) Split resulting values by "." delimiter into new rows.
4) Filter column on value length = 5.
This gives me the following output ([calcOutput] column):
Pete
Hi Anonymous ,
Paste this code over the default code in a new blank query to see the steps I took to complete this:
let
removeChars = List.Transform({65..90, 97..122}, each Character.FromNumber(_)),
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("HY1LCgIxEETv0uuhMP2JcZ2QqAnMAYYsBHHlzvuDmd71q1dFHwdlvTBev/fnCxVLDKaN/KK5HVSVpSAisoaE8dQUdRUcvTDKXjtWfBUYeqv7uT/RdVMTBA2RccMovT6WdnadTRjBxLTds792oDn/", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Starting String" = _t, Extract = _t]),
chgTypes = Table.TransformColumnTypes(Source,{{"Starting String", type text}, {"Extract", Int64.Type}}),
addCalcOutput = Table.AddColumn(chgTypes, "calcOutput", each Text.Remove([Starting String], removeChars)),
splitByDelimiter = Table.ExpandListColumn(Table.TransformColumns(addCalcOutput, {{"calcOutput", Splitter.SplitTextByDelimiter(".", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "calcOutput"),
filterLengthFive = Table.SelectRows(splitByDelimiter, each Text.Length([calcOutput]) = 5)
in
filterLengthFive
Summary:
1) Create list of all letters, lower and upper case.
2) Use list to remove all text characters from starting string.
3) Split resulting values by "." delimiter into new rows.
4) Filter column on value length = 5.
This gives me the following output ([calcOutput] column):
Pete
Pedantic suggestion:
Whitelisting digits rather than blacklisting letters is slightly simpler and slightly more robust.
That is, instead of Text.Remove([Starting String], removeChars), use Text.Select([Starting String], {".", "0..9"}).