0
votes

Im trying to use MS Excel Power Query to get value from SQL DB based on item in each row.

My Excel Table has the following in A1, B1:

Date: =TODAY()

A2, B2 has the following headers and A3 has the fruits list. C2 - F2 contains other information. Hence, the val needs to be populated in col. B

Fruit, Val
Apple
Orange
Banana

The SQL query looks like below:

select val
from MY_TABLE
WHERE fruit = ?
AND date = ?

The ? is the parameter and it links to cell $B$1 (date), $A3 (the first item in the fruit list)

I am using the ODBC data connection where I input my query and insert the final parameter as ?

Then from editing the connections > properties, I change the parameters under the 'Definition' tab, selecting the appropriate cells.

But when I drag this to the next cell, it doesn't update. I tried changing $A3 to $A4, but once again the value is returned in cell B3 only.

Any idea how I can update this for each row?

I know I could use the MS SQL data connection where I can use a query like

SELECT val
FROM MY_TABLE
WHERE fruit IN (
'Apple',
'Orange',
'Banana'
)

But the excel sheet is used by many people and hence, the fruits list is updated at regular intervals. So using a static query is not ideal.

What im trying to achieve is that whenever the fruits list gets updated, the user can choose to flash fill to the next cell, which will update the Col B, by referencing the equivalent cell A.

1

1 Answers

1
votes

I was not able to reproduce the exact problem you are facing with creating dynamic SQL query parameters in this way (for some reason the Parameters button under the Definition tab is greyed out, I am using Excel for Microsoft 365 on Windows 10). Anyway, if you were to succeed in doing this, wouldn't you end up with a unique query for each cell? I would imagine that would hurt performance when clicking on Data > Refresh All.

In any case, I believe one of the reasons for using Power Query is to have it write SQL queries for you: Power Query Editor > Query Settings pane on the right > Applied Steps, right-click on the last one and click on View Native Query to see the SQL query being sent to the server. As you further process the data, this underlying SQL query will be automatically edited depending on the statements supported by query folding. Of course, the connector needs to support this, so I suggest using the MS SQL Server connector. Note that sometimes the View Native Query option is greyed out but query folding is still taking place, the only way to know for sure is by using a profiling tool on the database.

Here is a way to use Power Query so that the whole Val column gets updated in a single data refresh.

Click on cell B1 and name it cellDate by using in the name box left of the formula bar, then right-click on cell B1 > Get Data from Table/Range... to open the Power Query Editor.

cellDate

Replace the content of the Power Query Editor formula bar with this:

= Date.From(Excel.CurrentWorkbook(){[Name="cellDate"]}[Content][Column1]{0})

You now have a query that returns the date from cell B1. Now click on the query that contains the table you are importing from the database (named Fruits in this example). Filter the Date column using the drop-down list and select any random date.

filter_date

In the formula bar, replace #date(2021, 9, 10) with cellDate. Now every time you change the date in cell B1 and refresh the data, this filter will be updated. If you are ignoring Privacy Level settings or using a Public Privacy Level for your workbook, this filter step should be folded to the data source.

Close and load these queries as connections only.

Select the range of cells containing the fruit names, create a Table, name it listFruits and right-click > Get Data from Table/Range... to open the Power Query Editor.

listFruits

In the Query List on the left, right-click on listFruits > Duplicate. Rename it as listFruitsValues. On the Home tab > Merge Queries. Select Fruits as the second table and click on the Fruit column in each table. Select as Join Kind: Left Outer (all from first, matching from second), then click on OK. Note that from this step onwards, the query is not folded back to the data source.

merge

Click on the expand button of the Fruits column, select only the Val column, uncheck Use original column name as prefix, then OK. Remove the Fruit column.

This is what the Power Query Editor window should look like at this stage.

PQ_editor

Now you can load the listFruitsValues query in the worksheet next to the Fruit table. Here is what is that looks like with the default table formatting.

table_loaded

Now if any edit is made to the date and/or the list of fruits, clicking on Data > Refresh All will update the Val column accordingly.


On a final note, I would suggest considering a different approach if the source table filtered for the date (i.e. Fruits in this example) is not too large. The issue with the approach presented above is that the users need to click on the Refresh All button after every edit of the fruit list. This can be avoided by simply loading the Fruits query in a separate worksheet and using the following formula to populate the Val column:

=XLOOKUP(A4,Fruits[Fruit],Fruits[Val])

By creating a single Table with the Fruit and Val columns, the values are instantaneously updated when changes are made to the list of fruits and the Fruits query only needs to be refreshed when the date is changed.