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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Parameterized SQL Query

Hello, 

 

Is there a way wherein we can have paramterized SQL queries? Baiscally a way to run the SQL query for a specific filter condition rather than the running it for the entire population? 

For example: Let's say I have employees table, instead of importing the entire employees data (SELECT * FROM EMPLOYEES) can I only import the data for a specific employee based on its Employee_ID entered by the user (via slicer or any kind of prompt filter)? So that the SQL would run only for that specific Employee_ID ( SELECT * FROM EMPLOYEES WHERE EMPLOYEE_ID = 'XYZ') and take less time to execute. 


Thanks, 
Jash.

1 ACCEPTED SOLUTION
edhans
Super User
Super User

Yes. Have you seen this article? Your query in Power Query can use this parameter, you just have to ensure that all steps up to and including the point where it filters is "folded" meaning Power Query or the service will generate the SQL statement and just get back the requested data.

 

You can also do this using quries in Power Query that return a single value (text, number, integer, date, etc) and use that query result as a filter. For example, the following query would return the earliest date from a table.

 

let
    Source = #"qryOpen Apply To Nos",
    #"Calculated Minimum" = List.Min(Source, [Date])
in
    #"Calculated Minimum"

 As long as that step folds you could use that query as a filter for your table to only get dates  on or after the date that query calculated. That isn't technically a parameter as defined by Power BI, but it is fully dynamic vs true Parmeters which have to be keyed in in the service.

 

This article has a much fuller description of how I create dynamic date tables based on the earliest and latest dates in my data by using these pseudo-parameters, and in SQL, it is 100% folded the way I do it, so the server does all of the heavy lifting.



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

View solution in original post

1 REPLY 1
edhans
Super User
Super User

Yes. Have you seen this article? Your query in Power Query can use this parameter, you just have to ensure that all steps up to and including the point where it filters is "folded" meaning Power Query or the service will generate the SQL statement and just get back the requested data.

 

You can also do this using quries in Power Query that return a single value (text, number, integer, date, etc) and use that query result as a filter. For example, the following query would return the earliest date from a table.

 

let
    Source = #"qryOpen Apply To Nos",
    #"Calculated Minimum" = List.Min(Source, [Date])
in
    #"Calculated Minimum"

 As long as that step folds you could use that query as a filter for your table to only get dates  on or after the date that query calculated. That isn't technically a parameter as defined by Power BI, but it is fully dynamic vs true Parmeters which have to be keyed in in the service.

 

This article has a much fuller description of how I create dynamic date tables based on the earliest and latest dates in my data by using these pseudo-parameters, and in SQL, it is 100% folded the way I do it, so the server does all of the heavy lifting.



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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors