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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Syndicate_Admin
Administrator
Administrator

Extract leading zeros from a column with data type text

Hello

I have a column with both numeric and text values, but in power BI it is defined in text (because otherwise the texts would not be displayed and it is necessary). When this happens, the numeric values in the column are transformed and zeros are added to the left, I would like the original number to be kept without the zeros added. For example:

I have:

gerardcanals_0-1627563610687.png

And I would like to get:

material_number
28545-30
10020215-138
10020320-138
4000453300
940152
00400-C10
00120-C24
20000095-090

As you can see, the values in yellow/orange are the ones I need to modify, but at the same time, in the same column, I have alphanumeric values that start with zeros that I would like to keep (green).

I can't figure out how to do it. Obviously, if you change the data type to integer, the zeros disappear but it generates error in the fields that are alphanumeric.

Could someone help me?

7 REPLIES 7
v-rzhou-msft
Community Support
Community Support

Hi @Syndicate_Admin 

In addintion to   's reply, try If and Text.Contains to update the code.

New Code:

Desire_material_number
=
if 
Text.Contains([material_number], "-") 
then 
[material_number] 
else 
Text.TrimStart([material_number], "0")

Result:

1.png

Finally remove the original column.

 

Best Regards,

Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

Greg_Deckler
Super User
Super User

@Syndicate_Admin Perhaps 

=Text.TrimStart([Column1], "0")

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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Gràcias for the answer, but I do not solve the probema since that function also removes the zeros from the values :

00400-C10
00120-C24

@Syndicate_Admin Sorry, I'm not seeing that behavior or I am not understanding your exact needs:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwMTDQdTY0UIrVAfEMjYA8IxMoDwJMwISpsbEBUFUsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.TrimStart([Column1], "0"))
in
    #"Added Custom"

Greg_Deckler_0-1627575882534.png

 


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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Look yes! precisely in that image you see the problem to your solution.

As I comment, what I want to get is:

4000453300
00400-C10
00120-C24

With your solution, I get rid of the zeros of all cases. In the original explanation I comment that I just want to remove the zeros from the values that are integers and that the values that are alphanumeric keep the zeros.

gerardcanals_0-1627625483765.png

That those that have been marked in yellow have the zeros removed and that those that have been marked in green remain the same.

@Syndicate_Admin So then just add an if statement that if the value contains a hypen, don't do the trim.


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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

good!

Muhas thanks for the answer! The truth is that there are more casuistry, it should be an if statement that if the value contains an alphanumeric, do not make the cut. The truth is that I do not know if the possibility is successful, nor how it would be done, but for now I have solved the problem by doing several steps with the data transformer.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.