I have a few worksheets that use sql queries to retrieve data. I would like all of these sheets to query data based on the same date range entered by a user in excel.
eg. I would enter somewhere in a form or in a cell a date range and retrieve the value in sql to obtain data based on the user specified dates.
Is there anyway to access values from forms or from cells in SQL (Query editor perhaps)?
In MS Query (which you use to build a QueryTable) set the tick to display the Criteria grid, and for required fields enter something in brackets as a criterion value:
A screenshot http://img228.imageshack.us/img228/4593/97662427.gif
This will create two parameters in the query.
Save and return to Excel, right-click the QueryTable, click Parameters... and point them to the right cells.
Related
I am very new to SQL, I want to import data from SQL Server to Excel using this query
SELECT
Model, Factory, TargetTime, TotalEvalMins
FROM
AMSView
WHERE
WeekNumber = 45 AND WeekYear = 2021
I want to change the week number & year dynamically by taking user input from a cell.
Can anyone please suggest how to change the query?
Let's say the user values week & year in worksheet sample in A1,A2 , how can I write that query?
Since the amount of data is huge I must apply where while querying the data instead of applying filters in Excel.
Sorry for my bad English
Name each of your cells that you will use as parameters. This page describes the process.
Name a cell
1. Select a cell.
2. In the Name Box, type a name.
3. Press Enter.
For each cell containing a parameter for your query:
Select the cell
Use Data>Get & Transform Data>From Table/Range. This will open the PowerQuery Editor. You will see something like this:
Right-click the cell in row 1 in the grid in the Power Query Editor and select 'Drill Down'. This converts the query on your parameter cell to a named value which can be used in other queries. It looks like this:
Now in Excel, use Data>Get Data and create your query from the database. I created a sample table in a local SQL Server database called AMSView, then connected to it with the query text in your post. When finishing the query connection, select 'Transform' so the query opens in the PowerQuery Editor.
Now, use Home>Advanced Editor and edit as follows by replacing the fixed values in the WHERE clause with concatenated names of your parameter cells, converted to text. For brevity, I have only used one parameter. If you've used capital letters in your cell names, remember, the M language is case-sensitive, so the concatenated parameter name must have identical casing to the named value.
let
Source = Sql.Database("localhost", "StackOverflowTest", [Query="SELECT #(lf) Model, Factory, TargetTime, TotalEvalMins #(lf)FROM #(lf) AMSView #(lf)WHERE #(lf) WeekNumber = " & Number.ToText(week_number)])
in
Source
Once your query is finished, use Home>Close & Load to load the results to the workbook. Now, when your parameter cells change, you need only refresh the query (right-click, refresh) and the data will be filtered as required.
How do I connect the Excel cell with the date to the SQL Query? I use Power Query. The Database is SQL Server.Please Help me.
Example of Query:
Select Account, Date
From Accountdate
Where Date = "Value in Excel cell"
Type a value into an Excel cell
Keep that cell selected. From the Power Query ribbon choose From Table.
Uncheck My Table Has Headers before clicking OK.
In the Power Query Query Editor, right click on that single value which you entered before and choose Drill Down
Click Apply & Close
In the Power Query, choose From SQL
Connect to your data source (don't worry about using the parameter yet.)
Once you're in the Query Editor with the correct SQL table being shown, choose the column you want to filter by your parameter. Go ahead and do a filter using the filter dropdown. Now change the formula bar and replace the number that you filtered by with the name of the query that you created in Step 5. That query by default will be called Table1 so your query in this step might look something like this: = Table.SelectRows(dbo_MySqlTable, each [ID] > Table1)
You will probably get a prompt asking you to classify the permissions to apply to each data source to make sure there are no security leaks. Once you've done that, click OK.
Click Apply & Close.
I have a stored proc in my SQL DB which I would like to query for data via Power Query in excel. There are three parameters for this procedure for filtering data, and I would like to pass these via Power Query too. The specific parameters should come from some form controls present in my excel sheet - namely some dropdown comboboxes. I'd like to, within the Power Query M, extract the current value of the combobox and pass it as a parameter to the stored procedure.
I know it's possible to gather cell and row/column data within M, but I haven't been able to find out how to collect form data.
Is there an approach I can take for this?
Thanks!
user3261018,
you can try following approach:
Assign the combobox value to a named cell, then address it with
Excel.CurrentWorkbook(){[Name="NameOfCellWithComboboxValue"]}[Content]
This is a problem that has stumped me for a couple of days, and one I've been trying to work on. Unfortunately I very new to working on this stuff, and don't have a lot of resources trying to solve this problem, so if I am unclear or you have a question please post a comment about it.
The problem I am trying to tackle is this:
After getting external data from a SQL Server and having a table in a spreadsheet, say the is called Products, and has columns for productid (column A), productname (column B), supplierid (column C), and categoryname (column D). Is there a way to define a cell, say E1 where you use can use VBA to create a dynamic query. In this dynamic query you would enter some value of categoryname where it would query the table, and give you the results?
It is possible to perform a dynamic query without using any VBA.
Base your table on "From Microsoft Query" (under the "Get External Data" group on the Data tab). Build your query using the Query Wizard, selecting the table columns of interest. On the "Query Wizard - Filter Data" tab, where you would normally say a particular column must have a specific value, instead of the specific value, use just a question mark (for any and all columns that need to be used to filter). When you return your data to Excel, you will be prompted to "Enter Parameter Value" for each ? you used. Just click OK at this stage.
When the Excel "Import Data" dialog appears, click "Properties...". On the Definition tab click the button labelled "Parameters...". For each parameter, choose to "Get the value from the following cell" and to "Refresh automatically when cell value changes". Click the red arrow button (to select a cell) and click in the cell where each parameter will get its value from.
It's a good idea to have a label alongside each cell, and maybe special formatting for the data entry cell. The results table can be on the same worksheet as the parameter entry cells, when you enter a value in the entry cell, a new query will be run and the result will be displayed.
Here is a link that might help.
It goes into using VBA to filter.
I found it a while ago when I looked for something similar, hope it helps.
Excel 2007 with Data in SQL 2008 View.
Trying to create a PivotTable from an external datasource (SQL 2008 - View). I would like to create a filter for a date range, since the view contains a lot of records. The problem is I can't figure out how to specify a filter for my view so the filtering takes place in SQL. Ideally user would be prompted somewhere in the spreadsheet for the parameters or a cell in excel would specify the dates.
Just add parameters on Excel side as you would do with a QueryTable. Those are processed on the server.
Go to MS Query and just put a criterion in Criteria grid. The only problem is, since that is a PivotTable, you can't make those parameters variable, you'll have to put costant dates in the query. In you don't like that, you can make a QueryTable on a separate sheet, add variables to it, make those point to input cells and then build the pivot using result range of the query as a data source.