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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
rdstone230
Regular Visitor

Extracting two letter State abbreviation from Address Field

I'm looking to extract the two letter State abbreviation from an address field that has an inconsistent pattern.  I tried filtering as:

=List.Select(Address Field, each Text.Length(_) = 2)

but the results were not consistent.

 

Address Field

7904 NW Tiffany Springs Pkwy, Kansas City, MO 64152, USA

F6X5+C4 Beebeetown, IA, USA

J27C+29 Modale, IA, USA

W6W4+GX Sentinel Butte, ND, USA

I-94, Terry, MT 59349, USA

US-395, LIND, WA 99341, USA

5 REPLIES 5
dufoq3
Super User
Super User

Hi @rdstone230, try this. It also handles Country Code with space cases (just for sure).

 

Result

dufoq3_1-1713084524940.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fc3BCoJAFIXhVzm4dYTSUZmlGoWVFqiMIC6MxpBkDJ0Q3z7dJG1aXr7/cItCc9mGIuZIm7qu5ITk1TfyMeD6HCeCUyWHakDQqPmILnDo1jYJssTTSlJoeye39YDCF+ImhOpGSRB6qx9NN9BNhqi7V634Ne5wqh9yJEKqRooW/lupuYl3axMajBKkou+X9ylsZlG2cpYYFrMJzuEy4h7Y7Nv/jm9QfgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Address Field" = _t]),
    Ad_State = Table.AddColumn(Source, "State", each 
        [ a = Splitter.SplitTextByAnyDelimiter(Text.ToList(" ,"))([Address Field]),
          b = List.Select(a, (x)=> Text.Length(x) = 2 and Text.Length(Text.Select(Text.Upper(x), {"A".."Z"})) = 2),
          c = List.Last(b)
        ][c], type text)
in
    Ad_State

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

ronrsnfld
Super User
Super User

This should work even if some are missing the Country Code at the end. It looks for the last space or comma separated substring that consists of just two capital letters.

 

Note: Code edited to ensure two character string are, indeed, Letters.

 

 

   stateAbbrev = Table.AddColumn(#"Previous Step","State", (x)=>
        let 
            split = List.Reverse(Text.SplitAny(x[Address Field]," ,")),
            twoCaps = List.Select(split, each Text.Length(_)=2 and Text.Upper(_)=_ and (try Number.From(_))[HasError])
        in 
            twoCaps{0}, type text)

 

Greg_Deckler
Community Champion
Community Champion

@rdstone230 Table.AddColumn(#"Changed Type", "Custom", each let splitColumn1 = List.Reverse(Splitter.SplitTextByDelimiter(", ", QuoteStyle.None)([Column1])) in Text.Start(splitColumn1{1}?, 2), type text)



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler I was able to have it work by chaging the formula to:
Table.AddColumn(#"Changed Type", "Custom", each let splitColumn1 = List.Reverse(Splitter.SplitTextByDelimiter(", ", QuoteStyle.None)([Column1])) in Text.Start(splitColumn1{1}?, 3), type text)

 

This solved it!  Thank you very much for your help!

@Greg_Deckler  - this is very close, but its not returning me with the 2 letters.  The outcome is only the first letter of the two letter State abbrevaition. 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors