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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
SwethaSatya
New Member

R Script in direct query

I am using R Script in Direct Query to retrieve data from SQL server using SP by passing logged-in user id.

In Power Query Editor window, the script runs fine and retrieves data. But on applying changes to the report, it is throwing below error:

Microsoft SQL: Incorrect syntax near the keyword 'EXEC'.
Incorrect syntax near ')'.

 

Sample code:

let
RScript = R.Execute("output <- read.table(text=system2(""whoami"", stdout=TRUE))"),
output = Record.FieldValues(RScript{[Name="output"]}[Value]{0}){0},

Source = Sql.Database("SQLServerName", "DBName", [Query="EXEC SP_Name '"&output&"'"])
in
Source

3 REPLIES 3
d_gosbell
Super User
Super User

R script is not supported as a direct query source see https://docs.microsoft.com/en-us/power-bi/connect-data/power-bi-data-sources#data-sources-p-r 

 

You might want to look into using either CURRENT_USER or SUSER_NAME or something like that inside your SQL query and then just use a SQL Server data source in Power Query

We are using SQL server authentication to connect to DB. In this scenario, how do I pass LoggedIn user as parameter to SP?


@SwethaSatya wrote:

We are using SQL server authentication to connect to DB. In this scenario, how do I pass LoggedIn user as parameter to SP?


You can't you would need to use Windows Auth to use either of those functions.

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

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.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.