How can I use parameters to create a dynamic SQL statement? I specified two parameters "DateStart" and "DateEnd" which I want to include in my Data source's SQL statement, but I don't know what the proper way to reference them is. My aim is to have the users be able to adjust the parameter dates at the opening of the report.
For Example:
Select projectid,projectname,date,agent,count(*) from production Where
fromdate = '04/01/2020' and to date = '04/05/2020" Group by Projectid,
Projectname,date,agent
Now, dates filters will be added in Excel cells and based on inputs result will appear on the table.
Also, advise will that work in MS Office 2016 & O365 version as well.
Thank you in Advance!
Related
I have a SQL query I am using for a workbook requiring multiple pivot views from the same dataset. Part of the query is looking at a declared value and based on that value it will either return one result or another, that declared value however needs to be a parameter that end users will input into the workbook.
Normally I would use the excel function to pull the data from the SQL server and then pass a parameter in the advanced query option using " Let Parameters = Excel.CurrentWorkbook()".. however the result set it too large and therefore I need to use Power Pivot.
I however for the life of me can find no help on how to pass a parameter from a cell in a worksheet to power pivot - is this possible, is there a VB script I can use to update the query, alternatively what other options are available other than having to get the end users to go into power pivot and edit the query in the properties each time?
As the variable is used in a computation, I am not using the parameter to filter the dataset and that seems to be the only advice I have been able to find. I am new to Power Pivot.
I came right by doing the following:
Add the SQL query in Power Query, as a data connection only. In the advanced query options, I added the required parameters.
I then selected the query and selected the option to load to Data Model.
Seems to be working.
I got an excel file which should use a query from an ms access file (which is a front end for a ms sql server)
But instead of receiving every entry I want to use a filter by a value that is in one of the excel cells.
So if in A1 is date 01.01.2000, I want to use this value to only show me all entries by that date
If do not need to use power query if there is an easier way to do it.
You can pass a value into power query using a table and have your original query reference that cell/table. More detail here: https://exceleratorbi.com.au/pass-excel-parameter-power-query/
check the sample file, fill the parameters as per your DataBase
https://docs.google.com/spreadsheets/d/1nhB710FEl_AfFQy6HwDmR8xhcWWuw91p/edit?usp=sharing&ouid=100599386090580961037&rtpof=true&sd=true
Please note I have found solutions to this in Excel 2007, 2010, 2013 etc but nothing that relates directly to 2016.
I bring in a query to Excel by using Data / Get Data / From Database / From SQL Server Database.
What I want to do is restrict 1 column in the data it retrieves to a specific value.
In my exact example there is a date column and I want a cell in the spreadsheet to pass the date value to filter the returned query data.
For this application, I don't think you need to actually pass the date as a parameter to your query. You can just use the date from the cell as a filter after you've loaded your query into the Query Editor. As a result of query folding, Excel should automatically pass it into the WHERE clause of the native query it sends to the SQL Server Database.
If you don't know how to reference a cell value in the Query Editor, please see this question:
How can I reference a cell's value in a Power Query
So in Excel 2016, they have this neat tool called Power Query, basically a glorified excel table. Every table has steps in it for filtering, removing columns, etc... The first step is the source step, to assign a connection string basically to retrieve data, normally this source just points back to the query which created it.
Anyways, I'm trying in VBA to dynamically change the source of these power queries, anyone have any ideas?
I tried using the whole connections vibe, but was unsuccessful.
You can access the query through ActiveWorkbook.Item. You can then modify the Formula property. You can find the documentation on these objects here.
Please note that the Power Query object model was only added to VBA in Excel 2016 and cannot be accessed in prior versions.
The queries can be accessed via the Queries collection in a Workbook object. The relevant property for the source is Formula.
Example code:
ActiveWorkbook.Queries.Item("MyQuery").Formula = "[Insert actual M formula here]"
Many years later, but I'm adding a different solution for those, like me, still stuck with Excel 2013. As stated by #Alejandro in his response, Power Query was only added to the object model in Excel 2016.
If you are using an older version of Excel, you can use a cell-based solution similar to the one talked about here for relative source paths:
https://techcommunity.microsoft.com/t5/excel/power-query-source-from-relative-paths/m-p/206150
Basically have a cell some where in your workbook that contains your query's source. Name that cell, using standard Excel names. In the example below, I've named the cell SourceFileName, and I'm trying to load an Excel file to PowerQuery. The full name of the source Excel file (including path) is in SourceFileName . You can then access the contents of that cell via Power Query:
<previous M code>
sourceFileName= Excel.CurrentWorkbook(){[Name="SourceFileName"]}[Content]{0}[Column1],
Source = Excel.Workbook(File.Contents(sourceFileName ), null, true),
<rest of M code>
So. I have prepared an MDX query in SQL Server Management Studio (SSMS) which shows me the result that I want. However, I would like to query it from Excel instead of from SSMS, so that I can work with the figures and create graphs and such.
My MDX could look like the following:
SELECT
[Dates].[By Month].[Month].&[2015-02]
ON 0,
(
[Region-Office].[Region Hierarchy].AllMembers *
[Measures].[Earnings]
) ON 1
FROM [Model]
When querying this MDX in SSMS, I get the result that I want: namely that both Region and Office is printed to only one column:
Above MDX query returned from SSMS, note that both region and office is in the same column
When I turn to Excel and create an MSOLAP.6 connection with the following connection string:
Provider=MSOLAP.6;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=CubeData;Data Source=CubeServer;Extended Properties="VisualMode=2";MDX Compatibility=2;Safety Options=2;MDX Missing Member Mode=Error
I get a different result: namely that region is printed to one separate column and office printed to another separate column:
Above MDX query returned to Excel table using MSOLAP.6, note that region and office is printed in two separate columns, which is not what I want
Can I in any way control what the Excel table returns so that I can retrieve the same output as the one I query in SSMS?
Thank you very much in advance!
The connection string you supplied is the same as I have when using excel's out-of-the-box SSAS connection wizard to a pivot table, so I'll assume nothing more complicated than that.
If in a pivot I add a multi-level dimension to rows I get behaviour like the following - for a "classic" pivot:
I can create a custom set in Excel like the following:
That set then becomes available to use and all members are in a single column: