Google sheets previous 6 months data query - google-sheets

so I have this query that pulls all the data from the previous month but I would like it to pull the previous 6 months
=query(Insiders!A4:L,"Select * where month(C)=month(now())-1",1)

Taking the current month as July, if you want to display results where the date in Col C is in Jan 2021 to June 2021 inclusive, then try this:
=arrayformula(query(Insiders!A4:L,"Select * where C>= date '"&text(eomonth(today(),-7)+1,"yyyy-mm-dd")&"' and C< date '"&text(eomonth(today(),-1)+1,"yyyy-mm-dd")&"' ",1))
Currently (at 10/7/2021), text(eomonth(today(),-7)+1,"yyyy-mm-dd") = 2021-01-01
text(eomonth(today(),-1)+1,"yyyy-mm-dd") = 2021-07-01
These are the two date search ranges.
You can alter the eomonth() function parameters -7 and -1 to move the months into a different period.

try:
=QUERY(Insiders!A4:L, "where month(C)+1="&MONTH(TODAY()), 1)

Related

Show value if months equals current month

I have a google sheet with monthly targets per product, and in another sheet I want to reference this and only show the target of the current month.
June 2022 July 2022 August 2022
Product 1 50 60 70
Product 2 20 40 60
The formula I tried is:
=IF(MONTH(A1)=MONTH(targets!$B$1:$D$1), targets!B2:D2, "")
Where A1 has =TODAY()
Use FILTER() function.
=FILTER(B2:D3,MONTH(B1:D1)=MONTH(F1))
You could also try
=index(B2:D3,0,match(G1,B1:D1))
But exact match might be safer
=index(B2:D3,0,match(eomonth(G1,-1)+1,B1:D1,0))
in case the required month is missing.

EOMONTH returns the 1st day of the next month for months with 30 days

When I use the formula below the results of the EOMONTH function
returns the start of the next month for any month with 30 days instead of the last day of the specified month. The month and years are correct, so I'm pretty sure it's EOMONTH when used in another function.
For example,the results in B3 should be "11/31/1965" but it returns "12/1/1965".
=DATE(YEAR(B2),MONTH(B2)+6,DAY(TEXT(EOMONTH(MONTH(B2)+6,0))))
I have tried subtracting a day, but it returns the end-of-month -1 for months with 31 days (30). So I have the same problem in the other case.
I have also used IFS() to account for months with 30 days, and it miscalculates the date the same way.
=IFS( MONTH(B2)+6 = 4,DATE(YEAR(B2),MONTH(B2)+6,DAY(EOMONTH(MONTH(B2)+6,0))-2) ,
MONTH(B2)+6 =
6,DATE(YEAR(B2),MONTH(B2)+6,DAY(EOMONTH(MONTH(B2)+6,0))-2) ,
MONTH(B2)+6 =
9,DATE(YEAR(B2),MONTH(B2)+6,DAY(EOMONTH(MONTH(B2)+6,0))-2) ,
MONTH(B2)+6 =
11,DATE(YEAR(B2),MONTH(B2)+6,DAY(EOMONTH(MONTH(B2)+6,0))-2) ,
TRUE ,DATE(YEAR(B2),MONTH(B2)+6,DAY(EOMONTH(MONTH(B2)+6,0)) ) )
The EOMONTH function by itself where I just pass in the date as a string works correctly (column F).
Any Idea on what I'm doing wrong?
Thanks in advance.
Yes, as #player0 has explained, you can't just add something to a month and feed it into eomonth. Try putting
=eomonth(month(B2)+6,0)
into B3 (formatted as a date).
You get
1/31/1900
Why? month(b2)+6 gives 11 (which is just a number). Dates in google sheets are represented as days since 12/31/1899. So 11 formatted as a date gives 1/11/1900. Applying eomonth to that gives the last day of January 1900, which is the 31st. Feeding that into your formula would give 11/31/65, but that date doesn't exist, so you get 12/1/65.
If you want to go forward 6 months and then get the last day of the month, you need
=eomonth(date(year(B2),month(B2)+6,1),0)
You can also use the Edate function, which does not roll over into the first day of the next month:
=eomonth(edate(B2,6),0)
EOMONTH does not understand MONTH. instead, it converts it into date. to use EOMONTH you need to supply it with valid date
=EOMONTH(B2, 0)

Get date from week number in Google Sheets

If I have week 7 in 2017 what week date is the Monday in that week in Google Sheets?
=DATE(B9,1,1)-WEEKDAY(DATE(B9,1,1),3)+7*(WEEKDAY(DATE(B9,1,1),3)>3)+7*(A9-1)
is the least complicated formula I know which works for week numbers in Sweden (i.e. Monday first day of week, ISO rules for what is week 1).
Short answer (A1==Week, B1==Year):
=DATE(B1;1;1)+((A1-1)*7)-WEEKDAY(DATE(B1;1;1);3)
Long answer:
DATE(<year>;1;1) // days since 1970 until the frist day of the year
plus
((<week number>-1)*7) // how many days into the year is this week
minus
WEEKDAY(DATE(<year>;1;1);3) // how many extra days from previous year in first week
PS:
This assumes monday as the first day of week you have to change the arguments for WEEKDAY to change it to sunday
Because of this definition (https://en.wikipedia.org/wiki/Week) the 4th of January must be used instead the 1st. The 4th of January is the first day which is always in the week 1.
=DATE(B1;1;4)+((A1-1)*7)-WEEKDAY(DATE(B1;1;4);3)
If you are using ISO weeks, the accepted answer doesn't account for weeks overlapping on 2 technical years like 2020-w53, which is from 28 Dec 2020 until 3 Jan 2021.
Therefore I'm using this formula instead:
=DATE(K2,1,1)-WEEKDAY(DATE(K2,1,1),2)+7*(WEEKDAY(DATE(K2,1,1),2)>3)+7*(L2-1) +1
Where K is the Year, and L is the Week number (split in 2 columns from yyyy-ww)
to have it in an arrayformula:
=ArrayFormula(if(K2:K="",, DATE(K2:K,1,1)-WEEKDAY(DATE(K2:K,1,1),2)+7*(WEEKDAY(DATE(K2:K,1,1),2)>3)+7*(L2:L-1) +1 ))
You can use =ArrayFormula(if(E2:E="",,split(E2:E,"-"))) to split yyyy-ww in two columns.
NOTE: This formula would return the Monday (Which is the first day of the week in international standard, ISO)
Worked this up for 2023. It will work through end of 2024 too .. that said the AND logic is flawed .. feel free to suggest something to make this better
=IFS(
AND(ISOWEEKNUM(A8)=52,YEAR(A8)<>YEAR(A7)),
DATE(YEAR(A8-1),1,1)-WEEKDAY(DATE(YEAR(A8-1),1,1),3)+7*(WEEKDAY(DATE(YEAR(A8-1),1,1),3)>3)+7*(ISOWEEKNUM(A8)-1),
DATE(YEAR(A8),1,1)-WEEKDAY(DATE(YEAR(A8),1,1),3)+7*(WEEKDAY(DATE(YEAR(A8),1,1),3)>3)+7*(ISOWEEKNUM(A8)-1)
)

Showing days passed in month, or total days in month if the month is over (google spreadsheets)

Looking for a formula (for google spreadsheets) that shows either the days past in the current month if we're not past the last day of the current month, or the total days in that month if that month is behind us.
So, if today's March 25th, the formula would output 25 ... if today's April 1st, though, the formula would output 31.
Based on your sample, this should work (assuming the date is in A1):
=if(day(A1)=1,A1-date(year(A1),month(A1)-1,1),A1-date(year(A1),month(A1),1)+1)

Add one month to current date

I want to add 1 month to date, but it is adding only 4 weeks.
I tried like this,
2012-05-04 + DateTimeUtilities.ONEMONTH = 2012-05-31
The result i am getting is 2012-05-31
I want to add a full month (30 days or 31, or when month is a leapyear 29 or 28).
Try converting your time to a Calendar object then increment the month field:
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(timeInMillis));
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+1);
long newTimeInMillis = cal.getTime().getTime();
You may want to check for overflow from December to January and increment the year.
The API documentation confirms that DateTimeUtilities.ONEMONTH is four weeks, so what you got is what you should expect.

Resources