Get certified for free when you join Fabric Data Days 2026 and dive into Fabric, Power BI, SQL, AI, and other essential data skills.
Join nowJuly 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more
I have an Excel file that I want to transform in Power Query using R script. The script runs fine in R Studio, but when I run it as a query in Power Query, I get the error: Error in relevel.factor(dataset_name$Column6, ref = "FactorA") : 'ref' must be an existing level.
ErrorCode=-2147467259
ExceptionType=Microsoft.PowerBI.Scripting.R.Exceptions.RScriptRuntimeException
'Ref' is an existing level, and as I mentioned, this section of code runs correctly in R Studio. Prior to this step, Power BI applies the following steps automatically when I bring in the Excel file and select Transform Data:
Is one of these steps changing something in the data that is making it difficult for R to recognize the data type? I have used this same code in Power BI through the Get Data > R script functionality (along with file path information in the R code) without a problem.
I am wanting to use R in Power Query to hopefully be able to change the data source to other files with the same structure, and then Power BI will automatically run it through the queries and update the visuals according to the new data file.
Thank you!
Ellen
Solved! Go to Solution.
It would appear that the problem was due to a space being entered at the end of "Factor A ". Trimming white space solved the problem. Of course, now I have another problem, but I'll put that in another thread.
@edaugherty , Have you installed all required packages, refer : https://docs.microsoft.com/en-us/power-bi/connect-data/desktop-r-in-query-editor
or
Thanks for responding, @amitchandak!
I assume you mean the R packages. Yes, all necessary packages are installed and loaded. In any case, the function causing problems is from base R, not a package. As I mentioned, the script works just fine when used through the Get Data > R script pathway. It does not work when I use it to transform the original Excel file in Power BI's Power Query.
It would appear that the problem was due to a space being entered at the end of "Factor A ". Trimming white space solved the problem. Of course, now I have another problem, but I'll put that in another thread.
Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.
Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.
| User | Count |
|---|---|
| 22 | |
| 22 | |
| 18 | |
| 16 | |
| 13 |
| User | Count |
|---|---|
| 63 | |
| 41 | |
| 40 | |
| 39 | |
| 38 |