Forum Discussion

reinsken's avatar
reinsken
Frequent Visitor
5 years ago
Solved

Extracting specific pattern from text

Hi, I'm trying to extract from the column "Description" a specific string patter (in this example it's "BE12 3456 7890 1234" (19 characters) in Power Query. I've searche dozen of boards, but can't...
  • PhilipTreacy's avatar
    PhilipTreacy
    5 years ago

    Hi reinsken 

    This code extracts what you want.  Download PBIX file with code here.

     

     

    let
        NumList = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"},
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("DY07DoNQDASvsqKOEBIQkjYJV0iDKAyYyNL7IGNz/rxqm5nZaaq+clH0E6vrWSZQVmbD5eFwI+Man5x4RcyBTxPG4eGSRIojq1GN13h/oG+fDZqh7zA0bYfRhCJ29p+QIfBqJc2OUoAkY908otytcojVeLvSIuaKlBNYi7OzRk7m8YYlUNoKVs3zHw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Description = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Description", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "FirstNum", each List.PositionOfAny(Text.ToList([Description]), NumList)),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each Text.Middle([Description], [FirstNum]-2, 19))
    in
        #"Added Custom1"

     

     

    Delete the FirstNum column, I just left it there so you can see how the code's working.

    Phil 


    If I answered your question please mark my post as the solution.
    If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.

  • Jimmy801's avatar
    5 years ago

    Hello reinsken 

     

    you can search for "BE" in you text and then extract 19 characters if this is specific enough

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKk48vC07JyUt6/C24kSFbDBDISexODsrJU3BydXQSMHYxNRMwdzC0kDB0MjYROHwNqAkRH1OIoihFBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Description = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Description", type text}}),
        Extract = Table.AddColumn
        (
            #"Changed Type",
            "Extract",
            each Text.Range(_[Description], Text.PositionOf(_[Description], "BE", Occurrence.First),19 )
        )
    in
        Extract

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy