Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Extract 5 Consecutive Numbers from Text String

Hello Everyone,   This is my first post so please let me know if I need to change anything.  I have a need to extract a 5 digit number from a text string.  Unfortunately there wasn't a specific for...
  • BA_Pete's avatar
    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
        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