Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
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!
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.