I'm trying to generate an array of dates in Google Sheets, where I know the first date, the last one and I want the other dates to be at 1-month interval, with the date always being the last day of the month.
StartDate (C3) = 2019-03-31
EndDate (I3) = 2019-06-30
Expected outcome:
2019-03-31
2019-04-30
2019-05-31
2019-06-30
This is the code I have tried
=ArrayFormula(ADD(C3,row(INDIRECT("C1:C"&eomonth(I3,0)-C3))))
But it's returning this
2020-04-01
2020-04-02
2020-04-03
2020-04-04
2020-04-05
2020-04-06
2020-04-07
2020-04-08
2020-04-09
2020-04-10
2020-04-11
2020-04-12
2020-04-13
2020-04-14
2020-04-15
2020-04-16
2020-04-17
2020-04-18
2020-04-19
Please try:
=ARRAYFORMULA(EOMONTH(C3,ROW(INDIRECT("a1:a"&DATEDIF(C3,I3,"M")+2))-1))
=DATEDIF(C3,I3,"M")+2 = the number of monthes between 2 dates includind these dates.
=ARRAYFORMULA(ROW(INDIRECT("a1:a3"))) = basic counter
=ARRAYFORMULA(TEXT(UNIQUE(EOMONTH(ROW(INDIRECT(
DATEVALUE(C3)&":"&DATEVALUE(I3))), 0)), "yyyy-MM-dd"))
My formula comes in 3 flavors.
Column Formula
C Dates in between two dates
C =ArrayFormula(edate(A1,sequence(datedif(A1,A2,"M"))))
D Dates in between two dates including End_date
D =ArrayFormula(edate(A1,sequence(datedif(A1,A2,"M")+1)))
E Dates in between two dates including Start_date and End_date
E =ArrayFormula(edate(A1,sequence(datedif(A1,A2,"M")+2)-1))
And on https://sheetaki.com/auto-populate-dates-between-two-given-dates-in-google-sheets/
there is a very easy to understand method (once you understand all the SEQUENCE parameters), but for day increments
=ARRAYFORMULA(TO_DATE(SEQUENCE(B1- A1 +1,1,A1)))
Auto-Populate Dates Between Two Given Dates in Google Sheets
Auto-Generate Dates Between Two Given Dates in Google Sheets
google sheets generate | populate a line for each month between two dates
Related
I'm trying to get Weekly MTD and YTD values based of hourly data, but I'm having difficulties achieving this.
This is the data I'm working with:
max(Date) - Last day of the week
ISOWeek - Week in question
Value - The data I'm trying to sum
SELECT MAX(ISOWeek) AS [ISOWeek]
,MAX(Date) AS [Date]
,SUM(Value1) AS [MTD]
FROM Table1
GROUP BY ISOWeek, FORMAT(Date,'yyMM')
ORDER BY ISOWeek DESC
This is what that query returns:
ISOWeek Date MTD
29 2020-07-19 367529
28 2020-07-12 367138
27 2020-06-30 103290
27 2020-07-05 266755
26 2020-06-28 346588
25 2020-06-21 337168
This is what I would like to get:
ISOWeek Date MTD
29 2020-07-19 261515
28 2020-07-12 184104
27 2020-07-05 103414
26 2020-06-28 432114
25 2020-06-21 346588
The data has to be grouped by ISOWeek, if it's a week that dips into two months, I'm only interested in the MTD of the month in which the week ends. We have hundreds of values, so the plan is to create a MTD view and a YTD view. If I can get some help with the MTD one, I can get the other one done.
I'm nearly sure that what I'm after has to do with a WHERE clause and DATEADD but I'm not too sure what it should say.
Thank you for taking the time.
I don't really follow the rules you would like to apply, but per dates apply the formula to get weekstart/monthend or what you need. Place the date instead of the current date in the example.
Then group by the modified date.
You could build a date dimension where you have the required dates in some columns (first day of month, first day of week,etc.). This way you get a table with all the dates and the matching result for each.
It might be easier/faster to join it on the requried column.
declare #monthstart date,
#monthend date,
#weekstart date
;
select #monthstart=datefromparts(year(current_timestamp),month(current_timestamp),1);
select #monthend=EOMONTH(getdate(),0);
select #monthstart,#monthend,EOMONTH(getdate(),1) as next_month, EOMONTH(getdate(),-1) as previous_month;
select cast(DATEADD(d,1-DATEPART(WEEKDAY,current_timestamp),CURRENT_TIMESTAMP) as date) as Sunday,
cast(DATEADD(d,2-case when DATEPART(WEEKDAY,current_timestamp)=1 then 8 else DATEPART(WEEKDAY,current_timestamp) end,CURRENT_TIMESTAMP) as date) as Monday
;
Creating a function that takes a start and end date and counts how many Sundays between those dates fell on the 1st of the month on kdb+, how would I do this?
The function needs to show how many times this has happened since 1950
Let's define a function which returns a weekday of its argument (of type date) first.
The underlying value of a date is the count of days from 1/1/2000 and we know that 1/1/2000 was Saturday. The next day was obviously Sunday, then Monday etc. and every 7th, 14th, 21st, etc. day after and before Jan 1, 2000 was Saturday too. So if we take a date modulo 7 we'll get a weekday number where 0 is Saturday, 1 is Sunday, etc. which leads us to the following definition.
weekday:{ `sat`sun`mon`tue`wed`thu`fri x mod 7 }
Now we can create a function that answers the original question:
sundaysThe1st:{[start;end]sum `sun=weekday dates where 1=`dd$dates:start+til 1+end-start }
start+til 1+end-start generates a list of dates between start and end, dates where 1=`dd$dates returns only the first days of the months and `sun=weekday dates returns 1b if the 1st day of the month is Sunday and 0b otherwise. sum is effectively the number of 1's which is exactly what we need.
Hope this helps.
I have MinLoginTime and MaxLoginTime stored in 2 globalmap variables:
globalMap.put("MinLoginTime","2017-10-24") //ignore the datetime format, but it a date
globalMap.put("MaxLoginTime","2018-04-26")
I want to put month wise iteration and fetch records. i.e. Here we see there are 7 months in example: 10,11,12,1,2,3,4
I want to generate these kind of dates:
FromDate ToDate
2017-10-01 2017-10-31
2017-11-01 2017-11-30
2017-12-01 2017-12-31
...
2018-04-01 2018-04-30
Then, need to iterate over each of these rows and do something (lets use tLog for now)
Could someone please help as to what Talend components can be used here for generating date ranges, where to store them and how to iterate them to do something?
You can achieve this pretty easily using a combination of Talend components and some Java code. Talend has a good collection of date manipulation functions.
First, store your global variable dates as Date type.
globalMap.put("MinLoginTime", TalendDate.parseDate("yyyy-MM-dd", "2017-10-24"))
Then tLoop_1 loops on all the months between your min and max dates. This code gets the number of months between the 2 dates :
TalendDate.diffDate((Date)globalMap.get("MaxLoginTime"),(Date)globalMap.get("MinLoginTime"),"MM")
tJava_3 just stores the date of the current iteration in a CURRENT_DATE global variable. It is the sum of the min date and the current iteration value (from 0 to N months).
globalMap.put("CURRENT_DATE", TalendDate.addDate((Date)globalMap.get("MinLoginTime"), (Integer)globalMap.get("tLoop_1_CURRENT_VALUE"), "MM"))
tFixedFlowInput_1 defines 2 Date columns: FromDate and ToDate in order to get the first and last day of the current iteration's month respectively.
TalendDate.getFirstDayOfMonth((Date)globalMap.get("CURRENT_DATE"))
TalendDate.getLastDayOfMonth((Date)globalMap.get("CURRENT_DATE"))
Check TalendDate class reference for all date manipulation methods.
I have a question about timestamps hope you can help me.
I'm reading one timestamp column from excel to matlab using;
[temp, timestamps] = xlsread('2012_15min.xls', 'JAN', 'A25:A2999');
This column have date like this:
01-01-2012 00:00
01-01-2012 00:15
01-01-2012 00:30
01-01-2012 00:45
01-01-2012 01:00
(it goes on until the end of January in periods of 15 minutes)
Now I want to get a new column in matlab that keeps only year month day and hour, this data must be separated and I don't want to keep repetitive dates (e.g I don't want to get 4 dates with 01 01 2012 0 only one of that)
So I want to get:
01 01 2012 0
01 01 2012 1
01 01 2012 2
It must go until the end of January with periods of 1 hour.
If you know that there is data for every hour you could construct this directly, but if you have possible missing data and you therefore need to convert from your timestamps, then some combination of datestr/datenum/datevec is usually the best bet.
First, convert timestamps with datevec:
times = datevec(timestamps); % sometimes need to also use format string
Then, take only the year/month/day/hour, removing repetitions:
[times_hours,m,n] = unique(times(:,1:4), 'rows');
You can use the indices in m to extract the matching data for those times.
If you want this converted back to some sort of string you can use datestr and specify format:
timesout = datestr(times_hours,'dd mm yyyy hh');
NEWBIE at work! I am trying to create a simple summary that counts the number of customer visits and groups by 1) date and 2) hour, BUT outputs this:
Date Day of Wk Hour #visits
8/12/2013 Monday 0 5
8/12/2013 Monday 1 7
8/12/2013 Monday 6 10
8/13/2013 Tuesday 14 25
8/13/2013 Tuesday 16 4
We are on military time, so 14 = 2:00 pm
Select
TPM300_PAT_VISIT.adm_ts as [Date]
,TPM300_PAT_VISIT.adm_ts as [Day of Week]
,TPM300_PAT_VISIT.adm_ts as [Hour]
,count(TPM300_PAT_VISIT.vst_ext_id) as [Total Visits]
From
TPM300_PAT_VISIT
Where
TPM300_PAT_VISIT.adm_srv_cd='22126'
and TPM300_PAT_VISIT.adm_ts between '07-01-2013' and '08-01-2013'
Group by
cast(TPM300_PAT_VISIT.adm_ts as DATE)
,datepart(weekday,TPM300_PAT_VISIT.adm_ts)
,datepart(hour,TPM300_PAT_VISIT.adm_ts)
Order by
CAST(TPM300_PAT_VISIT.adm_ts as DATE)
,DATEPART(hour,TPM300_PAT_VISIT.adm_ts)
This should solve the problem:
; With Streamlined as (
SELECT
DATEADD(hour,DATEDIFF(hour,'20010101',adm_ts),'20010101') as RoundedTime,
vst_ext_id
from
TPM300_PAT_VISIT
where
adm_srv_cd='22126' and
adm_ts >= '20130701' and
adm_ts < '20130801'
)
Select
CONVERT(date,RoundedTime) as [Date],
DATEPART(weekday,RoundedTime) as [Day of Week],
DATEPART(hour,RoundedTime) as [Hour],
count(vst_ext_id) as [Total Visits]
From
Streamlined
Group by
RoundedTime
Order by
CONVERT(date,RoundedTime),
DATEPART(hour,RoundedTime)
In the CTE (Streamlined)'s select list, we floor each adm_ts value down to the nearest hour using DATEADD/DATEDIFF. This makes the subsequent grouping easier to specify.
We also specify a semi-open interval for the datetime comparisons, which makes sure we include everything in July (including stuff that happened at 23:59:59.997) whilst excluding events that happened at midnight on 1st August. This is frequently the correct type of comparison to use when working with continuous data (floats, datetimes, etc), but means you have to abandon BETWEEN.
I'm also specifying the dates as YYYYMMDD which is a safe, unambiguous format. Your original query could have been interpreted as either January 7th - January 8th or 1st July - 1st August, depending on the settings of whatever account you use to connect to SQL Server. Better yet, if these dates are being supplied by some other (non-SQL) code, would be for them to be passed as datetimes in the first place, to avoid any formatting issues.