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.
How can I use parameters to create a dynamic SQL statement? I specified two parameters "DateStart" and "DateEnd" which I want to include in my Data source's SQL statement, but I don't know what the proper way to reference them is. My aim is to have the users be able to adjust the parameter dates at the opening of the report.
Solved! Go to Solution.
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 Source
Oddly 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!
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
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 .
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
106 | |
93 | |
75 | |
62 | |
50 |
User | Count |
---|---|
147 | |
107 | |
105 | |
87 | |
61 |