Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
I am using Power BI to automate some CSV exports, and I can't find a way to keep leading zeroes. I have tried converting the data type to string, but still leading zeroes are omitted. The issue is not present when running the scripts directly in R or Python, so it seems to be related to Power BI.
Here are some examples scripts that can be used to reproduce the issue:
Using R script:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjA3MDUzU4rViVYyNDQwMDICMw2MDQwMgcxYAA==", 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}}),
#"Run R script" = R.Execute("# 'dataset' holds the input data for this script#(lf)#(lf)dataset$Column1 <- as.character(dataset$Column1)#(lf)fileName <- paste(""C:/test.txt"")#(lf)write.table(dataset, sep = "","", file = fileName, quote = FALSE, row.names = FALSE, col.names = FALSE)",[dataset=#"Changed Type"])
in
#"Run R script"
Using Python script:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjA3MDUzU4rViVYyNDQwMDICMw2MDQwMgcxYAA==", 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}}),
#"Run Python script" = Python.Execute("# 'dataset' holds the input data for this script#(lf)import pandas#(lf)data = pandas.DataFrame(dataset)#(lf)data = data.astype(str)#(lf)data.to_csv('C:/test.txt')",[dataset=#"Changed Type"])
in
#"Run Python script"
Input data is:
070566
110022
030012
Expected output is:
070566
110022
030012
Actual output is:
70566
110022
30012
Appreciate any help!
Solved! Go to Solution.
Hello, I did it successfully using column from example, generating this (obviously you might need to change the locationdata type):
Table.AddColumn(#"Changed Type", "Custom", each Text.PadStart(Text.From([Column1], "en-NZ"), 6, "0"), type text)
Ah man thats crazy. I am out of ideas other than to stick some weird character at the front then strip it out in the CSV itself:
Table.AddColumn(#"Changed Type", "Custom", each Text.PadStart(Text.From([Column1], "en-NZ"), 7, "|0"), type text).
Hello, I did it successfully using column from example, generating this (obviously you might need to change the locationdata type):
Table.AddColumn(#"Changed Type", "Custom", each Text.PadStart(Text.From([Column1], "en-NZ"), 6, "0"), type text)
Thanks for this! I needed to keep leading zeros for numbers 1 to 9. This worked for me when I edited it like this:
Text.PadStart(Text.From([Company No], "en-FI"), 1, "'"), type text)
Hi! Unfortunately, the resulting .txt file still showing without leading zeroes:
Here is the PQ script I am now running:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjA3MDUzU4rViVYyNDQwMDICMw2MDQwMgcxYAA==", 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}}),
AddColumn = Table.AddColumn(#"Changed Type", "Custom", each Text.PadStart(Text.From([Column1], "nb-NO"), 6, "0"), type text),
#"Run R script" = R.Execute("# 'dataset' holds the input data for this script#(lf)#(lf)dataset$Column1 <- as.character(dataset$Column1)#(lf)fileName <- paste(""C:/test.txt"")#(lf)write.table(dataset, sep = "","", file = fileName, quote = FALSE, row.names = FALSE, col.names = FALSE)",[dataset=AddColumn])
in
#"Run R script"
You may have to do it in your code R (or Python). For example, using the str_pad function in the stringer library.
Pat
To learn more about Power BI, follow me on Twitter or subscribe on YouTube.
Yeah, I did try that as well. It seems that even though the datatype in the Python/R script is definately a string, it will for some reason be converted to numbers in the CSV export. This behavior happens only when running the script in Power BI.
Ah man thats crazy. I am out of ideas other than to stick some weird character at the front then strip it out in the CSV itself:
Table.AddColumn(#"Changed Type", "Custom", each Text.PadStart(Text.From([Column1], "en-NZ"), 7, "|0"), type text).
I could do that, but then I would need to have a separate process to strip it again, since I need to deliver the CSV files in a that specific format... Thanks for the help though!
Check out the July 2025 Power BI update to learn about new features.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
User | Count |
---|---|
10 | |
8 | |
6 | |
6 | |
6 |