cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
pbeeyeqs Regular Visitor
Regular Visitor

Column: Add leading zeros left and right of hyphen

I have a text column in PowerBI, with numeric digits separated by a hyphen. I need the left side to be exactly 5 digits. If it is less, then add leading zeros. The right side needs to be 4 digits. Any less, add leading zeros.

For example:

  • 0002-800 -> 00002-0800
  • 0001-0800 -> 00001-0800
  • 12345-220 -> 12345-0220

Any help is appreciated. Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
v-yulgu-msft Super Contributor
Super Contributor

Re: Column: Add leading zeros left and right of hyphen

Hi @pbeeyeqs,

 

Based on Vvelarde's suggestion, I have something to add。 Before adding the custom column, please remove the "Change type" step after spliting column.

1.PNG

 

Then, you can paste the provided code into "add custom column" box.

2.PNG

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwMNK1MDBQitUBcwx1DWA8QyNjE1NdIyMgLxYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "ID", Splitter.SplitTextByDelimiter("-", QuoteStyle.Csv), {"ID.1", "ID.2"}),
    #"Added Custom" = Table.AddColumn(#"Split Column by Delimiter", "Custom", each Text.Repeat("0",5-Text.Length([ID.1])) & [ID.1] & "-" &
    Text.Repeat("0",4-Text.Length([ID.2])) & [ID.2]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"ID.1", "ID.2"})
in
    #"Removed Columns"

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
Vvelarde Super Contributor
Super Contributor

Re: Column: Add leading zeros left and right of hyphen

@pbeeyeqs 

 

Hi, you can do with Query Editor

 

Step 1: Select your column and Split it by Delimiter.

 

Step 2: Add a Custom Column : (ID.1 y ID.2 is the splitted columns).

 

Text.Repeat("0",5-Text.Length([ID.1])) & [ID.1] & "-" &
 Text.Repeat("0",4-Text.Length([ID.2])) & [ID.2]

 

Step 3: Remove other columns (ID1 and ID2)

 

Regards

 

Victor

 

 




Lima - Peru
Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!




Highlighted
v-yulgu-msft Super Contributor
Super Contributor

Re: Column: Add leading zeros left and right of hyphen

Hi @pbeeyeqs,

 

Based on Vvelarde's suggestion, I have something to add。 Before adding the custom column, please remove the "Change type" step after spliting column.

1.PNG

 

Then, you can paste the provided code into "add custom column" box.

2.PNG

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwMNK1MDBQitUBcwx1DWA8QyNjE1NdIyMgLxYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "ID", Splitter.SplitTextByDelimiter("-", QuoteStyle.Csv), {"ID.1", "ID.2"}),
    #"Added Custom" = Table.AddColumn(#"Split Column by Delimiter", "Custom", each Text.Repeat("0",5-Text.Length([ID.1])) & [ID.1] & "-" &
    Text.Repeat("0",4-Text.Length([ID.2])) & [ID.2]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"ID.1", "ID.2"})
in
    #"Removed Columns"

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Helpful resources

Announcements
Can You Solve These Challenge

Challenge: Can You Solve These?

Find out how to participate in the first Power BI 'Can You Solve These?' challenge.

Community News & Announcements

Community News & Announcements

Get your latest community news and announcements.

Virtual Launch Event

Microsoft Business Applications October Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

Community Kudopalooza

Win Power BI Swag with Community Kudopalooza!

Each week, complete activities and be qualified in the drawing for cool Power BI Swag.

Users Online
Currently online: 64 members 1,277 guests
Please welcome our newest community members: