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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
NPC
Helper I
Helper I

Find Last Code

Hi All,

 

Wondering if you can help.

 

Basically I’m trying to write a query that is going to pull specific codes (numbers) from a column that is mostly text (comments). The challenge is that the same or a few other codes can be present within that column and the only one I need is the one that appears last. Here is an example of what a column named “Comments” can look like :

 

“-------------------- FSA This inspection needs to be completed within the next 12 months. Code 004-22. The issue is not urgent Reviewed by xxxx ---------------------------------------- FSA After reviewing this record, this inspection needs to be completed in 3 months. Recomending 002-22. Reviewed by yyyy.”

 

In this case, I would want my query find “002-22 ”. 

 

There is no particular style/format that folks are entering their comments other than the codes.

 

Is there a way I can have the query read from right to left or bottom to top so I can only pull the last code.

 

Thank you for your help.

1 ACCEPTED SOLUTION
hnguy71
Super User
Super User

@NPC ,

 

Try this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LY7BDsIgEER/ZcJZTIFqwtk/MN5IDwpri6aQFFr17wV0k93D7L6dMYadoiN0Xc+l3OMyEXxKa50IMWNdRgoZZ9o8vcjh9sG7VBFsnCk4H8YCywqzYWeYkIr3hyOuyd1rQ0nBpWq7zUbtn48ErTXXuml5KkYxUDGDLUma2EBRn8JF+gWZifL/YvgC", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),

    // Source: https://www.excel-first.com/pattern-matching-in-power-query/
    // what is your current pattern.
    Pattern = "***-**",

    // length of pattern
    pLength = 6,

    // valid characters to look for
    ValidCharacters = List.Transform({0..9}, each Text.From(_)),

    // replace valid chars with pattern key
    ReplaceValidWithPatternKey = List.Zip({ValidCharacters,List.Repeat({"*"},List.Count(ValidCharacters))}),

    // find pattern. return last pattern.
    LastCode = 
        Table.AddColumn(
            Source, 
            "LastCode", 
            (x) => 
                Text.End(
                    Text.Combine(
                        List.Transform( 
                            Text.PositionOf( 
                                Text.Combine( 
                                    List.Transform( 
                                        List.ReplaceMatchingItems( 
                                            Text.ToList( x[Column1]), ReplaceValidWithPatternKey
                                            ), 
                                            Text.From
                                        )
                                    ), 
                                Pattern , Occurrence.All
                            ), 
                    each Text.Range(x[Column1],_, pLength )
                        ), ","
                ),
                pLength
            ),
            type text
        )
in
    LastCode

 

hnguy71_0-1664682917866.png

 

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

View solution in original post

3 REPLIES 3
hnguy71
Super User
Super User

@NPC ,

 

Try this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LY7BDsIgEER/ZcJZTIFqwtk/MN5IDwpri6aQFFr17wV0k93D7L6dMYadoiN0Xc+l3OMyEXxKa50IMWNdRgoZZ9o8vcjh9sG7VBFsnCk4H8YCywqzYWeYkIr3hyOuyd1rQ0nBpWq7zUbtn48ErTXXuml5KkYxUDGDLUma2EBRn8JF+gWZifL/YvgC", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),

    // Source: https://www.excel-first.com/pattern-matching-in-power-query/
    // what is your current pattern.
    Pattern = "***-**",

    // length of pattern
    pLength = 6,

    // valid characters to look for
    ValidCharacters = List.Transform({0..9}, each Text.From(_)),

    // replace valid chars with pattern key
    ReplaceValidWithPatternKey = List.Zip({ValidCharacters,List.Repeat({"*"},List.Count(ValidCharacters))}),

    // find pattern. return last pattern.
    LastCode = 
        Table.AddColumn(
            Source, 
            "LastCode", 
            (x) => 
                Text.End(
                    Text.Combine(
                        List.Transform( 
                            Text.PositionOf( 
                                Text.Combine( 
                                    List.Transform( 
                                        List.ReplaceMatchingItems( 
                                            Text.ToList( x[Column1]), ReplaceValidWithPatternKey
                                            ), 
                                            Text.From
                                        )
                                    ), 
                                Pattern , Occurrence.All
                            ), 
                    each Text.Range(x[Column1],_, pLength )
                        ), ","
                ),
                pLength
            ),
            type text
        )
in
    LastCode

 

hnguy71_0-1664682917866.png

 

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!
NPC
Helper I
Helper I

Thanks @amitchandak. Still trying to read through what you suggested. 

amitchandak
Super User
Super User

@NPC , try to use text analytics.if that can help https://learn.microsoft.com/en-us/power-bi/transform-model/desktop-ai-insights

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.