How to integrate GOOGLEFINANCE results in an array formula? - google-sheets

Following ARRAYFORMULA is giving me an error "parameter 1 value is invalid."
=ARRAYFORMULA(IF(ISBLANK(B2:B), "", GOOGLEFINANCE(B2:B, "price")))
B column of my sheet contains the stock symbols whose value I wish to fill in my sheet. Example below
Company Name
GOOG
ADBE
MSFT
Can someone help here?

This questions seems to be a duplicate of Google finance as an argument for ArrayFormula. I have answered it with more detail. As a new user I'm only able to post an answer here, I cannot comment/flag as duplicate.
Anyway, assuming stock symbols are in range B2:B, then you put the following formula in cell C2:
=IFERROR(MAP(B2:B,LAMBDA(company,GOOGLEFINANCE(company,"price"))))
This is a new method since the introduction of LAMBDA and its helper functions in Google Sheets in August 2022.
The trick here is, as far as I understand, that MAP(LAMBDA) calculates the specified formula for each row in the input array separately (effect similar to manually expanding the formula over the whole range), whereas ARRAYFORMULA passes the whole array as an argument to the formula (GOOGLEFINANCE is special and doesn't work intuitively with such input).

LAMBDA and friends are available, so this works:
=BYROW(A2:A, LAMBDA(row, IF(row = "",, GOOGLEFINANCE(row, "price"))))
Old story:
GOOGLEFINANCE cannot be used in array formulas. You'll have to extend your formula downwards.
For example this formula should be in every cell from C2 and down:
=IF(NOT(ISBLANK($B$2:$B)), GOOGLEFINANCE($B$2:$B, "price"), "")

Related

mapping vlookup for each element of splitted list in google sheets

This question is also answered here:
Get a vlookup of a cell after split in Google sheet
but not marked as corrected answer, and cannot make it work.
Goal : I want to apply a vlookup function to a split function, so that I can search for corresponding values (found by the vlookup) for each token obtained from a string.
Consider this sheets:
// Sheet 'veggies'
A
apple, pine, tree
pine
// Sheet 'themes':
A
B
C
apple
8
theme1,theme2
tree
3
theme2
pine
1
theme1,theme3
I want to:
split cells of column A of 'veggies' by commas, so to have tokens
vlookup for the C column in 'themes' sheet, by using the index of tokens, for all of them
As approach I tried to first retrieve the frequences of tokens in column B, sheet 'themes', and cannot understand what my formula is doing:
=ARRAYFORMULA( VLOOKUP( split(A2;",");'themes'!A$2:D;2;FALSE))
This formula only get the frequency from column be for the first token, while for others will only report N/A saying could not find a value, but it is clearly there.
Any help?
Am I on the right track ?
P.s. if one would like to offer use of query , like in the other SO answer, please help me to break down what it does.
ARRAYFORMULA( VLOOKUP( split(A2;",");'themes'!A$2:D;2;FALSE))
Your formula works. But when splitting by comma , there's a extra space left over in all the elements from the second element. So, when
apple, pine, tree is splitted, it becomes apple, pine, tree(note the extra space prefix). To fix, you can simply add a space to the split as well:
=ARRAYFORMULA( VLOOKUP( split(A2;", ");'themes'!A$2:D;2;FALSE))
this should work if you want the results in one cell.
=ARRAYFORMULA(TEXTJOIN(", ";TRUE;IFERROR(VLOOKUP(SPLIT(A2;", ";0);themes!A:C,2,0))))
Use this formual
=ArrayFormula(LAMBDA(v,
IF(v="",,{v,SPLIT(VLOOKUP(v,themes!A2:C,3,0), ", ",1)}))
(FLATTEN(IF(A2:A="",,SPLIT(A2:A, ", ", 1)))))

Copy a NB.SI formula into a Google Sheets sheet

I am turning to you today for help with a problem on Google Sheets.
I receive this data from a Google Sheets form: An answer (0 or 1) to 5 different questions.
I would like to calculate in column A (in green) the scores out of 5 for each row, as soon as a new row is added by the form.
I tried to use the ARRAYFORMULA() function but it does the count for all the cells in the range and not just row by row:
Do you have an idea to have a score out of 5 for each line of question and have it apply to the whole file as soon as a new line is added by the Google Form?
Thanks for your help
If you want to use COUNTIF (English correspondance for NB.SI), modify your formula to:
=ARRAYFORMULA(COUNTIF(IF(B1:F=1,ROW(B1:B)), ROW(B1:B)))
or for your regional settings:
=ARRAYFORMULA(NB.SI(IF(B1:F=1,ROW(B1:B)), ROW(B1:B)))
You can get a row-by-row sum with sumif() like this in cell A3:
=arrayformula( sumif( if(column(B3:F), row(B3:F)), row(B3:F), B3:F) )
This formula uses open-ended range references so it will create results all the way down to the end of the sheet. To limit that, use a range reference like B3:F100 instead.

Google Sheets ARRAYFORMULA and SUMIF combination

I recently noticed a mistake in my calculation, and I've identified the root cause of the problem: it seems that I mistakenly used the SUMIF function in Google Sheets' ARRAYFORMULA.
I have the sample spreadsheet here.
I wrote the ARRAYFORMULA function that results in column C, which I thought would be the same with the formula in column B.
The formula in column C:
=ARRAYFORMULA(SUMIF(H$3:H$6&I$3:I$6,"<="&A3:A31&">="&A3:A31,G$3:G$6))
The formula in column B:
=SUMIFS(G$3:G$6,H$3:H$6,"<="&A3,I$3:I$6,">="&A3)
In essence, I want to get the value for each date based on predefined values with their own periods.
Please, use this formula in the cell C3:
=ARRAYFORMULA(MMULT(IF((A3:A31>=TRANSPOSE(H3:H5))*
(A3:A31<=TRANSPOSE(I3:I5))=1,TRANSPOSE(G3:G5),0),ROW(G3:G5)^0))
I made a new tab called MK.Help and erased all the other formulas. then i put this formula in C3:
=ARRAYFORMULA(ARRAY_CONSTRAIN(MMULT(N(A3:A>=TRANSPOSE(A3:A)),MMULT((A3:A>=TRANSPOSE(H3:H))*(A3:A<=TRANSPOSE(I3:I)),N(G3:G))),COUNTA(A3:A),1))
Does that work for you?

Is there a function that can create an expanding array formula that conditionally sums a column based on multiple criteria?

To start, this is my first time posting and so please let me know if I can fix my post in any way to make it easier to answer.
I am trying to create an auto-expanding array formula
I have a sheet with my investment asset mix that including amounts of shares owned of each particular stock, and a sheet that tracks when I receive dividends. My goal is to write an automatically expanding array formula that will sum up the amount of shares that own of a stock on the date a dividend is received and return that value. I have written three different formulas that all accomplish this but none of them will auto-expand as an array.
I'm sure there are a lot of solutions I've overlooked. To boil it down, I need an expanding array formula that will sum the "Shares" column of my asset mix sheet ('Asset Mix'!D2:D, or 'AssetMixShares') conditionally. The name of the stock entered in 'Dividends'!C2:C needs to match the name of the stock in 'Asset Mix'!A2:A (or the named range 'AssetMixStocks'). It then needs to check the dates in 'Asset Mix'!C2:C (or 'AssetMixDates') against the dates in 'Dividends'!A2:A and sum all share amounts where the purchase date is less than (earlier than) the Ex-Dividend Date.
I could probably write some sort of vlookup array on the "Running Total" column -- 'Asset Mix'!E:E -- that would solve the issue, but I'm hoping to eliminate that column. I feel very strongly that what I'm trying to do should be possible without the help of a running total column -- I just don't have the knowledge.
I have tried countless functions and formulas, but the four that I currently have in my example worksheet are SUM, SUMPRODUCT, DSUM, and QUERY.
Attempt 1
SUM and IF
=ArrayFormula(SUM(IF('Asset Mix'!A:A=C2,IF('Asset Mix'!C:C<A2,'Asset Mix'!D:D))))
Attempt 2
SUMPRODUCT
=({arrayformula(SUMPRODUCT(--((AssetMixStock=(indirect("C"&ROW())))*(AssetMixDate<(indirect("A"&ROW())))),AssetMixShares))})
Attempt 3
DSUM
=DSUM('Asset Mix'!A:E,"Shares",{"Date","Stock";"<"&A2,C2})
Attempt 4
QUERY
=arrayformula(query(AssetMix,"Select sum(D) where A = '"&C2:C&"' and C < date'"&(text(year(A2:A),"0000") & "-" & text(month(A2:A),"00") & "-" & text(day(A2:A),"00"))&"' label sum(D) ''",0))
These will all work, as long as I manually drag the formula down, but I want to write some sort of formula that will auto-expand to the bottom of the Dividends sheet.
I have tried to create a Dummy sheet that has both of the relevant sheets. Please let me know if you can access it -- the link is below.
https://docs.google.com/spreadsheets/d/1wlKffma0NJ0KrlWxyX_N20y62azsGpFp3enhmjzJK1Q/edit?usp=sharing
Thanks so much for getting this far and any help you can provide!
We can focus in the first formula to understand a way to make it "self-expandable". As we see it contains references to the cells A2 and C2 in "Dividends" sheet:
=ArrayFormula(SUM(IF('Asset Mix'!A:A=C2,IF('Asset Mix'!C:C<A2,'Asset Mix'!D:D))))
Every time some data appears in these columns (A and C), the formula should work. We can control the presence of the formula by onEdit trigger, if editing is manual. Consider the code:
function onEdit(e) {
var sheet = SpreadsheetApp.getActive().getActiveSheet();
if (sheet.getName() == 'Dividends') {
var row = e.range.getRow();
for (var offset = 0; offset < e.range.getHeight(); offset++) {
sheet.getRange(3, 10).copyTo(sheet.getRange(row + offset, 10));
}
}
}
It checks any modification on the sheet "Dividends" and copies required formula to the modified row(s). This way the formula is expanded for other rows in use.
Well, it's solved! I'll leave this up in case anyone else has the same question.
A kind soul explained the magic of MMULT() to me, and wrote this solution.
=ARRAYFORMULA(MMULT((C2:C=TRANSPOSE('Asset Mix'!A2:A))*(A2:A>TRANSPOSE('Asset Mix'!C2:C)),N('Asset Mix'!D2:D))

Google Spreadsheet Function That Sums Numbers In A Column When the Row Contains An EXACT Text

I've been at this problem for a while now. I am trying to sum numbers under a specific column when the rows equal a certain text and then display that sum on a different sheet. So far I came up with this formula: =IF(EXACT(A2,Table!A2:A)=TRUE,SUM(Table!C2:C)); however the only problem is that is sums everything in column C (which makes sense).
I wish there was a way to do something like the following: SUM(Table!C2:C where EXACT(A2,TABLE!A2:A)=TRUE). I've also tried the SUMIF(), DSUM(), and QUERY() functions to no avail. I must be getting logically tripped up somewhere.
Figured it out: =SUM(FILTER(Table!E4:E, EXACT(Table!A4:A,A4)=TRUE)).
=sum ( FILTER (b1:b10, a1:a10 = "Text" ) )
// the above formula will help you to take the sum of the values in column B when another column A contain a specific text.
The formula is applicable only in Google Spreadsheets

Resources