Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
Hi,
I currently have the following (which works):
= Sql.Database("Northwest", "Reporting", [Query="SELECT DISTINCT SUBSTRING([IssueKey],1,(CHARINDEX('-', [IssueKey])-1) ) AS TeamProject#(lf)FROM [Reporting].[dbo].[SprintIssuesSummaryEOS] WHERE Sprint IN#(lf)(SELECT Sprint#(lf)FROM dbo.SprintDates#(lf)WHERE SprintStartDate <= CAST(GETDATE() AS DATE) and SprintEndDate >= CAST(GETDATE() AS DATE))"])
Now I need to modify it to not look at the current date, but a date in a table within Powerquery:
The table is as follows:
TableName: CurrentSprintInfo
Column: PriorSprintStartDate
Column: PriorSprintEndDate
So now I want something like:
= Sql.Database("Northwest", "Reporting", [Query="SELECT DISTINCT SUBSTRING([IssueKey],1,(CHARINDEX('-', [IssueKey])-1) ) AS TeamProject#(lf)FROM [Reporting].[dbo].[SprintIssuesSummaryEOS] WHERE Sprint IN#(lf)(SELECT Sprint#(lf)FROM dbo.SprintDates#(lf)WHERE SprintStartDate = CurrentSprintInfo.PriorSprintDate and SprintEndDate = CurrentSprintInfo.PriorSprintEndDate)
I don't think it is the correct syntax though.
Check your query syntax. In the below setup, probably similar to yours, I have Query1, which is simple text that will get turned into a SQL SELECT statement. It concatenates Parameter1, which has a Current Value of FooBar. The evaluation of the concatenation is displayed for Query1, and just by looking at it I can see that, as is, it would fail if I tried to execute it against a SQL database. Why? Because the Parameter value is of type string and NOT enclosed in single quotes.
For you, copy the contents of the Query text and paste it into something to execute against the database. That may give you a clue.
Proud to be a Super User! | |
I have Parameter1 as :
CurrentSprintInfo[PriorSprintStartDate]{0}
but now an error "Expression.SyntaxError: Token ',' expected."
I have the end as :
WHERE SprintStartDate = " & #"Parameter1")]
Giving me an error "Expressions.Syntax.Error: Token ']' expected.
I fixed one thing, but with this:
= Sql.Database("Northwest", "Reporting", [Query="SELECT DISTINCT SUBSTRING([IssueKey],1,(CHARINDEX('-', [IssueKey])-1) ) AS TeamProject#(lf)FROM [Reporting].[dbo].[SprintIssuesSummaryEOS] WHERE Sprint IN#(lf)(SELECT Sprint#(lf)FROM dbo.SprintDates#(lf) WHERE SprintStartDate = " & CurrentSprintInfo[PriorSprintStartDate]{0} & " and SprintEndDate = " & CurrentSprintInfo[PriorSprintEndDate]{0} & ")"])
I am getting the following error(note: CurrentSprintInfo[PriorSprintStartDate]{ and CurrentSprintInfo[PriorSprintEndDate] I converted the type to date) The SprintStartDate is in a sql table defined as datetime:
There is only ever one row in the CurrentSprintInfo table.
So, you should do the following:
= Sql.Database("Northwest", "Reporting", [Query="SELECT DISTINCT SUBSTRING([IssueKey],1,(CHARINDEX('-', [IssueKey])-1) ) AS TeamProject#(lf)FROM [Reporting].[dbo].[SprintIssuesSummaryEOS] WHERE Sprint IN#(lf)(SELECT Sprint#(lf)FROM dbo.SprintDates#(lf)WHERE SprintStartDate = " & CurrentSprintInfo[PriorSprintDate]{0} & " and SprintEndDate = " & CurrentSprintInfo[PriorSprintEndDate]{0} & ")"
If all the naming I used is correc,t this should work right away.
You are asking Power Query to take values from tables it has at its disposal and inject some values into a SQL Query? doesn't work like that.
One option: Separate the SQL Query text from the query itself. Do this by starting a new Blank Query and manually editing it like this:
= "SELECT ... FROM
WHERE..."
Then in your data query, edit the Source line to be
= Sql.Database("Northwest", "Reporting", [Query=#MySQLtext])
Now you can 'break into' the SQL Query text and concatenate in things like date parameters. I have not done it with values from a table, but I suppose it is possible.
Proud to be a Super User! | |
So what your saying is to build the #MySQLtext which would represent the "Where" clause with my values ?
You should do something like this:
= Sql.Database("Northwest", "Reporting", [Query="SELECT DISTINCT SUBSTRING([IssueKey],1,(CHARINDEX('-', [IssueKey])-1) ) AS TeamProject#(lf)FROM [Reporting].[dbo].[SprintIssuesSummaryEOS] WHERE Sprint IN#(lf)(SELECT Sprint#(lf)FROM dbo.SprintDates#(lf)WHERE SprintStartDate = " & CurrentSprintInfo[PriorSprintDate] & " and SprintEndDate = " & CurrentSprintInfo[PriorSprintEndDate] & ")"
So, idea is to have a concatenated string here, where you will pass your values. Instead of "." notation you use square brackets to provide the column names. But now the question is what are the values that you want to pass there? Are these a single values only, or you want to apply it for the entire table, row by row? If the second option is valid here, you should ditch the table name, and reference columns only. If the first option is your scenario, then you must additionally drill down to single value within column structure like this: CurrentSprintInfo[PriorSprintDate]{0}.
User | Count |
---|---|
9 | |
8 | |
6 | |
6 | |
6 |