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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
amitabhk1971
Helper I
Helper I

SQL parameters in direct query

Hi,

I am using the below query as part of a sql query to generate report in Direct Query mode. It throws an error while loading the dataset. It says there is a syntax error at DECLARE. But when I open the query editor it does not show any syntax error. I think in Direct Query mode it does not accept DECLARE @variablename ....... statement. Can someone suggest a workaround? 

 

DECLARE @dates TABLE(dt datetime);
DECLARE @dateFrom datetime;
DECLARE @dateTo datetime;
SET @dateFrom = dateadd(dd,-7,getutcdate())
SET @dateTo = getutcdate()
WHILE(@dateFrom < @dateTo)
BEGIN
SELECT @dateFrom = DATEADD(minute,15,@dateFrom)
INSERT INTO @dates
SELECT @dateFrom
END;

 

This piece of query is at the top of the full query and then I use @dates  parameter at different places in the sql query. This works fine in import mode. But I need Direct Query mode to run this.

 

Thanks,

Amitabh

1 ACCEPTED SOLUTION
Eric_Zhang
Microsoft Employee
Microsoft Employee


@amitabhk1971 wrote:

Hi,

@I am using the below query as part of a sql query to generate report in Direct Query mode. It throws an error while loading the dataset. It says there is a syntax error at DECLARE. But when I open the query editor it does not show any syntax error. I think in Direct Query mode it does not accept DECLARE @variablename ....... statement. Can someone suggest a workaround? 

 

@DECLARE @dates TABLE(dt datetime);
@DECLARE @dateFrom datetime;
@DECLARE @dateTo datetime;
@set @dateFrom = dateadd(dd,-7,getutcdate())
@set @dateTo = getutcdate()
@WHILE(@dateFrom < @dateTo)
BEGIN
@SELECT @dateFrom = DATEADD(minute,15,@dateFrom)
@INSERT INTO @dates
@SELECT @dateFrom
END;

 

@This piece of query is at the top of the full query and then I use @dates  parameter at different places in the sql query. This works fine in import mode. But I need Direct Query mode to run this.

 

Thanks,

Amitabh


@amitabhk1971

In DirectQuery mode, Power BI sends the SQL Statement as below, that's why you got the invalid syntax.

SELECT XXX, XXX, XXX FROM
(
the sql statement input, in your case, the statement starts with DECLARE
)

As a workaround, you can create a built-in calendar table with 15 minutes difference of each datetime row and query from Power BI desktop instead of generating the table on  the fly.

 

let
    Source = Sql.Database("yourserver", "yourdb", [Query="select * from calendar where dt between '"&Date.ToText(Date.AddDays(Date.From(DateTimeZone.UtcNow()),-7), "yyyy/MM/dd")&"' and '"&Date.ToText(Date.From(DateTimeZone.UtcNow()), "yyyy/MM/dd")&"'"])
in
    Source

 

 

View solution in original post

1 REPLY 1
Eric_Zhang
Microsoft Employee
Microsoft Employee


@amitabhk1971 wrote:

Hi,

@I am using the below query as part of a sql query to generate report in Direct Query mode. It throws an error while loading the dataset. It says there is a syntax error at DECLARE. But when I open the query editor it does not show any syntax error. I think in Direct Query mode it does not accept DECLARE @variablename ....... statement. Can someone suggest a workaround? 

 

@DECLARE @dates TABLE(dt datetime);
@DECLARE @dateFrom datetime;
@DECLARE @dateTo datetime;
@set @dateFrom = dateadd(dd,-7,getutcdate())
@set @dateTo = getutcdate()
@WHILE(@dateFrom < @dateTo)
BEGIN
@SELECT @dateFrom = DATEADD(minute,15,@dateFrom)
@INSERT INTO @dates
@SELECT @dateFrom
END;

 

@This piece of query is at the top of the full query and then I use @dates  parameter at different places in the sql query. This works fine in import mode. But I need Direct Query mode to run this.

 

Thanks,

Amitabh


@amitabhk1971

In DirectQuery mode, Power BI sends the SQL Statement as below, that's why you got the invalid syntax.

SELECT XXX, XXX, XXX FROM
(
the sql statement input, in your case, the statement starts with DECLARE
)

As a workaround, you can create a built-in calendar table with 15 minutes difference of each datetime row and query from Power BI desktop instead of generating the table on  the fly.

 

let
    Source = Sql.Database("yourserver", "yourdb", [Query="select * from calendar where dt between '"&Date.ToText(Date.AddDays(Date.From(DateTimeZone.UtcNow()),-7), "yyyy/MM/dd")&"' and '"&Date.ToText(Date.From(DateTimeZone.UtcNow()), "yyyy/MM/dd")&"'"])
in
    Source

 

 

Helpful resources

Announcements
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.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

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