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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Tracy000
Helper I
Helper I

How to pass a value to SQL DB DirectQuery where condition

Hi team,

 

I have a PBI file with many tables there, all of these tables come from SQL Server (DirectQuery).

I wrote SQL statements in the Advanced options part, like following:

select A, B, C, D, E, sum(sales_amt) as sales
FROM Table
where month in (concat(left(convert(varchar, dateadd(MONTH, -2, GETDATE()), 102), 4), RIGHT(substring(convert(varchar, dateadd(MONTH, -2, GETDATE()), 102), 1, 7),2))
,concat(left(convert(varchar, dateadd(MONTH, -1, GETDATE()), 102), 4), RIGHT(substring(convert(varchar, dateadd(MONTH, -1, GETDATE()), 102), 1, 7),2)))

 

I fetch the months 202409, 202408 data from Table.

But sometimes we want to change the month, not fetch these 2 months, so that means I have to manually change the code for each table, we have around 20 tables.

 

I want to pass parameter/value to this SQL code, so I only change in parameter/value, then the code in each table could be changed accordingly.

 

I created the values as following, now I want to pass these values to the SQL statement. Is it possible?
If yes, can you pls. advise how to write in where condition?

Thanks a lot!

Tracy000_1-1729676272112.png

 

 

1 ACCEPTED SOLUTION
SamWiseOwl
Super User
Super User

Hi @Tracy000 

You can right click on your query and change it into a parameter.

SamWiseOwl_0-1729679342660.png

 

Then connect to SQL put your code under the Advanced section.

SamWiseOwl_2-1729679424331.png

 

 

After your code has connected, on the right hand side under Applied steps click on Source (not the cog).

This will show your code in the M editor above your table.

SamWiseOwl_3-1729679455260.png

 

Go to where you want to pass in the parameter and split the SQL script using '" & yourparameter & "' that will insert your parameters value into the code 🙂

SamWiseOwl_4-1729679487375.png

 


If you are happy with this answer please mark as a solution for others to find !

Kudos are always appreciated! Check out our free Power BI video courses.

View solution in original post

2 REPLIES 2
SamWiseOwl
Super User
Super User

Hi @Tracy000 

You can right click on your query and change it into a parameter.

SamWiseOwl_0-1729679342660.png

 

Then connect to SQL put your code under the Advanced section.

SamWiseOwl_2-1729679424331.png

 

 

After your code has connected, on the right hand side under Applied steps click on Source (not the cog).

This will show your code in the M editor above your table.

SamWiseOwl_3-1729679455260.png

 

Go to where you want to pass in the parameter and split the SQL script using '" & yourparameter & "' that will insert your parameters value into the code 🙂

SamWiseOwl_4-1729679487375.png

 


If you are happy with this answer please mark as a solution for others to find !

Kudos are always appreciated! Check out our free Power BI video courses.

Hi @SamWiseOwl ,

Many thanks for your reply and suggestion.

I used this format '" & yourparameter & "' in M editor without converting query to parameter, then it works.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.