How to include month and year in arrayformula sumif statement - google-sheets

I have the following code which I'm using in my Google Spreadsheet.
=ArrayFormula(sumif(month(income!A$2:A), month(A2) , income!B$2:B))
There are two sheets, main and income.
Income sheet contains payments received in every month. (there are multiple payments in each month). So I sum up earnings in a month and display them as monthly earning in main sheet.
The problem is - since it only considers month, when we have april 2014 and april 2015, the total sum for april in main sheet counts both years.
I am trying to figure out how to use year+month in the formula. Any help?

I just figured it out using the TEXT function.
Here's what I did:
=ArrayFormula(sumif(text(income!A$2:A, "mmm yyyy"), text(A2, "mmm yyyy") , income!B$2:B))
The first value in sumif is this:
text(income!A$2:A, "mmm yyyy")
It basically fetches month and year from all rows in column A in income sheet.
The second value is:
text(A2, "mmm yyyy")
It fetches month and year from A2 cell in main sheet.
Those two are compared. And we get the result. Problem solved.

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))

Switch date and month google sheet

I have a data column which date in this format 05/12/2021 = dd/mm/yyyy but google sheet see it as 05/12/2021 = mm/dd/yyyy, how can I use a formula to switch the day and month around and display as 5/12/2021 = d/mm/yyyy. It has to be done on a formula level as there are many other dates column that has the format and information right.
Formula tried
=TEXT(A3:A,"d/mm/yyyy")
If the date is entered correctly, you should be able to format it by going to the 'Format' menu.
However, if you need to transpose the formatted day and month using formula, then try this in row 3 (any column but A):
=arrayformula(datevalue(regexreplace(to_text(A3:A),"(.|..)[\/\-\.](.|..)[\/\-\.](.*)","$2\/$1\/$3")))

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.

GoogleFinance formula to find previous historical trading day of a stock

I have been searching for a solid formula to find stock prices of "previous trading days" of a large batch of historical dates.
The formula I started with:
=googlefinance(A5, "close", B5-1)
where A5 refers to a ticker, and B5 refers to a date in the past.
I get #NA results for previous days that fall on weekends or holidays.
I need a formula that will work reliably for 365 days per year, no matter the day of the week or number of non-trading days prior. I have tried something like this:
=index(googlefinance(A5, "close", (B5-1)-2,2),2,2)
That formula works if the B5 date is the first trading day of the week, but not if it falls anywhere midweek.
I tried using WORKDAY(B5,-1) in a few ways but I realize that function can only calculate a number of workdays, not produce a date.
I need a formula that I do not have to edit and adjust cell by cell.
Here is one way that gives
the closing price of the date if it was a trading-day
the closing price of the previous trading day, if it was not a trading day.
=vlookup(B5+16/24,googlefinance(A5, "close", B5-7, 7),2,true)
Here is how it works:
googlefinance(A5, "close", B5-7, 7) gets the data for 7 trading days starting from the date 7 days ago.
vlookup get the close price on the date closest to the date in B5 if B5 is a non-trading day.
B5 + 16/24 is to match the date with the closing price date stamp of 4pm.

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)

Resources