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

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.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors