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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

Dynamic parameter for SQL to filter last year

Hi

 

I need to pass a dynamic parameter to an SQL query, so I can get the last year, but filtered in the SQL code.

Actually, I'm using a fixed parameter, with today's day and month.

But how can I calculate the parameter instead of having a fixed value?

I've seen some post where they calculate the field and then use reduce rows, but I need to use it in the SQL code.

 

Thanks!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Finally, I've solved it in this way:

 

let

//First, create the variable in PowerQuery
Startdate=Text.From(Date.Year(Date.AddMonths(DateTime.LocalNow(),-12))),
StartMonth=Text.PadStart(Text.From(Date.Month(Date.AddMonths(DateTime.LocalNow(),-12))),2,"0"),

YearMonth=Startdate & StartMonth,

//Then use the variable in the SQL

Source = Sql.Database("ServerName", "DatabaseName", [Query="
Select *
from TABLE_NAME
Where YEAR_MONTH='"&YearMonth&"'
"])


in
Source

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Finally, I've solved it in this way:

 

let

//First, create the variable in PowerQuery
Startdate=Text.From(Date.Year(Date.AddMonths(DateTime.LocalNow(),-12))),
StartMonth=Text.PadStart(Text.From(Date.Month(Date.AddMonths(DateTime.LocalNow(),-12))),2,"0"),

YearMonth=Startdate & StartMonth,

//Then use the variable in the SQL

Source = Sql.Database("ServerName", "DatabaseName", [Query="
Select *
from TABLE_NAME
Where YEAR_MONTH='"&YearMonth&"'
"])


in
Source

@Anonymous by doing it that way you have created a SQL statement that will prevent further folding. 

 

If you create a blank query and then put your yearmonth logic in it to return an integer (202001 for example), then refer to it in a query as I displayed, additional steps in your query will continue to fold.

 

By not allowing folding to continue, it will pull in EVERY column for that table and 100% of any further transformations will be done on the client or the gateway.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Greg_Deckler
Community Champion
Community Champion

In theory you could use a query parameter and have it be the SQL code to evaluate. Then in your SQL query/stored procedure/etc. just wrap an EVALUATE around that parameter coming in?



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
edhans
Super User
Super User

Just create a filter like you normally would, then replace the created filter string with the name of your parameter. For example:

 

= Table.SelectRows(Sales_Customer, each ([TerritoryID] = 10))

 

becomes

 

= Table.SelectRows(Sales_Customer, each ([TerritoryID] = varTerritory))

 

where my varTerritory variable (parameter) is dynamically calculated. It is just another query really that evaluates to a whole number in this case, but could be a date, text, etc.

 

And note that this may even fold depending on the rest of your queries. This is what Power BI is sending to my SQL server:

 

select [_].[CustomerID],
    [_].[PersonID],
    [_].[StoreID],
    [_].[TerritoryID],
    [_].[AccountNumber],
    [_].[rowguid],
    [_].[ModifiedDate]
from [Sales].[Customer] as [_]
where [_].[TerritoryID] = 10 and [_].[TerritoryID] is not null

 

Note that it is sending a 10 but if varTerritory changes, Power Query will change the SQL statement next time it executes.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Anonymous
Not applicable

That works when using query folding.. But I'm writing the query directly, so I can't filter in PowerQuery or I will load the whole table.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.