Forum Discussion
How do I pass parameters to my SQL statement?
- 9 years ago
I got it to work. Here is my code ( DateBegin & DateEnd parameters have been set to 'text'):
let StartDate=DateBegin, EndDate=DateEnd, Source = Sql.Database("this-is-the-database-url.com", "TableNameHere", [Query="SELECT * FROM [TableNameHere].[dbo].[Logs] WHERE CreatedTime >='" &StartDate& "' AND CreatedTime <='" &EndDate& "' ", CommandTimeout=#duration(0, 0, 10, 0), HierarchicalNavigation=true, MultiSubnetFailover=true]) in SourceOddly enough, I got a bunch of error messages when exiting the 'Query Editor' (as show in my previous post) but when I created everything again from scratch, it works perfectly.
I then saved the Power BI document as a template. When you open the template, you get prompted to enter the "DateBegin" and "DateEnd" parameters. Enter the parameters and the SQL query gets made, voila! :cathappy:
I think you're under something of a misaprehension about how data is retrieved in Power BI. The user viewing a published report does not have any way to affect the query behind the data in that report. That query simply is what it is, and it is either refreshed on a schedule in Import mode, or it is refreshed on demand if you're in DirectQuery. In any case there's no way to give the end user an input on the report that would affect the query. The dataset merely contains whatever the query has already returned. The user can be given slicers and other filtering tools on a report that will filter the results on the page, but that filtering happens within the data already in the report after the queries have completed.
I've seen tutorials where the Power BI report is saved as a template and upon opening the template, the report asks to choose from a list of parameters (country for example). Are you telling me there is no way to implement SQL date queries in this manner? I'm currently working with a massive SQL table which has several gigabytes of data. Naturally, I can't download the entire thing and do the filtering within the view. I really need a way for the user to be able to define the SQL query one way or another.
- SAG19 years agoHelper I
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 .
- Anonymous7 years agoNot applicable
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!
- JasonRoy7 years agoFrequent Visitor
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
SourceParameterized Query
let
Source = Sql.Database("MyTestServer", "MyTestDatabase", [Query="SELECT test_key from MyFactTestTable WHERE datekey >= " & StartDate & " AND datekey <= " & CloseDate"])
in
Source