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

Data Days is here! Join us now for 60+ days of learning, challenges, and connection. Learn more

Reply
jkraush1
Frequent Visitor

Query Editor R Script errors

I uploaded a .xlsx file into Power BI desktop and tried to run a simple R script on it in query editor as a test run.

Specifically the script I ran was:
# 'dataset' holds the input data for this script
dataset$Hour = format(dataset$Date, "%H")

Where Date is a column containing date-time information and dataset$Hour is an attempt to make a new column that just pulls the hour from dataset$Date. The comment the script editor comes with says that the open table is automatically named 'dataset', so I used the name dataset when referencing the sheet.

 

I tested this code directly in R and it worked fine, but when I tried to re-create my work in Power BI I got the following error:

DataSource.Error: ADO.NET: R script error.
Error in format.default(structure(as.character(x), names = names(x), dim = dim(x),  : 
  invalid 'trim' argument
Calls: format -> format.factor -> format -> format.default
Execution halted

Details:
    DataSourceKind=R
    DataSourcePath=R
    Message=R script error.


I am unsure how to procede here. What did I do wrong and what can I do to fix this going forward?

1 ACCEPTED SOLUTION
v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @jkraush1,

 

I got the same error when using above script. In my test, the Date column is set to DateTime, formatted as "1/1/2017 5:20:20AM".

 

Please try below code.

Hour=format(as.POSIXct(dataset$Date, format="%Y-%m-%d %H:%M:%S"), format="%H")

 

Regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
BeardyGeorge
Advocate I
Advocate I

given the errors you're getting, it's possible the data isn't formatted as you're expecting (or pbi is changing it somewhere along the way) - would you be able to give us some examples of the data you have?

 

Thanks,

 

George.

George,

It appears that power bi is changing how the data is formatted, from a datetime to a string. the as.POSIX code provided for me thankfully did not return an error

v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @jkraush1,

 

I got the same error when using above script. In my test, the Date column is set to DateTime, formatted as "1/1/2017 5:20:20AM".

 

Please try below code.

Hour=format(as.POSIXct(dataset$Date, format="%Y-%m-%d %H:%M:%S"), format="%H")

 

Regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hello @v-yulgu-msft,

I used that code and thankfully it does not return the same error as before. However, it seems to result in PowerBI outputting an empty table. In fact, I have tried a few other lines of code in R script and the result seems to always be a blank table with only empty columns for 'name' and 'value'. Any idea why this is happening?

jkraush1
Frequent Visitor

For clarification that line was all the code I had written in the script.

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.