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
Related
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)
)
I have the below set of records in Google Sheets. I would like to filter the rows with specific name and date range. Once I have the filtered data, I would like to fetch the last row's final amount cell data.
Ex: I would like to fetch final amount as 300 if my date(dd/mm/yyyy) range is 01/01/206 to 11/06/2016 and Name selection is 'Sandeep'.
As I have experience SQLite db, I have inserted the same records in DB and got the expected result using the below query.
select Final from MyTable where Date in (select max(Date) from MyTable WHERE Date BETWEEN '01/01/2016' AND '11/06/2016' and name = "Sandeep")
But I am not getting idea how to use multiple select statements in google sheets. It is ok for me to get result using any other way. So please help me to get the result as explained above.
= QUERY (A1:E50,"Select F where A > date '2016-1-1' and A < date '2016-6-11' and B ='Sandeep' order by A desc limit 1")
Use Column IDs A,B,C instead of name, income. Multiple columns can be given in a single Select clause separated by a ,
Dates in where clause should be written in yyyy-mm-dd format only(regardless of the format of dates in actual column)
See if this works
=index(E:E, max(filter(row(A:A), A:A>date(2016, 1, 1), A:A<date(2016, 6, 11), B:B="Sandeep")))
If you want to include start and end date, change > to >= and < to <=.
We have a spreadsheet that gets updated monthly, which queries some data from our server.
The query url looks like this:
http://example.com/?2016-01-31
The returned data is in a json format, like below:
{"CID":"1160","date":"2016-01-31","rate":{"USD":1.22}}
We only need the value of 1.22 from the above and I can get that inserted into the worksheet with no problem.
My questions:
1. How to use a cell value [contain the date] to pass the date parameter [2016-01-31] in the query and displays the result in the cell next to it.
2. There's a long list of dates in a column, can this query be filled down automatically per each date?
3. When I load the query result to the worksheet, it always load in pairs. [taking up two cells, one says "Value", the other contains the value which is "1.22" in my case]. Ideally I would only need "1.22", not the title, can this be removed? [Del won't work, will give you a "Column 1" instead, or you have to hide the entire row which will mess up with the layout].
I know this is a lot to ask but I've tried a lot of search and reading in the last few days and I have to say the M language beats me.
Thanks in advance.
Convert your Web.Contents() request into a function:
let
myFunct = ( param as date ) => let
x = Web.Contents(.... & Date.ToText(date) & ....)
in
x
in
myFunct
Reference your data request function from a new query, include any transformations you need (in this case JSON.Document, table expansions, remove extraneous data. Feel free to delete all the extra data here, including columns that just contain the label 'value'.
(assuming your table of domain values already exists) add a custom column like
=Expand(myFunct( [someparameter] ))
edit: got home and got into my bookmarks. Here is a more detailed reference for what you are looking to do: http://datachix.com/2014/05/22/power-query-functions-some-scenarios/
For a table - Add column where you get data and parse JSON
let
tt=#table(
{"date"},{
{"2017-01-01"},
{"2017-01-02"},
{"2017-01-03"}
}),
add_col = Table.AddColumn(tt, "USD", each Json.Document(Web.Contents("http://example.com/?date="&[date]))[rate][USD])
in
add_col
If you need only one value
Json.Document(Web.Contents("http://example.com/?date="&YOUR_DATE_STRING))[rate][USD]
I am a beginner in cognos 10. I want to know how to get the value of a prompt from a Query1 and use it on Query2. My requirement is I want a prompt to ask the year for which I want the data in Query1 and in Query2 I want the data relevant to the previous year to that entered in the prompt. How to do that?
You can use the same parameter (tied to the prompt) in filters in both queries. If your parameter is Parameter1 and contains a four-digit year, and your data item in your filter is [Year] then your Query1 filter might look like this:
[Year] = ?Parameter1?
Your Query2 filter would be:
[Year] = ?Parameter1? - 1
Depending on your data source you may have to cast the string parameter to an integer before doing the subtraction though most SQL implementations will implicitly convert the string parameter to an integer for you.
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