1

I have a SQL query to get the data into Power BI. For example:

select a,b,c,d from table1 
where a in ('1111','2222','3333' etc.)

However, the list of variables ('1111','2222','3333' etc.) will change every day so I would like the SQL statement to be updated before refreshing the data. Is this possible?

Ideally, I would like to keep a spreadsheet with a list of a values (in this example) so before refresh, it will feed those parameters into this script. Another problem I have is the list will have a different nr of parameters so the last variable needs to be without a comma.

Another option I was considering is to run the script without the where a in ('1111','2222','3333' etc.) and then load the spreadsheet with a list of those a's and filter the report down based on that list however this will be a lot of data to import into Power BI.

It's my first post ever, although I was sourcing help from Stackoverflow for years, so hopefully, it's all clear.

vestland
  • 55,229
  • 37
  • 187
  • 305
Dyniel
  • 11
  • 2
  • I haven't used Power BI for a while. When you say `"However, the list of variables ('1111','2222','3333' etc.)..."`, do you mean a `List` as in Power Query/M? If so, you can just use `Text.Combine(someList, ",")`, which will effectively turn `{"1","2","3"}` to `"1,2,3"`. If you needed `'` before and after each list item, you could try something like `List.Transform(someList, each "'" & Text.From(_) & "'")`, then call `Text.Combine`. All of this will give you a concatenated string, which you can then put into your SQL query. – chillin Jan 04 '19 at 22:45

1 Answers1

-1

I would create a new Query to read the "a values" from your spreadsheet. I would set the Load To / Import Data option to Only Create Connection (to avoid duplicating the data).

Then in your SQL query I would remove the where clause. With that gone you actually don't need to write custom SQL at all - just select the table/view from the Navigation UI.

Then from the the "table1" query I would add a Merge Queries step, connecting to the "a values" Query on the "a" column, using the Join Type: Inner. The resulting rows will be only those with a matching "a" column value (similar to your current SQL where clause).

Power Query wont be able to send this to your SQL Server as a single query, so it will first select all the rows from table1. But it is still fairly quick and efficient.

Mike Honey
  • 14,523
  • 1
  • 24
  • 40
  • Thank you Mike. I did this in slightly different way. Load SQL without the where clause. Then load spreadsheet with the list and make relationship. And finally unfiltered blanks on the report level (to remove the not matching rows). Works fine and no need to tweak anything. Next time I run this report just click refresh and it will load the new data, spreadsheet and perform the same way. – Dyniel Jan 11 '19 at 14:19