Adapt SQL Query with an Excel valua - excel

I'm using Excel 2013 to get data from a SQL Server 2012 database,
I successfully get data using an SQL Query, but now I want that query to be dynamic, based on the Month and Year.
How is it possible to use a cell value (a spreadsheet cell) as a parameter for such a query?
I use this Query:
SELECT [Class_02] AS [MS Part Number]
,[701].[dbo].[ItemClasses].[Description] AS [Item Name]
,ROUND([701].[dbo].[frhsrg].[esr_aantal]/3, 0) AS [Aantal]
,[701].[dbo].[ItemAccounts].[SlsPkgsPerPurPkg] AS [Prijs]
,SUM(DISTINCT [701].[dbo].[frhsrg].[esr_aantal]) * [701].[dbo].[ItemAccounts].[SlsPkgsPerPurPkg] as Totaalprijs
FROM [701].[dbo].[Items]
WHERE DATEPART(YEAR, fakdat) = '2013'
AND DATEPART(QUARTER, fakdat) = '1'
AND [701].[dbo].[frhsrg].[docnumber] LIKE '%kwartaal%'
I have two values in Excel (Month and Year) like March 2013, and then the DATEPART statement in SQL has to be based on that. I use the Excel 2013 Add-In PowerPivot to fill the Excel sheet with the SQL Query output. How does the SQL Query need to be to do this?
In forward, many thanks!

... I you have the query saved as a string in your Excel VBA macro (I'm assuming that is where this query is run from), simply write it as follows:
WHERE DATEPART(YEAR, fakdat) = <<YEAR>>
AND DATEPART(QUARTER, fakdat) = <<MONTH>>
Basically having <<YEAR>> and <<MONTH>> as placeholders, then you can just use the Replace function looking something like this:
Replace strSQL, "<<YEAR>>", Range("A1").Text
where strSQL would be the SQL string

Customize a parameter query - http://office.microsoft.com/en-us/excel-help/customize-a-parameter-query-HP010216113.aspx

Related

SQLite ODBC Query Output in Microsoft Query

I have connected a SQLite database to an Excel worksheet via Microsoft Query and an ODBC connection (using the driver at this link). In this SQLite database, I have several views that do various calculations and I need them rendered in Excel. All the data is coming through as expected in SQLite (example data below). The problem I am having is that Excel (the table object specifically) is only identifying the correct numeric data type for the [Diff] column when the first value in the column is NOT NULL. This is causing some headaches on the Excel side... I need to manually convert the [Diff] column to numeric. But if the first value is numeric (View2), this is done automatically by Excel.
Is there any workaround for this? I think this might be an Excel issue... or maybe this has to do with SQLite decltypes? Any help would be appreciated. Thanks
CREATE Table DateTest(
"A" DATE,
"B" DATE
);
INSERT INTO DateTest VALUES (NULL, NULL), ('2022-01-01', '2022-02-02'), (NULL, NULL), ('2022-02-01', '2022-03-01')
;
CREATE VIEW View1 AS
SELECT
[A]
, [B]
, julianday([B]) - julianday([A]) AS [Diff]
FROM DateTest
;
CREATE VIEW View2 AS
SELECT
[A]
, [B]
, julianday([B]) - julianday([A]) AS [Diff]
FROM DateTest
ORDER BY julianday([B]) - julianday([A]) DESC
;

Excel 2016 query editor, filter/where clause

I'm struggling with probably the simplest thing. How do I filter my data using a where clause in Excel 2016, ODBC sql database source.
I'm new to Excel 2016, I used to just modify the SQL Command via the odbc properties. Now i can't even see the SQL command when I create the odbc.
So from just playing around. I gathered that there is now a series of "Applied Steps", but I struggle to find where I can find how to limit the rows to a date range. (I don't want to display the date because I'm aggregating by inventory type) But there must be somewhere I can say return only for date '2020-05-01' for example. (where date = '2020-05-01')
the next steps will then be making that a dynamic parameter so I can have some sort of input when the data refresh.
Any tips?
The SQL statement is greyed out
Right clicking a step -> view native SQL will gray out when that step cannot be query folded. Previous steps are still viewable.
You can find the sql query and server name if you choose advanced editor -- which displays all steps in one editor.
native sql
If you want raw sql, import the table as a new connection and paste your query into that box.
note: [] in powerquery is a Record literal.
// indented for readability
let
Source = Sql.Database("server", "dbo"),
// using parameterized queries
RawSQL = "SELECT * FROM users as
WHERE EnglishMonthName=#MonthName
AND
EnglishDayNameOfWeek=#DayName",
TableName = Value.NativeQuery(
Source, RawSQL
[
MonthName="March",
DayName="Tuesday"
]
)
in
TableName
I don't want to display the date because I'm aggregating by inventory type
Using the UI, you would import the table keeping the date column. Filter rows. Then choose remove column as your last step. Your model will not display removed columns.
Where clause in PowerQuery aka: Get Data
Where is the function Table.SelectRows The UI will generate the code for you
Right click your date column -> filter -> date/time -> equals
If you want 2020/05/09 It generates:
= Table.SelectRows(#"Source Table", each [Date] = #date(2020, 5, 9))
You may use multiple conditions, like a date range
= Table.SelectRows(#"Table Name", each
[Date] >= #date(2020, 5, 9) and
[Date] <= #date(2020, 6, 1)
)

How to refer to other queries through "m" formulas or power query?

The following line of code will create a variable of the type table with the content from a table in the spreadsheet.
Instead of referring to a table in excel spreadsheets, I want to refer to another query. — How do I do that?
mytable = Excel.CurrentWorkbook(){[Name="table_in_excel_spreadsheet"]}[Content]
If your query name is a text variable, use Record.Field(#shared, YourQuery)
If you're query is called Query1, it's:
mytable = Query1
For queries with more complicated names with spaces and special characters, like My Query, it's:
mytable = #"My Query"

Excel: Query SQL table with date parameter from cell

I have an ODBC connection, which simply queries a SQL table:
SELECT * FROM [TABLE] WHERE myDate = ?
If I run the query in MS Query, it prompts for a date and I can just enter 4/2/2015 and it returns 4/2/2015 data perfectly
I have the parameter set to read from cell (B1):
=WorkSheetName!$B$1
When I switch back to Excel and put 4/2/2015 in B1 and then refresh - it gives me a conversion failed when converting date and/or time from character string error.
I tried editing my query to WHERE CONVERT(Varchar(10),myDate,101) = ? but had no luck. Not sure why I am getting this, seems like it should be so simple.
I appreciate the feedback I was getting - but it turned out to be something very simple on my part that I was overlooking. The actual cell I was keeping my date was formatted as a date, and giving a conversion error. Once I formatted it to a text cell, it returned the data properly for the given date. Thanks
The filter part must be padded with # while trying to use Date as a filter.
It should be like
SELECT * FROM [TABLE] WHERE myDate = #4/2/2015#
Converting the "myDate" column to "Smalldatetime" format should work for you.
Try this:
SELECT * FROM [TABLE] WHERE Cast(myDate as smalldatetime) = ?
Thanks

Breaking an Access/Excel table into multiple Excel sheets based on a field value

I have a table that groups sets of values based on a document they came from. The table is currently in Excel, but could easily be imported into Access if it's easier from there.
doc value1 value2 value3
foo 29037 1098273 1023978
foo 1029387 109178 10918
bar 102893 101982 102907
biz 1023894 1908237 1023894
bar 102734 997124 834347
What is the easiest way to export these values into three different Excel workbooks (foo.xlsx, bar.xlsx, and biz.xlsx), with the column headings and values in the original table?
Thanks in advance for the help.
You can use ADO with Excel ( http://support.microsoft.com/kb/257819 ), but this would be very simple indeed with Access, DoCmd.TransferSpreadsheet and a query.
SELECT * FROM MyTable WHERE Doc='Foo'

Resources