Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
JohnHarker
New Member

Extracting a sequence in Power Query

I have a long column of data where I need to extract a specific sequence. I am looking for the format ####-##A####. The problem is there is no pattern as to where this sequence apprears in the cell and the letter could be any letter.

 

I need to find the sequence and then copy it to a new column, is there a way to do this with a custom column in Power Query?

 

JohnHarker_0-1725004277377.png

Thank you

10 REPLIES 10
Anonymous
Not applicable

I hope you’re doing well. I wanted to check in and see if the issue you mentioned has been resolved. If it has, could you please mark the response that helped you as the solution? This will make it easier for other users to find the answer quickly.

 

Thank you for your cooperation!

 

Best regards,

Stephen Tao

Ahmedx
Super User
Super User

and try this

 

let
f=(w)=> try  Splitter.SplitTextByRanges(
    {{
         [
             a = List.Transform( {0..9},(x)=>  "-"& Text.From(x)),
             b = List.Transform(a,(x)=>Text.PositionOf(w,x,2)),
             c = List.Sort( List.Combine(b),1){1}+1][c],12}}
             )(w){0} otherwise "",


        Source = Table.FromRows({
        {"519-1-16800 Work in progress-WHQ projects-Branch (Travaux en cours - Immobilisations)-2234-10S1460-Congo (Kinshasa) Lubumbashi Branch"},
        {"519-1-12910 Prepaid expenses-Service contracts-2294-11S4992-Congo (Kinshasa) Elephants Branch Annex-Contract Support\\Contract"},
        {"519-1-21510 Accrued liabilities payable-Service contracts-2294-11S4993-Congo (Kinshasa) Elephants Branch Annex-Contract Support\\Contract"},
        {"ddddddddd"}
    }, type table [Sample = text ]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each f([Sample]))
in
    #"Added Custom"

 

Screenshot_4.png

Ahmedx
Super User
Super User

pls try this

let
f=(x)=> Text.AfterDelimiter( Text.Split(x,"-Congo"){0},"-",{1,RelativePosition.FromEnd}),
    Source = Table.FromRows({
        {"519-1-16800 Work in progress-WHQ projects-Branch (Travaux en cours - Immobilisations)-2234-10S1460-Congo (Kinshasa) Lubumbashi Branch"},
        {"519-1-12910 Prepaid expenses-Service contracts-2294-11S4992-Congo (Kinshasa) Elephants Branch Annex-Contract Support\\Contract"},
        {"519-1-21510 Accrued liabilities payable-Service contracts-2294-11S4993-Congo (Kinshasa) Elephants Branch Annex-Contract Support\\Contract"}
    }, type table [Sample = text ]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each f([Sample]))
in
    #"Added Custom"

 Screenshot_2.png

PwerQueryKees
Super User
Super User

Awesome @AlienSx ...

thanks,  ! To be honest, I've made a mistake - there must be 12 (not 11) in Text.Range(txt, find_position + 1, 11)

 

@PwerQueryKees

Hilarious! Still a very creative and generic way to tackle OP's problem....

wdx223_Daniel
Super User
Super User

NewStep=Table.AddColumn(YourTable,"Extract",each List.RemoveNulls(List.Transform({0..Text.Length([LongTextColumn])-12},(x)=>let a=Text.Range([LongTextColumn],x,11),b=Text.SplitAny(a,{"-","A".."Z"}) in if List.Count(b)<>3 then null else  if List.AllTrue(List.Transform({0..2},(y)=>Text.Remove(b{y},{"0".."9"})="" and Text.Length(b{y})={4,2,4}{y}) then a else null){0}?)

AlienSx
Super User
Super User

let
    chars = List.Transform({"a".."z", "A".."Z"}, (x) => {x, "A"}),
    nums = List.Transform({"0".."9"}, (x) => {x, "#"}),
    replacements_list = List.Buffer(List.Combine({chars, nums})),
    pattern = "-####-##A####-",
    find_pattern = (txt) =>
        [lst = Text.ToList(txt), 
        rpl = List.ReplaceMatchingItems(lst, replacements_list),
        t = Text.Combine(rpl), 
        find_position = Text.PositionOf(t, pattern, Occurrence.First), 
        res = if find_position <> -1 then Text.Range(txt, find_position + 1, 11) else null][res],
    // edit Source to get your original data
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    new_col = Table.AddColumn(Source, "pattern", (x) => find_pattern(x[column_name]), type text)
in
    new_col

pattern.png

 

ZhangKun
Super User
Super User

let
    Source = Table.FromRows({
        {"519-1-16800 Work in progress-WHQ projects-Branch (Travaux en cours - Immobilisations)-2234-10S1460-Congo (Kinshasa) Lubumbashi Branch"},
        {"519-1-12910 Prepaid expenses-Service contracts-2294-11S4992-Congo (Kinshasa) Elephants Branch Annex-Contract Support\\Contract"},
        {"519-1-21510 Accrued liabilities payable-Service contracts-2294-11S4993-Congo (Kinshasa) Elephants Branch Annex-Contract Support\\Contract"}
    }, type table [Sample = text ]),
    fx = (str, pat) => Web.Page("<script>document.write('"& str &"'.match(/" & pat & "/g))</script>")[Data]{0}[Children]{0}{[Name="BODY"]}[Children][Text]{0}, 
    ExtractPattern = Table.AddColumn(Source, "Custom", each fx([Sample], "\d{4}-\d{2}[A-Z]\d{4}"))
in
    ExtractPattern
m_dekorte
Super User
Super User

Hi @JohnHarker ,

 

Give something like this a go, its a bit verbose but does the trick.

let
    Source = Table.FromRows({
        {"519-1-16800 Work in progress-WHQ projects-Branch (Travaux en cours - Immobilisations)-2234-10S1460-Congo (Kinshasa) Lubumbashi Branch"},
        {"519-1-12910 Prepaid expenses-Service contracts-2294-11S4992-Congo (Kinshasa) Elephants Branch Annex-Contract Support\\Contract"},
        {"519-1-21510 Accrued liabilities payable-Service contracts-2294-11S4993-Congo (Kinshasa) Elephants Branch Annex-Contract Support\\Contract"}
    }, type table [Sample = text ]),
    ExtractPattern = Table.AddColumn(Source, "Custom", each  
        Text.Combine( 
            List.Select( 
                List.TransformMany( 
                    Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"-"})([Sample]), 
                    each Splitter.SplitTextByCharacterTransition({"-"}, (c) => not List.Contains({"0".."9"}, c))(_),
                    (a, b) => Text.TrimEnd( Text.TrimStart(b, "-"), "-")
                ), (x)=> List.AllTrue(
                    {
                        Text.Length(x) =12, 
                        Text.PositionOf(x, "-") =4, 
                        Text.PositionOf(x, Text.Select(x, {"A".."Z"})) =7
                    }
                )
            ), ", "
        )              
    )
in
    ExtractPattern

with this result

m_dekorte_0-1725007523810.png

 

I hope this is helpful

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.