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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
on_qq
New Member

Trim a string with character then with 5 digits (C#####)

Hi, how can I return a new column with the format in first character follow with 5 digits in power query? Thanks a lots. 

on_qq_0-1678754898470.png

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LYwxDoAgEAS/sqG2gQOB8rS2QitCDDG0Fkp8vwTtNrOTiVFIReBpRqhXKXXAokhqEmmIgnXbavfeI+Sz5t/pnzEG8q7gp+AYrSPdsSKNtTT+qdwC1mELLFJ6AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Address = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Address", type text}})
in
#"Changed Type"

1 ACCEPTED SOLUTION
tackytechtom
Super User
Super User

Hi @on_qq ,

 

How about this?

 

tackytechtom_0-1678757475140.png

 

Credits to @ImkeF.

 

Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LYwxDoAgEAS/sqG2gQOB8rS2QitCDDG0Fkp8vwTtNrOTiVFIReBpRqhXKXXAokhqEmmIgnXbavfeI+Sz5t/pnzEG8q7gp+AYrSPdsSKNtTT+qdwC1mELLFJ6AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Address = _t]),
    #"ChangedType" = Table.TransformColumnTypes(Source,{{"Address", type text}}),
    #"CharactersToReplace" = Text.Combine(List.RemoveItems(List.Transform({1..126}, each Character.FromNumber(_)),{"0".."9"})),
    Convert=Table.AddColumn(ChangedType, "Custom1", each Text.SplitAny(
                            [Address],
                            CharactersToReplace)
                            ),
    Convert2=Table.AddColumn(Convert, "Custom2", each List.Select(
			[Custom1], 
			(li) => Text.Length(li) >=5){0}
			),
    #"Added Custom" = Table.AddColumn(Convert2, "Custom", each Text.Range ([Address], Text.PositionOf ([Address], [Custom2]) - 1, 6))
in
    #"Added Custom"


I suppose the idea is to fetch the postal code that is in the shape of "A12345". Be aware, though, that in case you have a street number with five or more digits, the solution above might not work as expected.

 

Let me know if this helps! 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

View solution in original post

1 REPLY 1
tackytechtom
Super User
Super User

Hi @on_qq ,

 

How about this?

 

tackytechtom_0-1678757475140.png

 

Credits to @ImkeF.

 

Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LYwxDoAgEAS/sqG2gQOB8rS2QitCDDG0Fkp8vwTtNrOTiVFIReBpRqhXKXXAokhqEmmIgnXbavfeI+Sz5t/pnzEG8q7gp+AYrSPdsSKNtTT+qdwC1mELLFJ6AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Address = _t]),
    #"ChangedType" = Table.TransformColumnTypes(Source,{{"Address", type text}}),
    #"CharactersToReplace" = Text.Combine(List.RemoveItems(List.Transform({1..126}, each Character.FromNumber(_)),{"0".."9"})),
    Convert=Table.AddColumn(ChangedType, "Custom1", each Text.SplitAny(
                            [Address],
                            CharactersToReplace)
                            ),
    Convert2=Table.AddColumn(Convert, "Custom2", each List.Select(
			[Custom1], 
			(li) => Text.Length(li) >=5){0}
			),
    #"Added Custom" = Table.AddColumn(Convert2, "Custom", each Text.Range ([Address], Text.PositionOf ([Address], [Custom2]) - 1, 6))
in
    #"Added Custom"


I suppose the idea is to fetch the postal code that is in the shape of "A12345". Be aware, though, that in case you have a street number with five or more digits, the solution above might not work as expected.

 

Let me know if this helps! 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors