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

Join 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.

Reply
magnus_b
Advocate II
Advocate II

Keep leading zeroes when export to CSV using R or Python script

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!

2 ACCEPTED SOLUTIONS
samdthompson
Memorable Member
Memorable Member

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)

 

 

// if this is a solution please mark as such. Kudos always appreciated.

View solution in original post

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).

 

// if this is a solution please mark as such. Kudos always appreciated.

View solution in original post

7 REPLIES 7
samdthompson
Memorable Member
Memorable Member

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)

 

 

// if this is a solution please mark as such. Kudos always appreciated.

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:


Skjermbilde 2021-05-06 kl. 08.48.21.png

 

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

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI 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).

 

// if this is a solution please mark as such. Kudos always appreciated.

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!

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.