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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
amitabhk1971
Helper I
Helper I

SQL query in direct query mode

Hi,

 

I am running a sql query in power bi to get data in direct query mode. The query throws correct result on sql server without any syntax error. However, when I run the same query in direct query mode, connecting to Azure SQL Database, in power bi's advanced options box under get data it throws correct result when I click the OK button but when I click the Load button on the same resultset it throws a syntax error message. The part where it is throwing the error is below:

 

DECLARE @LatestPackageVersion int;
SELECT
@LatestPackageVersion = MAX(PackageId)
FROM [Packages];
DECLARE @CurrentTime AS datetime;
SELECT
@CurrentTime = GETUTCDATE()

;

with cte_.....-- code continues.

 

The error message is:

Power BI error.jpg

 

 

 

 

 

 

 

 

 

 

 

Anyone getting any clue why is this happening?

 

Thanks,

Amitabh

2 ACCEPTED SOLUTIONS


@amitabhk1971 wrote:

Well I am using only the select part of the stored procedue. I am not using exec procname. 


@amitabhk1971

In my test on SQL Server, in DQ mode, Power BI sends a query wrapped as below.

 

SELECT XXXX, XXXX, XXX, XX FROM (

your query in you input in the pbi desktop

) t

So in your case, the actually query is 

SELECT XXXX, XXXX, XXX, XX FROM (

declare ....
...
select..

) t

That's why the you got the SQL syntax error. This is on SQL Server, and I think Power BI does the same thing to Azure SQL. As a workaround, instead of DECLARE statement, replace those variables in the SQL with a scalar value sub query.The CTEs can't work in this case as well, Try to recompose the SQL like.

 

From

 

FROM

DECLARE @LatestPackageVersion int; SELECT @LatestPackageVersion = MAX(PackageId) FROM [Packages]; DECLARE @CurrentTime AS datetime; SELECT @CurrentTime = GETUTCDATE() ;with cte as(
....
where datetime = @CurrentTime
and version = @LatestPackageVersion
)
select * from cte

TO

SELECT * FROM (
..
where datetime = GETUTCDATE()
and version = (select MAX(PackageId) FROM [Packages])

) cte

 

 

View solution in original post

Thanks Eric. That is what I did. I replaced the parameters with their select queries and now it works. 

 

View solution in original post

12 REPLIES 12
amitabhk1971
Helper I
Helper I

Hi,

 

I am trying to use MAX function in PowerBI using DAX/SQL in direct query mode but it says MAX is not supported in direct query mode. I checked the microsoft site for supported commands in DQ mode and MAX is included, but it is throwing this error on power bi. I went to options and checked "Allow unrestricted measures in direct query mode" under DirectQuery. Did not work. Does anyone know the solution. I need DQL mode because in DQ mode the report is real time while in import mode it needs to be refreshed as per a schedule. Any clue anyone? Thanks


@amitabhk1971 wrote:

Hi,

 

I am trying to use MAX function in PowerBI using DAX/SQL in direct query mode but it says MAX is not supported in direct query mode. I checked the microsoft site for supported commands in DQ mode and MAX is included, but it is throwing this error on power bi. I went to options and checked "Allow unrestricted measures in direct query mode" under DirectQuery. Did not work. Does anyone know the solution. I need DQL mode because in DQ mode the report is real time while in import mode it needs to be refreshed as per a schedule. Any clue anyone? Thanks


@amitabhk1971

The MAX function is allowed in DQ mode, based on my test on SQL Server. What database are you using and could you upload a snapshot of the "MAX is not supported in DQ mode"?

 

By the way, for real time purpose, you could also reference  Real-time streaming in Power BI

Hi Eric I am again stuck at the same point. In Direct query mode the following code is not running. Please see the error screenshot.

 

DECLARE @LatestPackageVersion int;
SELECT
@LatestPackageVersion = MAX(PackageId)
FROM [Packages]

 

The error:

DQ error.jpg

 

 

 

 

 

 

 

 

 

 

 

Any solution or workaround?

 

Amitabh


@amitabhk1971 wrote:

Hi Eric I am again stuck at the same point. In Direct query mode the following code is not running. Please see the error screenshot.

 

@DECLARE @LatestPackageVersion int;
SELECT
@LatestPackageVersion = MAX(PackageId)
FROM [Packages]

 

The error:

DQ error.jpg

 

 

 

 

 

 

 

 

 

 

 

Any solution or workaround?

 

Amitabh


@amitabhk1971

Those are SQL to do variable value assignment and it doesn't return any rows, that's why I think Power BI doesn't accept it. What's your goal when trying to run such SQL statements in Power BI desktop?

It is part of a stored procedure. When I run the stored procedure in PowerBI it says syntax arror at the Declare statement. This is the only place where there is a DECLARE statement. While the same stored procedure runs ok in sql server.

Amitabh

In fact the same stored procedure throws the correct result set in Power BI also when I click OK on the get data box. But it throws the error when I click LOAD button on the result box.

 

Amitabh


@amitabhk1971 wrote:

In fact the same stored procedure throws the correct result set in Power BI also when I click OK on the get data box. But it throws the error when I click LOAD button on the result box.

 

Amitabh


@amitabhk1971

I'm afraid that in DQ mode, executing a stored procedure is not supported. See this similar thread.

Well I am using only the select part of the stored procedue. I am not using exec procname. 


@amitabhk1971 wrote:

Well I am using only the select part of the stored procedue. I am not using exec procname. 


@amitabhk1971

In my test on SQL Server, in DQ mode, Power BI sends a query wrapped as below.

 

SELECT XXXX, XXXX, XXX, XX FROM (

your query in you input in the pbi desktop

) t

So in your case, the actually query is 

SELECT XXXX, XXXX, XXX, XX FROM (

declare ....
...
select..

) t

That's why the you got the SQL syntax error. This is on SQL Server, and I think Power BI does the same thing to Azure SQL. As a workaround, instead of DECLARE statement, replace those variables in the SQL with a scalar value sub query.The CTEs can't work in this case as well, Try to recompose the SQL like.

 

From

 

FROM

DECLARE @LatestPackageVersion int; SELECT @LatestPackageVersion = MAX(PackageId) FROM [Packages]; DECLARE @CurrentTime AS datetime; SELECT @CurrentTime = GETUTCDATE() ;with cte as(
....
where datetime = @CurrentTime
and version = @LatestPackageVersion
)
select * from cte

TO

SELECT * FROM (
..
where datetime = GETUTCDATE()
and version = (select MAX(PackageId) FROM [Packages])

) cte

 

 

Thanks Eric. That is what I did. I replaced the parameters with their select queries and now it works. 

 


@amitabhk1971 wrote:

Thanks Eric. That is what I did. I replaced the parameters with their select queries and now it works. 

 


@amitabhk1971

Glad to hear you've figured it out. If no further question, could you please mark the replies making sense as solution to close this thread?

Hi Eric,

 

I am actually using a long stored procedure in sql querying Azure SQL database. But anyways my purpose is to make my report real time. Thanks for referring to the Microsoft page regarding making a repport real time.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

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.