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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
deefunk1981
Frequent Visitor

Extract first 4 digits from postcode from text column

Hi,

 

I have a column which has full addresses in it which due to the various ways the addresses have been handed over from the client I can't use the extract by delimiter function.  I would like to create a new column which extracts the first 4 digits from the postcode. I have provided a screenshot of what it should look like.  Can anyone help me please?

postcode snip 1.png

 

postcode snip.png

 

Thanks,

David

2 ACCEPTED SOLUTIONS
amitchandak
Super User
Super User

@deefunk1981 , One way is to search position of BT and then take 4 character

 

mid([column], search("BT",[Column],,len([Column])), 4)

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

View solution in original post

truptis
Community Champion
Community Champion

@deefunk1981 ,

Steps to follow:
1- Create a new column (by right clicking on a table and selecting new column option) and use the calculation given by @amitchandak 

new_Column = mid([column1], search(" BT",[Column1],,len([Column1])), 4)

this will give you a new column with the first 4 digits of your postcode.

 

search(" BT"...) is going to search in your text if " BT" is there. If it is there then it will take first 4 characters(for which we have used ",4" in the formula) from BT to the right including BT.

View solution in original post

7 REPLIES 7
deefunk1981
Frequent Visitor

Thank you so much for all your help everyone.  The solution is now working 😁

Do all the postal codes include "BT"?





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Yes, all the postcodes are Northern Ireland and thankfully all start with BT which makes things a lot easier.

truptis
Community Champion
Community Champion

@deefunk1981 ,

Steps to follow:
1- Create a new column (by right clicking on a table and selecting new column option) and use the calculation given by @amitchandak 

new_Column = mid([column1], search(" BT",[Column1],,len([Column1])), 4)

this will give you a new column with the first 4 digits of your postcode.

 

search(" BT"...) is going to search in your text if " BT" is there. If it is there then it will take first 4 characters(for which we have used ",4" in the formula) from BT to the right including BT.

PaulDBrown
Community Champion
Community Champion

Try using this M code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc29DoIwFIbhW/nC3IE/RUeMuqiEAMYBGRp6Io21NccS4t1rdNTxHZ68bRtsjfSIIxyI+0Fa/0DvRvYCGzVJVqg9E72zoImfAqsmmWGeFwJHqz0p7LS9KHcLOtEGSYh6IDLaopR8FdifxzCMszXx16YLLPPyr40yVG6SxjiLSgmcJDPZu9PWf64pkqb6ld0L", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [project.address_full = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"project.address_full", type text}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "project.address_full", "project.address_full - Copy"),
    #"Inserted Text Before Delimiter" = Table.AddColumn(#"Duplicated Column", "Text Before Delimiter", each Text.BeforeDelimiter([#"project.address_full - Copy"], ", United Kingdom"), type text),
    #"Inserted Last Characters" = Table.AddColumn(#"Inserted Text Before Delimiter", "Last Characters", each Text.End([Text Before Delimiter], 8), type text),
    #"Inserted First Characters" = Table.AddColumn(#"Inserted Last Characters", "First Characters", each Text.Start([Last Characters], 4), type text),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted First Characters",{"project.address_full - Copy", "Text Before Delimiter", "Last Characters"})
in
    #"Removed Columns"

Captura de pantalla 2022-03-14 130956.jpg

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






amitchandak
Super User
Super User

@deefunk1981 , One way is to search position of BT and then take 4 character

 

mid([column], search("BT",[Column],,len([Column])), 4)

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Thanks for your reply.  This solution makes sense and I think it will work.  However, I am unsure what process to follow to use this.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.