Highcharts Navigator shows datetime in milliseconds - highcharts

How can I show datetime in the highcharts navigator as '%b, %Y' instead of milliseconds
SQL Code :
SELECT DATE_FORMAT(a1.date_time, '%d %b, %Y') AS dt_mon_yr,
(a4.ph1_active_energy)*10 - a1.ph1_active_energy AS 'ph1',
(a4.ph2_active_energy)*10 - a1.ph2_active_energy AS 'ph2',
(a4.ph3_active_energy)*10 - a1.ph3_active_energy AS 'ph3'
FROM powerpro a1
JOIN (SELECT DATE(date_time) date, MIN(date_time) AS min
FROM powerpro GROUP BY DATE(date_time)
) a2 ON a1.date_time = a2.min
JOIN (SELECT DATE(date_time) date, MIN(date_time) AS min
FROM powerpro GROUP BY DATE(date_time)
) a3 ON DATE(a1.date_time) = a3.date - INTERVAL 1 DAY
JOIN powerpro a4
ON a4.date_time = a3.min
WHERE DATE(a1.date_time) BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW() ORDER BY a1.date_time

You can use axis labels formatter and then use Highcharts.dateFormat

Related

How to type static date into formula calculation?

I am trying to create a formula that calculates the number of hours based on the pricing at the time the job was done and the total cost of the job in Google Sheets. Unfortunately, I'm running into a #ERROR when I try to run it and I think it has something to do with how the Dates are typed in and not the logic of the nested IF statements. Any thoughts? Can add more information if needed.
=IF((A2 >= 1/1/21 AND A2 <= 4/13/21), IF(E2 = "Elbow Grease", D2/165, D2/125), IF(E2 = "Elbow Grease", D2/195, D2/155))
A2 would be formatted like 1/1/21
E2 would be a string
D2 would be a numerical value (total cost)
try:
=IF((A2 >= "1/1/21") * (A2 <= "4/13/21"),
IF(E2 = "Elbow Grease", D2/165, D2/125),
IF(E2 = "Elbow Grease", D2/195, D2/155))

How to get last row which matches values in 2 columns on Google Sheet

I have a transaction table that contains the transaction of items bought whenever they are bought. Latest transaction will contains the total amount spent on that item after the item is bought.
I want to create a chart table of the amount spent over time based (daily) on the item. I use the following formula to get the row number of the item's latest transaction
ARRAYFORMULA(MAX(IF(Transaction!$B:$B="Apple",ROW(Transaction!$B:$B),-1))) --> returns 6'
But I can't seem to add on to this formula to get the latest row based on Item and if the Date is less than or equals to my chart date
TRANSACTION TABLE
1
Date
Item
Amount
Prev. Spent
Curr. Spent
2
1 Jan 2020
Apple
$10
$0
$10
3
1 Jan 2020
Banana
$12
$0
$12
4
2 Jan 2020
Corn
$3
$0
$3
5
2 Jan 2020
Banana
$5
$12
$17
6
3 Jan 2020
Apple
$50
$10
$60
7
5 Jan 2020
Banana
$23
$17
$40
CHART TABLE
Date
Apple
Banana
Corn
1 Jan 2020
$10
$12
$0
2 Jan 2020
$10
$17
$3
3 Jan 2020
$60
$17
$3
4 Jan 2020
$60
$17
$3
5 Jan 2020
$60
$40
$3
To solve your problem with simple formula, I would use sumifs function:
=sumifs($D:$D,$C:$C,I$15,$B:$B,$H17)+I16
Here is how the Gsheet data look like:
You can accomplish this via a custom function created in Google Apps Script. To achieve this, follow these steps:
In your spreadsheet, select Tools > Script editor to open a script bound to your file.
Copy this function in the script editor, and save the project (check inline comments for more information about what the function is doing):
function getTable(values) {
let times = values.map(row => row[0].getTime());
const minTime = Math.min(...times); // Get first day in series
const maxTime = Math.max(...times); // Get last day in series
times = getTimes(minTime, maxTime); // Get all days between minimum and maximum
const items = [...new Set(values.map(row => row[1]))]; // Get unique list of items
let targetValues = [["Date", ...items]]; // Header row
for (let i = 0; i < times.length; i++) { // Loop through all days
const time = times[i];
const dayRows = values.filter(row => row[0].getTime() === time); // Get rows with this day
if (dayRows) {
const rowValues = items.map((item, j) => { // Loop through all items in day rows
const foundRow = dayRows.find(row => row[1] === item); // Find row corresponding to day and item
const previous = i === 0 ? 0 : targetValues[targetValues.length - 1][j + 1]; // Value in previous row
if (foundRow) return foundRow[2] + previous;
else return 0 + previous;
});
targetValues.push([new Date(time), ...rowValues]);
} else { // If day is not in source data, values are the same than previous row
targetValues.push([new Date(time), ...targetValues[targetValues.length - 1].slice(1)]);
}
}
return targetValues;
}
function getTimes(minTime, maxTime) {
let allTimes = [minTime];
let currentTime = minTime;
while (currentTime < maxTime) {
let date = new Date(currentTime);
date.setDate(date.getDate() + 1);
currentTime = date.getTime();
allTimes.push(currentTime);
}
return allTimes;
}
Now, if you go back to your spreadsheet, you can use the function getTable as if you were using a regular sheets formula. You just have to provide the appropriate range as an argument (in this case A2:E7), as you can see here:
Reference:
Custom Functions in Google Sheets
I can offer a formula in two stages.
Transactions tab:
Pivot tab, cell A1:
=query({Transactions!A:F},"select Col2,max(Col4) where Col1 is not null group by Col2 pivot Col3",1)
ChartData tab, cell A1:
=arrayformula(
array_constrain(
{Pivot!A1:D1;
(sequence(max(row(Pivot!A:A))-1,1,min(Transactions!$B:$B))),
sumif(row(Pivot!B2:B),"<="&row(Pivot!B2:B),Pivot!B2:B),
sumif(row(Pivot!C2:C),"<="&row(Pivot!C2:C),Pivot!C2:C),
sumif(row(Pivot!D2:D),"<="&row(Pivot!D2:D),Pivot!D2:D)
},
counta(Pivot!A:A)+1,counta(Pivot!1:1)))

Can I join two queries together in a Stored procedure on Redshift?

Originally I was trying this with a function but to no avail. Redshift doesn't allow for table return types or select data from tables. I'm hoping that maybe there is a way to do this with a Stored procedure, but I don't have much confidence any more. I have created a SP for the code listed below is it possible two join the two queries to itself for the SP?
tried doing a function, can't happen I have read the slim documentation that Amazon has for SPs and didn't get an answer for what I'm looking for
YTD current year
select
year
, month
, revenue
,transactiondate
, Flag
from (
select
extract(year from transactiondate) as year
, to_char(transactiondate, 'Mon') as month
, extract(month from transactiondate) as month_number
, sum(netamount) as revenue
,transactiondate
,Flag
from
vw_costs_of_businesss_copy
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(transactiondate, 'YYYY-MM-DD') ->= concat(to_char(extract(year from '2019-01-01'), 'YYYY'),'01-01') --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(extract(year from to_date('2019-01-01', 'YYYY-MM-DD'))),'01-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(Cas(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as text ),'-01-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(Cast(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as Text),'0000'),'-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(transactiondate, 'YYYY-MM-DD')>= to_date(concat(to_char(Cast(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as Text),'0000'),'-01') , 'YYYY-MM-DD')
group by
year
, month
, month_number
,transactiondate
, Flag
)
order by month_number, year
--YTD PREV YEAR
select
year
, month
, revenue
,transactiondate
, Flag
from (
select
extract(year from transactiondate) as year
, to_char(transactiondate, 'Mon') as month
, extract(month from transactiondate) as month_number
, sum(netamount) as revenue
,transactiondate
,Flag
from
vw_costs_of_businesss_copy
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(transactiondate, 'YYYY-MM-DD') ->= concat(to_char(extract(year from '2019-01-01'), 'YYYY'),'01-01') --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(extract(year from to_date('2019-01-01', 'YYYY-MM-DD'))),'01-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(Cas(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as text ),'-01-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(Cast(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as Text),'0000'),'-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(transactiondate, 'YYYY-MM-DD')>= to_date(concat(to_char(Cast(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')-1) as Text),'0000'),'-01') , 'YYYY-MM-DD')
group by
year
, month
, month_number
,transactiondate
, Flag
)
order by month_number, year
It needs to have the YTD and prev_YTD listed so I can pass it as a param, is this possible? I realize I'd have to make a table for it to do so.
Where do I place the Prev_year, Prev-month columns
I think you are looking for the concept of UNION ALL to "join the two queries".
Here is an example using a Stored Procedure. See "Overview of Stored Procedures in Amazon Redshift" for more info.
CREATE PROCEDURE ytd_comparison(
date_param IN DATE
, result_set INOUT REFCURSOR)
AS $$
BEGIN
OPEN result_set FOR --YTD
SELECT year , month , revenue , transactiondate , Flag
FROM (SELECT EXTRACT(year FROM transactiondate) AS year
, TO_CHAR(transactiondate, 'Mon') AS month
, EXTRACT(month FROM transactiondate) AS month_number
, SUM(netamount) AS revenue
, transactiondate
, Flag
FROM vw_costs_of_businesss_copy
WHERE TO_DATE(transactiondate, 'YYYY-MM-DD') <= date_param
AND TO_DATE(transactiondate, 'YYYY-MM-DD') >= DATE_TRUNC('year',date_param)
GROUP BY year , month , month_number , transactiondate , Flag
)
UNION ALL --YTD PREV YEAR
SELECT year ,month ,revenue ,transactiondate ,Flag
FROM (SELECT EXTRACT(year FROM transactiondate) AS year
, TO_CHAR(transactiondate, 'Mon') AS month
, EXTRACT(month FROM transactiondate) AS month_number
, SUM(netamount) AS revenue
, transactiondate
, Flag
FROM vw_costs_of_businesss_copy
WHERE TO_DATE(transactiondate, 'YYYY-MM-DD') <= DATEADD(year, -1, date_param)
AND TO_DATE(transactiondate, 'YYYY-MM-DD') >= DATE_TRUNC('year', DATEADD(year, -1, date_param))
GROUP BY year , month , month_number , transactiondate , Flag
)
ORDER BY month_number , year
;
RETURN;
END;
$$ LANGUAGE plpgsql;
You need to provide a name for the output REFCURSOR when calling the procedure. You must open a transaction with BEGIN to maintain the cursor, then fetch the output from the REFCURSOR, and then discard the results cursor with END transaction.
BEGIN;
CALL ytd_comparison ('2016-07-01'::DATE, 'ytd_results');
FETCH ALL FROM ytd_results;
END;
UPDATE:
Alternatively, you could create a view for just the previous day.
CREATE OR REPLACE VIEW ytd_comparison AS
SELECT year , month , revenue , transactiondate , Flag
FROM (SELECT EXTRACT(year FROM transactiondate) AS year
, TO_CHAR(transactiondate, 'Mon') AS month
, EXTRACT(month FROM transactiondate) AS month_number
, SUM(netamount) AS revenue
, transactiondate
, Flag
FROM vw_costs_of_businesss_copy
WHERE TO_DATE(transactiondate, 'YYYY-MM-DD') <= GETDATE() - 1
AND TO_DATE(transactiondate, 'YYYY-MM-DD') >= DATE_TRUNC('year',GETDATE() - 1)
GROUP BY year , month , month_number , transactiondate , Flag
)
UNION ALL --YTD PREV YEAR
SELECT year ,month ,revenue ,transactiondate ,Flag
FROM (SELECT EXTRACT(year FROM transactiondate) AS year
, TO_CHAR(transactiondate, 'Mon') AS month
, EXTRACT(month FROM transactiondate) AS month_number
, SUM(netamount) AS revenue
, transactiondate
, Flag
FROM vw_costs_of_businesss_copy
WHERE TO_DATE(transactiondate, 'YYYY-MM-DD') <= DATEADD(year, -1, GETDATE() - 1)
AND TO_DATE(transactiondate, 'YYYY-MM-DD') >= DATE_TRUNC('year', DATEADD(year, -1, GETDATE() - 1))
GROUP BY year , month , month_number , transactiondate , Flag
)
ORDER BY month_number , year
;

Google sheet -> filtering a query

I created the following sheet:
https://docs.google.com/spreadsheets/d/1AJmzlBlrDZ0mfb_9aSm83fzZxrwYWOxWGxjNhlfqPLA/edit?usp=sharing
I am trying to find a formula to calculate the totals (C7, C8, C9) and wherever it is copied ie C17, C18, C19)
So for C7 I want to sum column D (Duration) wherever:
the Date (col A) is the same as the total row date (that is A7)
The Category (col E) is Internal
Similarly for So for C8 I want to sum column D (Duration) wherever:
the Date (col A) is the same as the total row date (that is A8)
The Category (col E) is External
and
for C9 I want to sum column D (Duration) wherever:
the Date (col A) is the same as the total row date (that is A9)
Has any category
I tried for C7 -
=QUERY({A:E, arrayformula(N(D:D))}, "select sum(Col6) where Col5='Internal' and Col1 = " & A7 & " label sum(Col6) ''",1)
but I get NA ??? any help ?
-
I wouldn't complicate life with a QUERY() here. Instead, just use FILTER().
Total Internal: =SUM(FILTER(D:D, A:A=A7, E:E="Internal"))
Total External: =SUM(FILTER(D:D, A:A=A8, E:E="External"))
Total All: =SUM(FILTER(D:D, A:A=A9))

Trouble in calculated column over date range in Power Pivot using DAX expression

As per application need, Need to do below task in Power Pivot report. (Not in SQL).
I have two date columns in one table
Column Hears :
/* Column Heads :*/ Activity| City | Start_Date | End_Date
/* Row 1 :*/ A1 | C1 | 01/01/2014 | 05/01/2014
/* Row 2 :*/ A1 | C1 | 06/01/2014 | 07/01/2014
/* Row 3 : */ A2 | C2 | 06/01/2014 | 07/01/2014
/* Row 4 : */ A3 | C3 | 03/01/2014 | 04/01/2014
Expected output like - If user selects date range 02/01/2014 to 07/01/2014
Column Header
City | #StartCount| #EndCount
C1 | 1 | 2
C2 | 1 | 1
C3 | 1 | 1
Here #StartCount is
Need to grouped by City.
Its Count of Distinct Activity
It should consider date boundries as : All activity for which start date 'greater or equeal(>=)' Input Start date and (less than '<') End Date
Here #EndCount is
Need to grouped by City.
Its Count of Distinct Activity
It should consider date boundries as : All activity for which Ends date 'Less or equeal(<=)' Input End date and (Greater than '>') End Date
Could you please suggest me expression to be used for such case.
Calculated measure or dax can be used..
The only way I can match your sample data is to ignore the "distinct activities" requirement in your question, since city C1 has only activity A1 and yet the final result counts to 2.
I'm also probably making a lot of assumptions that may be wrong because your sample data is quite limited. But here's my attempt:
declare #Activities table (Activity char(2),City char(2),Start_Date date,End_Date date)
insert into #Activities (Activity,City,Start_Date,End_Date) values
('A1','C1','20140101','20140105'),
('A1','C1','20140106','20140107'),
('A2','C2','20140106','20140107'),
('A3','C3','20140103','20140104')
declare #Start date
declare #End date
select #Start = '20140102', #End = '20140107'
select City,
SUM(CASE WHEN Start_Date < #Start THEN 0 ELSE 1 END) as StartCount,
SUM(CASE WHEN End_Date > #End THEN 0 ELSE 1 END) as EndCount
from #Activities where Start_Date < #End and End_Date > #Start
group by City
(Some of this relies on SQL Server syntax, but since you haven't tagged your question with a database system, I get to pick)
SQL Server syntax
select activity, city,
SUM(case when (startdate >= '2014/01/02' and startdate <= '2014/01/07') then 1 else 0 End) as IsStart,
SUM(case when (enddate >= '2014/01/02' and enddate <='2014/01/07') then 1 else 0 End) as IsEnd
from temp
Group by activity, city
demo: http://sqlfiddle.com/#!3/b2a64/1

Resources