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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
lipster26
Frequent Visitor

Extract State from text field

Hello,

I'm looking for a solution to extract the 2 characters for States from a free form text field. The 2 characters might be anywhere in the text column. I can also have another table that would contain the states abbreviations to do a lookup. But I'm not sure how to extract the 2 characters from Text column. Thanks for any help.

 

TextDesired Result
2021 NY EXT PMTNY
2017 CEG PA REFUNDPA
LA FRANCHISE TAX PMT RECODING TO ACCOUNT 21156300LA
MS FRANCHISE TAX PMT RECODING TO ACCOUNT 21156300MS
TN FRANCHISE TAX PMT RECODING TO ACCOUNT 21156300TN
CPG-LA2019-20 FRANCHISE TAX PMT RECODINGLA
CPG-MS 2020 FRANCHISE TAX PMT RECODINGMS
CPG-NC 2020 FRANCHISE TAX PMT RECODINGNC
TN FRANCHISE TAX PMT RECODING TO 21156300TN
2017 PA INCOME TAX NOTICE PAYMENTPA
2018 TN REFUNDTN
1 ACCEPTED SOLUTION
Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Based on the pattern, I can notice that any two alphabets word which appears first is a state in your strings. You can use following formula in a custom column without needing a lookup table

= List.First(List.Select(Text.SplitAny(Text.Trim(Text.Remove([Text],{"0".."9"})),"  -"),(x)=>Text.Length(x)=2))

 But if any two alphabets word appear first which is not a state name, then you would require a lookup table. This approach is suggested by @artemus

View solution in original post

2 REPLIES 2
Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Based on the pattern, I can notice that any two alphabets word which appears first is a state in your strings. You can use following formula in a custom column without needing a lookup table

= List.First(List.Select(Text.SplitAny(Text.Trim(Text.Remove([Text],{"0".."9"})),"  -"),(x)=>Text.Length(x)=2))

 But if any two alphabets word appear first which is not a state name, then you would require a lookup table. This approach is suggested by @artemus

artemus
Microsoft Employee
Microsoft Employee

Add a custom column that splits the Text into words (add additional word bounderies as needed).

Text.SplitAny([Text], " .,;")

Click the new column and choose "Expand to New Rows"

Perform an inner join with your state table in order to filter it.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors