Add a Weekly Column average automatically after 7 Days - google-sheets

I currently have a formula in Cell B1 which is pulling all the unique dates within my dataset and sort them from recent to oldest. What I would like to do is, after every 7th date to automatically create a column with "Weekly Average" and then continue the dates within the data set once again.
Example:
I am not sure if this is possible via a formula, or if I would need to script something to get the same result, but any help would be greatly appreciated.

Here is a proof of concept for February - not quite there yet because it should display a final total for 28th February.
=ArrayFormula(transpose(filter(flatten(split(sort(unique(filter(datevalue('Cleaned Data'!A2:A),'Cleaned Data'!A2:A<>"")),1,false)&if(WEEKDAY(sort(unique(filter(datevalue('Cleaned Data'!A2:A),'Cleaned Data'!A2:A<>"")),1,false))=2,"\Weekly average",),"\"))
,flatten(split(sort(unique(filter(datevalue('Cleaned Data'!A2:A),'Cleaned Data'!A2:A<>"")),1,false)&if(WEEKDAY(sort(unique(filter(datevalue('Cleaned Data'!A2:A),'Cleaned Data'!A2:A<>"")),1,false))=2,"\Weekly average",),"\"))<>"")))
EDIT
=ArrayFormula(transpose(filter(flatten(split(if(WEEKDAY(sort(unique(filter(datevalue('Cleaned Data'!A2:A),'Cleaned Data'!A2:A<>"")),1,false))=1,"Weekly average\",)
&sort(unique(filter(datevalue('Cleaned Data'!A2:A),'Cleaned Data'!A2:A<>"")),1,false),"\"))
,flatten(split(if(WEEKDAY(sort(unique(filter(datevalue('Cleaned Data'!A2:A),'Cleaned Data'!A2:A<>"")),1,false))=1,"Weekly average\",)
&sort(unique(filter(datevalue('Cleaned Data'!A2:A),'Cleaned Data'!A2:A<>"")),1,false),"\"))<>"")))
It would be shorter to use query than filter:
=ArrayFormula(text(transpose(query(flatten(""&split(if(WEEKDAY(sort(unique(filter(datevalue('Cleaned Data'!A2:A),'Cleaned Data'!A2:A<>"")),1,false))=1,"Weekly average\",)
&sort(unique(filter(datevalue('Cleaned Data'!A2:A),'Cleaned Data'!A2:A<>"")),1,false),"\"))
,"select Col1 where Col1 is not null")),"DD/MM/YYYY"))
These should be OK up to Christmas 2022, which falls on a Sunday.

Related

Data Validation + VLOOKUP for Month in Sheets

So I'm currently setting up a sheet that records weekly numbers. I'd love to have something that summarizes the totals (money in, loss, etc) by month. So if the week = 4/4/0 this is April (4) and is calculated just by a drop-down of months. Is such a thing possible in Sheets? I don't need the formula just the clue, really.
Update:
I found something similar:
=SUMIFS($N$7:$N$11,$M$7:$M$11,">="&S8,$M$7:$M$11,"<="&EOMONTH(S8,0))
N7:N11 is amount
M7:M11 is date
S8 is the date I'm querying for
The issue I have with that (^^) formula is that it wants me to present the date as D-MONTH-YYYY
but I'd love the date to just be the MONTH
You can set the column to show just the month by setting its format:
and creating a "Custom date & time"
Alternatively, you can use text to represent the months and convert them to number representation using MONTH(S8&1)
Then, you can do some funky way of converting the "month" to "date" using DATE(YEAR, MONTH(S8&1), 1), where "year" is the year you're querying for.
To sum it up, the formula from your question might look something like this:
=SUMIFS($N$7:$N$11, $M$7:$M$11, ">="&DATE(2022, MONTH(S8&1), 1), $M$7:$M$11, "<="&EOMONTH(2022, MONTH(S8&1), 1),0))

How to get last closing price of a stock in google finance (In a non-trading day)

I tried using
=INDEX(GOOGLEFINANCE("NASDAQ:MSFT","price",TODAY(), 2),2,2)
And saw that on some days (e.g. 12/6/2021, a Saturday) the function failed to return value. I assume that it is because there was no trading on that day.
Is there a way for me to pick the last value of a stock prior to that day? (e.g. If I calculate on a Saturday or on a Sunday with an American stock I would get Friday's value, Thursday for an Israeli stock etc.)
I am not aware of a way for GOOGLEFINANCE to automatically adjust for no trading days.
One way to do it is to get the "price" data for the last 7 days (to be safe) and then query that data to get the "price" value next to the max date.
This formula works for me:
=INDEX(QUERY(GOOGLEFINANCE("NASDAQ:MSFT","price", TODAY()-7, TODAY()),"select Col1, Col2 order by Col2 desc",1), 2, 2)
You can use the formula below, in the cell that you want the price to be.
=INDEX(GOOGLEFINANCE(A5;"price";$B$2);2;2)
Where A5 contains the stock symbol, like CMI, JNJ, NEE or whatever.
And the B2 contains the following formula:
=if(weekday(B1)=2;B1-3;if(weekday(B1)=1;B1-2;B1-1))
Finally, B1 is just =today().
This will adjust the day for weekdays only. So if it is Saturday, Sunday or Monday, it will give you the price of the stock on Friday.
Basically it will give the last closing price on business days.
Best regards.

Select a "tax month" with a non standard month start/end

I have a Google Sheet with data like this:
Date
In
Out
July 13
£40
July 21
£60
etc.
I'd like to add another column "month" which specifies what month the entry was made in. Only problem is I can't use the standard MONTH() function because for accounting purposes, the tax month is 16th - 15th. So July 13 would be considered to be in the June/July tax month, while July 21 would be considered to be in the July/August tax month.
I'm assuming I will need to maintain a table of the specific cut off dates like so:
Month
Start
End
Jun/July
16th June
15th July
etc.
But I can't work out how to use this as a lookup table to achieve what I want. Any thoughts appreciated.
I think this should work if your date is in A1:
=IF(DAY(A1)>15,TEXT(A1,"MMMM")&"/"&TEXT(A1+20,"MMMM"),TEXT(A1-20,"MMMM")&"/"&TEXT(A1,"MMMM"))
If sort order is important (e.g. in a subsequent pivot table) I would use this:
=IF(DAY(A1)>15,TEXT(A1,"MM MMMM")&"/"&TEXT(A1+20,"MMMM"),TEXT(A1-20,"MM MMMM")&"/"&TEXT(A1,"MMMM"))
Another way to specify the sort order is maintaining a separate table with sort order column. It's a completely different approach.
use:
=ARRAYFORMULA(IF(A2:A="",,IF(DAY(A2:A)<16,
TEXT("1/"&MONTH(A2:A)-1, "mmmm")&"/"&TEXT("1/"&MONTH(A2:A), "mmmm"),
TEXT("1/"&MONTH(A2:A), "mmmm")&"/"&TEXT("1/"&MONTH(A2:A)+1, "mmmm"))))

Show only the current Week

I am working with Google sheets, and the Form responses that get poured into it. I am wondering if there is a way to show the CURRENT work week? What I mean by this is a sheet that shows this Friday:
MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY
124 123 193 -- 234
344
But then the next Monday:
MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY
088 -- -- -- --
Is this possible, or is it to much for google sheets?
It's certainly possible. To begin with, I would put dates under Monday, Tuesday, etc: this simplifies subsequent computations, and is also helpful when reading the data. This would go under "Monday": it returns the Monday of the current workweek:
=today()-weekday(today())+2
Here +2 compensates for subtraction when the date is Monday (which is numbered 2). The other days of the week can be obtained by adding 1 to Monday (=A2+1), or directly by using =today()-weekday(today())+3 and so on.
Then you can pull data from the form sheet using functions such as query, filter, vlookup, ... For example, this formula returns the 3rd column from Form Responses 1 where the date is nearest to the content of A2 (current Monday)
=vlookup(A2, 'Form Responses 1'!A:A, 3)
A possible issue here is that Form Responses record both date and time, so the "nearest" record may be from 11:50pm of previous day. One way to solve this is to use filter:
=filter('Form Responses 1'!C:C, floor('Form Responses 1'!A:A) = A2)
This returns all C column entries from the form where the date in the A column matches the content of A2.

ArrayFormula Output Ordering

I am trying to take the output of the ArrayFormula in the top answer of this previous question, where the output is days of the week, and I want to output them in the proper order (Sunday, Monday, ..., Saturday).
Currently, when I use this formula I get the order Friday, Monday, Saturday, Sunday, Thursday, Tuesday, Wednesday. I have tried using the "order by" clause listing the days of the week in order order by("Sunday", "Monday", ..., "Saturday" without success.
My formula right now is =QUERY({H:H,H:H},"select Col1, count(Col2) where Col1 != '' group by Col1 label count(Col2) 'Number of Calls'",1).
A sanitized version of the data I am attempting to use is here. This data is over the course of one year. It has also been edited to include the two solutions I have so far for reference of future viewers.
Is it currently possible to do the ordering of the days within the Query? If not, what is the best way of going about getting the correct order.
I can't edit your sheet, but this is what I suggest. I don't understand why you have columns D-G, but I will assume you have a reason and work with what you have. First, change H2 to =weekday(F2) and copy it down to H66 (last row). This will return the number of the day of the week (Sunday=1, Monday=2...Saturday=7). Then in I2-I8 fill in the days of the week in the order Sunday through Saturday. In J2 put:
=COUNTIF($H$2:H,"=1")
for Sunday. In J3 put:
=COUNTIF($H$2:H,"=2")
for Monday. And so on through Saturday. That should do it.

Resources