Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

How to extract 5/6 length ID numbers from a text string?

Hi, I'm searching for a best-practice method to solve the following problem. I'm trying to extract a 5/6 length ID number from the following Page column. I want to extract the 5 & 6 lengt...
  • Zubair_Muhammad's avatar
    Zubair_Muhammad
    7 years ago

    Anonymous

     

    Try this..

     

    I think it will be close.
    Please see the attached file as well

    Tricks I learnt from ImkeF.  :smileywink:
    But I believe she still would have a better solution

     

    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 = 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

  • ImkeF's avatar
    ImkeF
    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