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 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:
Thank you so much !
I have been racking my brain half the day to make my StartDate and EndDate custom functions readable directly in the Source Query.
When I use a SQL statement directly in the Database lookup, I can write BETWEEN '2022-12-01' AND '2022-12-31', and it works perfectly! But this has to be done in the database connection directly.
Once i tried editting it directly in the Source Query in PowerQuery - nothing seemed to work. But deleting BETWEEN and rewriting to PowerQuery logic like yours using WITH AND instead - worked just fine now!
Thank you 😄