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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
v2
Frequent Visitor

Power Query add a leading Zero for variable length field

Hi,

I have date field that could have a lenght of 5 characters or 6 character as below:

 

11220 (read as 1/12/20)

121220 (read as 12/12/20)

 

How do i format this to be 6 characters. so if its 5 characters then add a leading 0 else leave as is. And also format with a "-"

 

the solution must be:

 

01-12-20

12-12-20

 

thanks

 

 

1 ACCEPTED SOLUTION

Thanks for your solution. I was unable to open your file as i have an older version of powerBi on my work computer, but used your solution and modified it to the below solution and it seems to work. Thanks.

 

= Table.AddColumn(#"Kept Last Rows", "DATES_FIXED", each if Text.Length([DATE]) = 6 then ([DATE]) else "0" & [DATE])

View solution in original post

6 REPLIES 6
mussaenda
Super User
Super User

How's your date if the month is October below?

v2
Frequent Visitor

Thanks for your response. I think i have found a solution.

themistoklis
Community Champion
Community Champion

@v2 

 

In a new step in PQ you can add the following code:

if Text.Length([Values]) = 6 then Text.Start([Values],2) & "-" & Text.Middle([Values],3,2) & "-" & Text.End([Values],2) else if Text.Length([Values]) = 5 then "0" & Text.Start([Values],1) & "-" & Text.Middle([Values],2,2) & "-" & Text.End([Values],2) else 0

 

See attached file

 

Thanks for your solution. I was unable to open your file as i have an older version of powerBi on my work computer, but used your solution and modified it to the below solution and it seems to work. Thanks.

 

= Table.AddColumn(#"Kept Last Rows", "DATES_FIXED", each if Text.Length([DATE]) = 6 then ([DATE]) else "0" & [DATE])

amitchandak
Super User
Super User

@v2 , Try like

Text.PadStart([Date], 6,"0")

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thanks for your solution AmitChandak.

Helpful resources

Announcements
October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors