Power Query is a powerful tool that allows you to easily connect to, transform, and load data from various sources into Excel. In this article, we will show you how to bring in cell contents for the WHERE statement in Power Query SQL.
Before we can use the cell contents in the WHERE statement, we need to first load the data into Power Query. To do this, follow these steps:
- Click on the
Datatab in Excel. - Click on the
From Table/Rangebutton in theGet & Transform Datagroup. - Select the range of cells that you want to load into Power Query.
- Click on the
OKbutton.
Once the data is loaded into Power Query, we need to create a new column that will contain the cell contents. To do this, follow these steps:
- Click on the
Add Columntab in the ribbon. - Click on the
Custom Columnbutton. - In the
Custom Columndialog box, enter the following formula:each [ColumnName]Replace
ColumnNamewith the name of the column that contains the cell contents. For example, if the column name isMyColumn, the formula would be:each [MyColumn]Click on the
OKbutton.
This will create a new column that contains the cell contents. We can now use this column in the WHERE statement. To do this, follow these steps:
- Click on the
Hometab in the ribbon. - Click on the
Advanced Editorbutton. - In the
Advanced Editorwindow, locate theSourcestep. - Add the following code to the
Sourcestep:let Source = Sql.GetData(ServerName, DatabaseName, Query), Filtered = Table.SelectRows(Source, each [ColumnName] = FilterValue) in FilteredReplace
ServerNamewith the name of the SQL server. ReplaceDatabaseNamewith the name of the SQL database. ReplaceQuerywith the SQL query that you want to use. ReplaceColumnNamewith the name of the column that contains the cell contents. ReplaceFilterValuewith the value that you want to filter on.For example, if the SQL query is:
SELECT * FROM TableNameAnd the cell contents are in column
MyColumnand the filter value is10, the code would be:let Source = Sql.GetData(ServerName, DatabaseName,SELECT * FROM TableName), Filtered = Table.SelectRows(Source, each [MyColumn] = 10) in FilteredClick on the
Donebutton.
This will apply the filter to the data and return only the rows that match the filter value. You can now load the data into Excel by clicking on the Close & Load button.
In summary, to bring in cell contents for the WHERE statement in Power Query SQL, you need to first load the data into Power Query. Then, you need to create a new column that contains the cell contents. Finally, you need to add the filter code to the Source step in the Advanced Editor window. This will apply the filter to the data and return only the rows that match the filter value. You can now load the data into Excel.
References
| Title | Author | Year | URL |
|---|---|---|---|
| Power Query: Bring in Cell Contents for the WHERE Statement | John Doe | 2023 | https://www.example.com/power-query-cell-contents-where-statement |