M code newb - question about custom formula to add a leading zero

Guitarmageddon

Board Regular
Joined
Dec 22, 2014
Messages
161
So in my source data, I have a column that is all store numbers. We use 5 digit site numbers, and some have leading zeroes until the numbers get high enough.

For example, store number:

01100
02100
05100
Then on to 14100, 15100, 16100, and so forth....

Power query gets rid of my leading zero. When I right click on that column within power query, and try to change type to text, it still gets rid of the leading zero. So Im not sure if there is an easier way to do this. I know in mcode there is a way to use IF statements but Im very new to it.

Basically Im trying to do something along these lines, but just say to excel, "if the Site Code column is less than 5 digits (since excel cuts the leading zero) then add a leading zero at the beginning. If its not, dont do anything.
VBA Code:
= Table.AddColumn(#"Changed Type", "Merged", each Text.Combine({"0", [Site Code]}), type text)
This is in the ballpark, but Im not sure how to tell it to stop when I get to the store numbers that require no action. Thoughts?

Like if Text.Length function returns less than 5, then Text.Combine etc
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Disregard this....so I ended up simply doing it again, and it gave me the option to transform to text, replace current column, and it in fact kept the leading zeros..... odd.

HOWEVER....if this is easy for someone to do, could you take me through the steps above if I hadnt gotten it to work?
 
Upvote 0
You could combine "00000" with your number, then use Text.End(text, 5) to just get the last 5 digits so you only get leading zeroes if needed. Or use Text.PadStart(text, 5, "0")
 
Upvote 0

Forum statistics

Threads
1,223,810
Messages
6,174,763
Members
452,582
Latest member
ruby9c

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top