Pass dates from an Excel cell reference into ODBC SQL PowerQuery - sql

I have several MySQL queries, including some that aggregate but don't necessarily return dates in the results, that have either a date or date range hardcoded into the query. I've loaded these into separate Excel worksheets through ODBC to output the results. Instead of going into the query and editing the source as needed to change the dates, how can I have the 'start' or 'start and end' date(s) of the query be a cell reference on that particular worksheet?
The date part of my SQL predicates look like:
WHERE post.trans_date >= '2020-03-01'
or
WHERE post.trans_date >= '2020-03-01'
AND post.trans_date <= '2020-03-31'
I'd like to just be able to change the dates directly in the cell(s), without VBA, and click Refresh to return the correct results. Thanks!

You can read cell values into PowerQuery and then use those cell values within your query similarly to the example I give in the linked post.

Related

Power BI Query Editor - Filter dates in Dataset based on external input (Excel)

I have a large dataset, with a Date column which goes into past dates and future dates.
I have an Excel table with a specific date (e.g. 01/05/2022). I want to filter out the rows which are in the past vs the input given (01/05/2022) by Excel. In the Power BI Query Editor.
I have only found a way on how to filter by current's date...
Thank you very much,
In Excel, name the input date's cell (Range), such as 'ThruDate' (No spaces, special characters, etc).
From the Data Menu, use 'From Table/Range' to send it into PQ Editor.
In PQ Editor ThruDate query, change Column1 Type to DATE, and Rename the Column 'Date'
In your dataset query, manually filter the date column.
Here's the trick! Edit the code that that step created in the Formula Bar or Advanced Editor.
Replace #date(2022, 1, 5) with ThruDate[Date]{0}
Now, each time you change the Input Date in Range("ThruDate") and Refresh, you'll see only the dates after the input date.

Standard SQL query returning correct results in BigQuery but not in Data Studio

I need to return a list of names which have parameters which fall between 2 dates. I have hard-coded two dates as part of the data verification. My sql query in BigQuery is as follows:
declare DS_START_DATE FLOAT64;
declare DS_END_DATE FLOAT64;
SET DS_START_DATE = 1578390532050;
SET DS_END_DATE = 1578391211289;
SELECT DISTINCT
Name AS block_names
FROM
my_data_source
LEFT JOIN
UNNEST (holes) AS n_holes
ON
1=1
WHERE
(n_holes.LastModifiedHoleDate ) > CAST(DS_START_DATE as FLOAT64)
AND n_holes.LastModifiedHoleDate < CAST(DS_END_DATE as FLOAT64)
Note: the DS_START_DATE and DS_END_DATE are both in UNIX time.
So basically, I am querying for results that were modified on the 7th of January, which will return only one result.
The above query return only one result, which is correct.
I have changed the format of the query slightly so that I can use it in the connection to my table in BigQuery from Data Studio:
SELECT DISTINCT
Name AS block_names,
n_holes.LastModifiedHoleDate as LM
FROM
my_data_source
LEFT JOIN
UNNEST (holes) AS n_holes
ON
1=1
WHERE
(n_holes.LastModifiedHoleDate ) >= CAST(#DS_START_DATE as FLOAT64)
AND n_holes.LastModifiedHoleDate <= CAST(#DS_END_DATE as FLOAT64)
I have enabled the date parameters in my Data Studio data source, and have finished creating the data source.
I have then made a test report using the above data source for the report. I simply have a date range control and a chart on my report.
No matter what range I choose on the date range, I get two results on the chart, where I should only get one.
Normally the tables and charts I have used in Data Studio have the option to select a date range dimension which links the data to the date range selected on the date time picker:
The table that I have added to this report has no such option. I am assuming that this is correct because we are using a data source that needs a start and end date?
The parameters option in the chart has nothing in it:
Again, I am assuming that Data Studio doesn't need parameters specified because I am using start and end dates.
What have I missed that my date range picker is not affecting the data displayed in my chart? It seems like it is linked to the chart automatically, but the results are wrong.
Many thanks in advance!
You need to convert #DS_START_DATE and #DS_END_DATE into a Unix timestamp if you want to compare them with one. For me this did the trick:
WHERE
n_holes.LastModifiedHoleDate >= UNIX_MILLIS(PARSE_TIMESTAMP('%Y%m%d', #DS_START_DATE))
AND n_holes.LastModifiedHoleDate <= UNIX_MILLIS(PARSE_TIMESTAMP('%Y%m%d', #DS_END_DATE))
I hope it also works for you!

Wrong data types when loading sql query with vba in excel

I have a SQL Query, which I want to use in VBA to load the data into Excel, the problem is that when the load is done the data in Excel is with the wrong type. For example, where it needs to be a date, it remains an integer, where an integer is a date. Is there any way to fix this?
Example my query:
sql = " SELECT DATA, ITEM, COMPANY, SALES FROM SALES "
You will have to format cells as per date format. Also ensure that values are correct in your query output.
Sheet1.Range("A2", "A100").NumberFormat = "yyyy-mm-dd"

Include missing dates as "0" in SELECT ... A,count(A) group BY (Google Sheets, or SQL)

I am creating a histogram of dates using SQL and I want to have "0" values for the missing dates. The specific SQL system I'm using is Google Sheets. Here is my example:
What I really want is for it to look like this:
Is there any way to do this?
The query that I'm using is SELECT a,COUNT(a) GROUP BY a ORDER BY a.
Is there a way to do what I want?
Create a column of the dates you want. For instance, you can put in the first date, the right below it have a formula that adds one day, and copy the formula down.
Then, use COUNTIF or a similar function to get the counts you want, e.g.:
=COUNTIF($A$1:$A:$5,"=" & C2)

Converting date-time format and querying based on a date condition in SQL

I have around 20,000 entries in a SQL table for which a date column is of the form
YYYY-MM-DD HH-SS. I would like to convert this format to a YYYY-MM-DD format so I can run a query on all of the entries that will count the number of entries based on
a) the month under which they fall
b) the day
I'm new to SQL and not sure if there is a way to loop through all of the entries and check based on the required criteria; and as such, would greatly appreciate any help.
I unfortunately, cannot send a screenshot of the table since the data is classified.
You don't need to change the data in the table. Most databases have year() and month() functions, so you could do:
select year(datecol), month(datecol), count(*)
from sqltable
group by year(datecol), month(datecol)
order by year(datecol), month(datecol);
If these specific functions are not available, then I'm sure your database supports something similar.