Using VLOOKUP in an array formula on Google Spreadsheets - google-sheets

Effectively I want to give numeric scores to alphabetic grades and sum them. In Excel, putting the LOOKUP function into an array formula works:
{=SUM(LOOKUP(grades, scoringarray))}
With the VLOOKUP function this does not work (only gets the score for the first grade). Google Spreadsheets does not appear to have the LOOKUP function and VLOOKUP fails in the same way using:
=SUM(ARRAYFORMULA(VLOOKUP(grades, scoresarray, 2, 0)))
or
=ARRAYFORMULA(SUM(VLOOKUP(grades, scoresarray, 2, 0)))
Is it possible to do this (but I have the syntax wrong)? Can you suggest a method that allows having the calculation in one simple cell like this rather than hiding the lookups somewhere else and summing them afterwards?

I'm afraid I think the answer is no. From the help text on
http://docs.google.com/support/spreadsheets/bin/answer.py?answer=71291&query=arrayformula&topic=&type=
The real power of ARRAYFORMULA comes when you take the result from one of those computations and wrap it inside a formula that does take array or range arguments: SUM, MAX, MIN, CONCATENATE,
As vlookup takes a single cell to lookup (in the first argument) I don't think you can get it to work, without using a separate range of lookups.

Google Spreadsheets does not appear to have the LOOKUP function
Presumably not then but it does have now:
grades Sheet1!A2:A4
scoringarray Sheet1!A2:B4

I still can't see the formulae in your example (just values), but that is exactly what I'm trying to do in terms of the result; obviously I can already do it "by the side" and sum separately - the key for me is doing it in one cell.
I have looked at it again this morning - using the MATCH function for the lookup works in an array formula. But then the INDEX function does not. I have also tried using it with OFFSET and INDIRECT without success. Finally, the CHOOSE function does not seem to accept a cell range as its list to choose from - the range degrades to a single value (the first cell in the range). It should also be noted that the CHOOSE function only accepts 30 values to choose from (according to the documentation). All very annoying. However, I do now have a working solution in one cell: using the CHOOSE function and explicitly listing the result cells one by one in the arguments like this:
=ARRAYFORMULA(SUM(CHOOSE(MATCH(D1:D8,Lookups!$A$1:$A$3,0),
Lookups!$B$1,Lookups!$B$2,Lookups!$B$3)))
Obviously this doesn't extend very well but hopefully the lookup tables are by nature quite fixed. For larger lookup tables it's a pain to type all the cells individually and some people may exceed the limit of 30 cells.
I would certainly welcome a more elegant solution!

I know this thread is quite old, but I'd been struggling with this same problem for some time. I finally came across a solution (well, Frankenstiened one together). It's only slightly more elegant, but should be able to work with large data sets without trouble.
The solution uses the following:
=ARRAYFORMULA(SUM(INDIRECT(ADDRESS(MATCH(), MATCH())))
as a surrogate for the vlookup function.
I hope this helps someone!

you can do so easily like this by hardcoding it in VR table:
=SUM(IFERROR(ARRAYFORMULA(VLOOKUP(A2:A, {{"A", 6};
{"B", 5};
{"C", 4};
{"D", 3};
{"E", 2};
{"F", 1}}, 2, 0)), ))
or you can use some side cells with rules:
=SUM(IFERROR(ARRAYFORMULA(VLOOKUP(A2:A, E2:F, 2, 0)), ))
alternatives: https://webapps.stackexchange.com/a/123741/186471

Related

Why is my COUNTA formula counting too high?

I know a similar but different question has been asked, but I don’t know how to use query or where to learn, so I figured I’d ask…
I’d like to use this formula:
=COUNTA(FILTER($L:$L,OFFSET($L:$L,0,1)="Draw",$L:$L=$E2))
L:L is a range of names, which it should test against E2:E, a range of unique names. I also need to ensure that the cell in the column to the right of each instance of the name in E2:E has a specific word in it (“Draw”). I tried using AND, but this just sets everything to 1, even when one of them has 3 draws. There are no losses, so the loss column should show up as all 0s, but it is instead 1s. The infuriating thing about this is that the draw column is all right for some reason. Picture below. For reference, Losses(as well as draws and wins)are the end goal.
image
COUNT is meant for counting numeric values, COUNTA for non empty cells. COUNTIF or COUNTIFS would probably be your best choice. Try with:
=COUNTIFS($L:$L,E2,OFFSET($L:$L,0,1),"Draw")

ARRAYFORMULA should be used only once in each formula or should it be used multiple times once for each need?

Sum the VLOOKUP results:
=ARRAYFORMULA(SUM(IFERROR(VLOOKUP(A1:A,B1:C,2,FALSE))))
Sum two cells:
=(Z1+Z2)
Sum two specific values from VLOOKUP:
=ARRAYFORMULA(SUM(IFERROR(VLOOKUP(G1:G2,H1:I,2,FALSE))))
Now I need to come up with an average of the three results:
=ARRAYFORMULA(
SUM(IFERROR(VLOOKUP(A1:A,B1:C,2,FALSE)))+
(Z1+Z2)+
SUM(IFERROR(VLOOKUP(G1:G2,H1:I,2,FALSE)))
)/3
But the faithful form would be:
=(
ARRAYFORMULA(SUM(IFERROR(VLOOKUP(A1:A,B1:C,2,FALSE))))+
(Z1+Z2)+
ARRAYFORMULA(SUM(IFERROR(VLOOKUP(G1:G2,H1:I,2,FALSE))))
)/3
Both will reach the same result, my question is, what is the most correct and safe way from the standards of those who work professionally with Google Sheet?
1 → Use only one ARRAYFORMULA call for the all the formula.
2 → Use multiple ARRAYFORMULA calls, one for each specific need.
Question reason:
I still haven't found risks of using a single ARRAYFORMULA in the beginning and doing everything else within it like =ARRAYFORMULA((...)+(...)) rather than =ARRAYFORMULA(...)+ARRAYFORMULA(...), but I not finding risks doesn't mean they don't exist.
One instance of ArrayFormula on the outside is both sufficient and the professional standard.
However, I must say that I don't understand your formula usage or intention from your posted example. You've got + between each element, which is redundant to SUM. And like ArrayFormula, you only need one outer SUM to sum all elements in your usage.

How to get only one of two repeating values

The Issue
In simple terms, I am trying to set a formula for an alternating pattern. The issue I keep running into is the fact that there are two alternating values, and Google Sheets doesn't like to repeat only one of those values without the other.
I have created an example sheet to demonstrate my issue. In Column A, a date is input. Column B and Column C then autofill with the day of the week and AM or PM respectively. Every other value in Column C alternates between AM and PM. I am trying to set it up so that the row is blank until a value in input in Column A. The issue comes when there is an odd number of Dates in Column A. Note that the alternating AM/PM pattern will never change.
What I've Tried
As seen in the image above, there are three main methods that I have tried. The data in C2:C8 is the desired result.
Method 1:
E2: =transpose(split({rept(join(";",{"AM";" "})&";",(roundup(counta(A2:A9)/2)))},";"))
F3: =transpose(split({rept(join(";",{"PM";" "})&";",(counta(A2:A9)/2))},";"))
These formulas work separately, and best represent what I am trying to accomplish, but I have not found a way to combine them to work together in one column.
Method 2:
H2: =transpose(split({rept(join(";",{"AM";"PM"})&";",(roundup(counta(A2:A9)/2)))},";"))
This is essentially the same as Method 1, but put into one formula. The issue here is that Google Sheets doesn't like to repeat half a number of times. So if the number of times to repeat (counta(A2:A9)/2) contains a half (i.e. 3.5), it will still round down to the nearest whole number.
Method 3:
J2: =ArrayFormula(TEXT(SEQUENCE(3),"")&{"AM";"PM"})
This one appeared most promising to me because when incrementing by one, it added one row, but I quickly ran into the issue where if I went over a sequence number of 2, it threw the error Array arguments to CONCAT are of different size.
References
I have used various search terms and websites to try to solve this, and have yet to find something that works. I may be missing something very simple, though, and hopefully this is a quick solution.
Example Sheet:
https://docs.google.com/spreadsheets/d/1I3EtptFLfDHpAQ8AR6Lwa01dSpJ3Cy8MTX1_OjHExSc/edit?usp=sharing
All my formulas are derived from the websites below:
REPT Function in Google Sheets
How to Repeat Multiple Columns N Times in Google Sheets
Delete everything in Col C (including the header) and place this formula in C1:
=ArrayFormula({"AM/PM"; IF(A2:A="",,IF(COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))=1,"AM","PM"))})
The COUNTIFS finds the number of matches for the date "up to this row" for every row. Since that count will (or should) only ever be a 1 or a 2, the IF makes easy work of assigning "AM" or "PM" accordingly.
If I understand correctly it is enough to use ISEVEN function to alternate by rows:
=ArrayFormula(IF(A2:A,CHOOSE(ISEVEN(ROW(A2:A))+1,"PM","AM"),))

How to use AVERAGEIFS within ARRAYFORMULA

I am trying to use AVERAGEIFS inside ARRAYFORMULA. Looking at other questions, I have come to the conclusion that it is not possible without using QUERY function.
My intention is to average the values of a column whenever they share the same ID.
I think this question comes pretty close to what I need, but I haven't been able to replicate and adapt its solution on my own sheet.
In this sheet I show the result I expect (I got it by dragging the formula). I've also reviewed the Query Language Reference, unsuccessfully.
Thanks a lot for your time and effort.
So the formula should be
=ArrayFormula(iferror(sumif(A2:A,A2:A,B2:B)/countif(A2:A,A2:A)))
Note that if there were any text values in the points column, this would still return a result (because count would be greater than zero) - you could instead use
=ArrayFormula(if(isnumber(B2:B),(sumif(A2:A,A2:A,B2:B)/countif(A2:A,A2:A)),""))
If you had a mixture of rows with text and rows with numbers for any ID, this would return a smaller result than the avg or average formula. This is a limitation of this method. You can't put an extra condition in (that column B has to contain a number) because you would need countifs and countifs isn't array-friendly. It still seems strange that AFAIK countif and sumif are the only functions out of this family that are array-friendly while countifs, sumifs, averageif etc. are not.
you can do:
=ARRAYFORMULA(IFERROR(VLOOKUP(A2:A; QUERY(A2:B; "select A,avg(B) group by A"); 2; )))

Google Spreadsheet vlookup with arrayformula PLUS multiplication of returned value(s)

I have my VLOOKUP with ArrayFormula working thanks to Adam's (AD:AM) brilliantly spelled-out solution (https://productforums.google.com/forum/#!searchin/docs/parallel$20lookup$20solution/docs/36A0epDlIdE/qnywZst0DioJ)
So in col J this is what I have:
=ArrayFormula(VLOOKUP(H2:H;ProductTable!A2:C;3*SIGN(ROW(H2:H));FALSE))
Works great. However, I would like to then take the returned value(s) from that formula in col J and multiply it against the Qty value(s) that are in row I.
Obviously I could add an extra helper column in col K...
=ARRAYFORMULA(I2:I*J2:J)
...but I was hoping to avoid adding yet another column to my Query results, especially since the returned results in col J have no visible purpose on the worksheet - they are meant to be used strictly for mathematical purposes only.
I've attempted multiple ways of slipping that formula into the ArrayFormula / VLOOKUP function in col J, but I've had no luck thus far.
Maybe it can't be done(?), or maybe I could just benefit from a fresh set of eyes looking at this issue.
Never mind. I'm not sure why this didn't appear to work when I had tried it before, but out of desperation I just walked away for a while, and then just re-tried everything again.
And this time I get the results that I was looking for:
=ArrayFormula(VLOOKUP(H2:H;Backend_Product!A2:C;3*SIGN( ROW(G2:G));FALSE )*I2:I)
(a bit of a DUH, so maybe I had originally referenced the wrong column or something)
The post you referenced is from 2011. I think that use case has been resolved since then.
I have a column using the formula =ArrayFormula(VLOOKUP(J$2:J,$C$2:$D,2,FALSE)). I created a check column with =VLOOKUP(J2,$C$2:$D,2,FALSE) which I dragged down a number of rows.
It appears that, in the array formula, J is being iterated just as it is in my dragged formula, while C:D are fixed. Both the original column and check column have the same result data so far as I've dragged the check column down.
Caveat: I did not do an exhaustive search or create a particularly clever test case. I just eyeballed a good heaping of rows of actual data.

Resources