0

I would like to use an Excel cell to change the reference data in a Where statement so that I don't have to keep going into power query to change the statement.

Instead of the 31690 in the below code I would like to reference cell B7 in sheet1 of the same Workbook instead.

Is this possible? and if so how? Thanks in advance.

WHERE ORDERDATE >= @Month13#(lf)#(tab)and STOCKCODE is not null#(lf)#(tab)AND SALESORD_HDR.ACCNO = '31690'
Gass
  • 7,536
  • 3
  • 37
  • 41

2 Answers2

0

Maybe something like this?

For this approach to work, you need to make sure your spreadsheet has a table and the table's range starts with A1 and spans beyond the cell with the value in it--in this case, B7. Here's an example:

I started by creating this spreadsheet with a table named Table1:

enter image description here

Then, I used Table1 as the source in Power Query.

enter image description here

Notice that with the table above, what was row 7 is row 6. This is because the column headers don't have row numbers in Power Query. This change in row numbering matters for finding your targeted cell.

Then I added some custom M code. This code first extracts the second column's name from the list of column names. (Because the second column would be column B of the spreadsheet.) Then it uses that second column's name to create a table of that column's values, from which it then extracts the sixth row entry. (Because that sixth row entry would be the seventh row entry in the spreadsheet.) Note that the {1} points to the second column and the {5} points to the sixth row. That's because Power Query indexing starts at 0.

enter image description here

I went into Advanced Editor and renamed the step from Custom to DateVariable:

DateVariable = Table.Column(Table1_Table, Table.ColumnNames(Table1_Table){1}){5},

Then I added some more custom M code to concatenate the DateVarable with the rest of your SQL statement as an example:

enter image description here

Here's my M code:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    DateVariable = Table.Column(Source, Table.ColumnNames(Source){1}){5},
    SQL_Statement = "WHERE ORDERDATE >= @Month13#(lf)#(tab)and STOCKCODE is not null#(lf)#(tab)AND SALESORD_HDR.ACCNO = '" & Text.From(DateVariable) &"'"
in
    SQL_Statement
Marc Pincince
  • 4,987
  • 6
  • 19
  • 40
0

Simple solution using a named range

First, select cell B7 and enter a name in the Name Box (e.g. CellReference). Then right-click on the cell and click on Get Data from Table/Range.

cell_reference


This opens the Power Query Editor with a query that returns a table containing the cell from the named range. Open the advanced editor, delete the entire content of the query and type Text.From(Excel.CurrentWorkbook(){[Name="CellReference"]}[Content][Column1]{0}) and click on Done, this is what it should look like:

cell_reference_query

Note: Text.From() is used so that value returned by CellReference can be concatenated with the SQL query using &. Also, this function is preferable to Number.ToText() which does not work with text values.

Finally, insert the query name in your SQL query: WHERE ORDERDATE >= @Month13#(lf)#(tab)and STOCKCODE is not null#(lf)#(tab)AND SALESORD_HDR.ACCNO = "&CellReference

Note that if the cell contained a text value instead, then you would need to adjust the syntax like this: ... SALESORD_HDR.ACCNO = '"&CellReference_Text&"'"



How to deal with warnings: new query permission and Formula.Firewall


How to give permission to run all new native database queries

Depending on your Query Options settings, you may get this warning message regarding the permission to run the modified SQL query each time CellReference contains a new value:

permission_warning

If you are certain that the cell will never contain a string of characters that could modify the database, you can disable this warning message by going to File -> Options and settings -> Query Options. Under GLOBAL, go to Security and uncheck Require user approval for new native database queries.

edit_permission

Note that this is a global setting that is immediately applied to all your Excel files, including those that are currently open.


How to disable the Formula.Firewall warning message

Depending on your Privacy Levels settings, you may get a Formula.Firewall warning message preventing the query from being executed:

firewall_warning

If you are in a situation where you can disregard privacy levels, you can disable this message by going to File -> Options and settings -> Query Options. Under CURRENT WORKBOOK, go to Privacy and select Ignore the Privacy Levels and potentially improve performance.

privacy_settings

Click on OK and refresh the query.

If, on the other hand, your workbook needs to preserve a privacy level of Private or Organizational, to my knowledge there is currently no way of integrating CellReference to a SQL query (even using a SQL parameter set with the Value.NativeQuery function or a Power Query Parameter ) without raising this warning message. The only solution would be to include CellReference in another step in the query, but then the filtering will occur in Power Query and not at the server level: query folding is interrupted when a step includes a query/function/parameter that is linked to an external data source including a named range in the workbook itself.

If your workbook privacy level is set to Public, you should be able to avoid this warning message by using the Value.NativeQuery function (you can even enable query folding for further query steps if you are using a SQL Server or PostgreSQL database). If you still get the warning message, you can try combining the two queries accessing each data source (the database and the worksheet) into a single query.



Note: these steps were tested with Excel Microsoft 365 (Version 2107) on Windows 10 64-bit connected to a local SQL Server 2019 (15.x) database.

This answer was prepared by referring to many blog posts by Chris Webb (linked above) and by Ken Puls (like this one).

Patrick FitzGerald
  • 3,280
  • 2
  • 18
  • 30