cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
SAG1 Regular Visitor
Regular Visitor

Re: How do I pass parameters to my SQL statement?

Hello CarlosDash 

 

I am in similar situation like this , i have huge DB that i can not download all together so need user to be able to select parameters and then get data ccordingly , how did you approach it ? Can you please help me here .

brandonmc Frequent Visitor
Frequent Visitor

Re: How do I pass parameters to my SQL statement?

Here's an example I got working using the AdventureWorks DB.


I created a sproc that accepts one int parameter.  I created a parameter in PowerBI of type ANY called IDIN.  

 

Here's my query:

 

= Sql.Database(".\MSSQLSERVER2016", "AdventureWorksDW2014", [Query="Exec dbo.testfrompbi "&IDIN&"", HierarchicalNavigation=true])

 

 

smfaizhaider Frequent Visitor
Frequent Visitor

Re: How do I pass parameters to my SQL statement?

@peterhinton

Please convert data type using Number.ToText(param)

manjirit Regular Visitor
Regular Visitor

Re: How do I pass parameters to my SQL statement?

All the tutorials and posts I saw were about how to pass the query parameter in the 'M' query in Power BI advance editor and nothing about how to actually pass a parameter by editing the souce query SQL statement. 

 

Any idea about that?

 

The main problem with adding the parameter in the Advanced Editor is that, once its edited there, then the setting wheel next the Source of the query disappears and there is no way to see or edit your SQL statement after that. 

dataviznz Regular Visitor
Regular Visitor

Re: How do I pass parameters to my SQL statement?

Any more insights on this? Does Power BI support passing a parameter to the SQL query?

bajaja2 Occasional Visitor
Occasional Visitor

Re: How do I pass parameters to my SQL statement?

Hey did you get any work around for the same?

 

I have also been working on Huge Set of tables and need to filter them based on the bases of date.

 

Please let me know is there a way to implement the same.

 

Appreciate the Help!

 

 

Highlighted
JasonRoy Frequent Visitor
Frequent Visitor

Re: How do I pass parameters to my SQL statement?

Step one is creating your custom SQL statement. I like to add my placeholder for a parameter when I create the SQL statements and replace those with the parameter once defined. The parameters need to be concatenated to the query using & outside of the quotes holding the query. If you created a table with a single SQL query, your M may look like the following.

 

Non-Parameterized Query

let
Source = Sql.Database("MyTestServer", "MyTestDatabase", [Query="SELECT test_key from MyFactTestTable WHERE datekey >= 20190101 AND datekey <= 20190201"])
in
Source

 

Parameterized Query

let
Source = Sql.Database("MyTestServer", "MyTestDatabase", [Query="SELECT test_key from MyFactTestTable WHERE datekey >= " & StartDate & " AND datekey <= " & CloseDate"])
in
Source

Helpful resources

Announcements
Ask Amir Anything

Exclusive LIVE Community Event No. 2 – Ask Amir Anything

Next in our Triple A series: Ask Amir Netz questions about the latest updates, features and future.

October 2019 Community Highlights

October 2019 Community Highlights

October was a busy month in the community. Read the recap article to learn about some of the events and content.

New Solution Badges

New Solution Badges

Two waves of brand new solution badges are coming! Read the article for more information on our new community badges.

Analytics in Azure virtual event

Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI.

Users Online
Currently online: 45 members 797 guests
Please welcome our newest community members: