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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
Anonymous
Not applicable

Direct Query with Where clause for date time field

Hello 

 

Trying to get my direct query with the where clause to fetch the records for the last 30 days from today from a table with date time format. I have tried the below approach and could not get it to work as the variable Start_date is not recognised. What is the correct format to pass the parameter to this where clause. I cannot use the parameter function as the report need be refreshed daily and user will not have access to change parameters.  Appreciate all the help.

 

Query editor : 

Start_date=Date.From(Date.AddDays(DateTime.FixedLocalNow(),-30)),

Source = Sql.Database("System_APP_DB001", "APP_DB", [Query="SELECT [id], [CreatedOn] FROM [APP_DB1].[db].[Document] where [CreatedOn] > Start_date ", CreateNavigationProperties=false])

 

 Error Message shows 

 

DataSource.Error: Microsoft SQL: Invalid column name 'Start_date'.
Details:
DataSourceKind=SQL
DataSourcePath=System_APP_DB001;APP_DB
Message=Invalid column name 'Start_date'.
Number=207
Class=16

 

1 ACCEPTED SOLUTION

Such as:

 

"SELECT COL1, COL2 FROM Table WHERE Date_Column > " & Start_date

 

2. Usually not. As long as you don't get a warning about an operation not supporting direct query you are fine. Power Bi will append a where clause to the end of your query when filtering happens.

 

E.g. if your query is:

SELECT A FROM B.

 

Then  when a user of the report click a filter, Power Bi will do something like:

SELECT * FROM ( SELECT A FROM B) WHERE B == 'ABC'

View solution in original post

3 REPLIES 3
artemus
Microsoft Employee
Microsoft Employee

The reason your query does not work is that you have Start_date in the query string, so it is passed directly to SQL. If you want it to be fixed based on last refresh, then you would: "<Query Text>" & Start_date

 

However, if you want to use direct query, you don't need to do anything. Just load in the data using direct query (without the where clause), then in your report, add a filter on the CreatedOn field, set it to filter within last 30 days (if you want, you can hide the filter).

Anonymous
Not applicable

@artemus 

 

Sorry. Are you saying the condition should be changed to & Start_date &. Tried it and that gives an error. 

 

Message=Incorrect syntax near '&'.

 

On the second suggestion, doesn't the model still try to query the whole table? I do have multiple joins on subsequent steps after filtering this table .I am new to the Power BI and trying to understand this from a performance point of view if applying the filter on the report is good enough to restrict the data fetch. 

Such as:

 

"SELECT COL1, COL2 FROM Table WHERE Date_Column > " & Start_date

 

2. Usually not. As long as you don't get a warning about an operation not supporting direct query you are fine. Power Bi will append a where clause to the end of your query when filtering happens.

 

E.g. if your query is:

SELECT A FROM B.

 

Then  when a user of the report click a filter, Power Bi will do something like:

SELECT * FROM ( SELECT A FROM B) WHERE B == 'ABC'

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.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

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.

Top Kudoed Authors