Google spreadsheets query range get item to the right - arrays

I am trying to get data from a "flexible" sheet.
The idea is to add in a row a string, and next to it add a formula that from another sheet, it looks up the string and returns the cell just at the right.
Example:
Data Sheet: DataCollection
| | A | B | C | D |
| 1 | Pepper | 2 | Sugar | 5 |
| 2 | Carbon | 3 | Toy | 34 |
so if in my other sheet, the "Summary" I add to A1 Sugar I would like to see in A2 a 5.
What I have tried so far
VLOOKUP function
=VLOOKUP(A1,'DataCollection'!A2:B&'DataCollection'!C2:D,1,false)
didn't work! I keep receiving a parse formula error.
QUERY function
=QUERY(DataCollection, "SELECT B WHERE A = A1")
that does not work either, I need many different Named Ranges and add a different query for each of them.
Here is a test spreadsheet in case it explains better that my wall of text:
https://docs.google.com/spreadsheets/d/15L5nPGfZ8OXS5Rhl3PdIVhtF7D3QzerkARskflDiJL4/edit?usp=sharing

you almost had it. try:
=VLOOKUP(A1, {'DataCollection'!A2:B; 'DataCollection'!C2:D}, 2, 0)
for an array use:
=ARRAYFORMULA(IFERROR(VLOOKUP(A1:A, {'DataCollection'!A2:B; 'DataCollection'!C2:D}, 2, 0)))

Related

COUNTIF over multiple non contiguous ranges and with one condition in Google sheets

There's a google sheet with content like this:
| A | B |
------------------------------
1 | Banana | Brazil |
2 | Banana | Uruguay |
3 | 2022-02-28 by flight | // Cell spans over two columns
4 | Pineapple | Paraguay |
5 | Banana | Brazil |
6 | 2022-03-08 by Ship | // Cell spans over two columns
7 | Coconut | Uruguay |
8 | Banana | Peru |
I need the amount of non "Brazil" entries of certain lines (excluding the combined ones). It should be st like this =COUNTIF(INDIRECT({B1:B2;B3:B5;B7:B9});"<>Brazil") which doesn't work. Is there an applicable way without using a series of COUNTIFS in which the condition has to be set for every single range?
If this is not possible that way then QUERY might be helpful (see Google Sheets Query Non-Contiguous Range). Then how to obtain the number of the results?
Thank You very much.
perhaps you overthinking it:
=COUNTIF(B:B; "Brazil")
update:
=COUNTIFS(B:B; "<>Brazil"; B:B; "<>")
You don't even need INDIRECT: Just =COUNTIF({B1:B2;B3:B5;B7:B9},"<>Brazil") will work.
To create skips, use FILTER+SEQUENCE.
For eg, This skips row 3 and 6:
=LAMBDA(ar,len,COUNTIF(FILTER(ar,SWITCH(SEQUENCE(len),3,0,6,0,1));"<>Brazil"))(B1:B9,ROWS(B1:B9))
Or with a named function:
SKIP(arr,rows_to_skip):
=FILTER(arr,
MAP(SEQUENCE(ROWS(arr)),
LAMBDA(n, AND(MAP(rows_to_skip,LAMBDA(s, s<>n))))
)
)
Then,
=COUNTIF(SKIP(B1:B9,{3,4,6}),"<>Brazil")
Skips row 3, 4 and 6 in the array B1:B9

Need Google Sheets Array Formula To Return Just The Cell(s) > 1

I'm looking to list duplicate values from another tab on my sheet. I have the formula below and it's almost complete. I need help with the formula only returning duplicate cells and how many occurrences.
Right now it will list out every cell in the column followed the number of occurrences in the next column to the right. I want it to show me just the cells where the number is greater than 1.
Any help would be appreciated!
=IFNA(ArrayFormula(
{
unique(filter(Completed!E2:E,Completed!E2:E<>"")),
COUNTIF(filter(Completed!E2:E,Completed!E2:E<>""),unique(filter(Completed!E2:E,Completed!E2:E<>"")))
}),"")
Here's an example of what it shows currently:
| 12345 | 1 |
| 01234 | 1 |
| 56789 | 2 |
Here's what I'd like it to return instead:
| 56789 | 2 |
try:
=IFNA(QUERY(INDEX(
{UNIQUE(FILTER(Completed!E2:E, Completed!E2:E<>"")),
COUNTIF(FILTER(Completed!E2:E, Completed!E2:E<>""),
UNIQUE(FILTER(Completed!E2:E, Completed!E2:E<>"")))}),
"where Col2>1"))
or just:
=QUERY(QUERY(Completed!E2:E,
"select E,count(E) group by E"), "where Col2 > 1", 0)

Comparing Multiple Values with AND using ARRAYFORMULA

As part of a larger formula I need to use an AND statement to compare 2 columns. An example is a table like this:
| A | B |
|---|---|
| 1 | 4 |
| 2 | 5 |
| 3 | 6 |
The formula I'm trying is pretty simple. Basically just ARRAYFORMULA(AND(A1:A>2, B1:B>3)). My expectation is the result would be
False
False
True
But I just get False on a single line. Can you use AND in an ARRAYFORMULA?
AND is not supported in ARRAYFORMULA
do:
=ARRAYFORMULA((A1:A>2)*(B1:B>3))

ArrayFormula in google sheets SUM if values match criteria

Ok, so I've been stuck on this for a while now trying to create an array formula that will do the following:
Let's pretend the data I'm working with appears as such:
SHEET 1 SHEET 2
| A | B | C | D | E | F | A | B |
1 | Up | Down | Down | Up | Down | 1 | Red | $50 |
2 | Red | Green| White| Black | Blue | 2 | Green | $100 |
3 | $50 | $200 | $15 | Hidden| | 3 | White | $10 |
4 | | $200 | | Hidden| $100 | 4 | Black | $70 |
5 | $50 | $200 | $15 | Hidden| | 5 | Blue | $100 |
Using that data, I need a formula in column F (that I can copy and paste for subsequent cells in the column).
My actual sheet has about 45 columns across and about 25 rows down with sensitive financial information, so I'm using pretend data above on a smaller scale.
So the formula should look something like this (this would be posted in column F of Sheet 1). It needs to check if $A$1:$E$1="Up" (disregard anything that is not "Up") in addition, IF that array = "up" then check to make sure $A3:$E3>$0 (I intend to exclude blank values and "hidden" values), then IF those 2 criteria are met, then return the corresponding value from $A$2:$E$2 and use that as a search key in a lookup formula (HLookup, Vlookup, offset+match, etc). So for example, the Formula in F3 would match those 2 criteria (which would be A2 [red] and then sum together all of the matching values - in this case, there is only 1 matching value, but usually there will be 3-5).
Attempt 1:
=ArrayFormula(SUM(OFFSET('Sheet 2'!$B$1,MATCH(IF(AND($A$1:$E$1="Up",$A3:$E3>"$0"),$A$2:$E$2,""),'Sheet 2'!$A$1:$A$5,0),0)))
Attempt 2:
=ArrayFormula(SUM(IF(AND($A$1:$E$1="Up",$A3:$E3>"$0"),OFFSET('Sheet 2'!$B$1,MATCH($A$2:$E$2,'Sheet 2'!$A$1:$A$5,0),0),"")))
My first attempt attempts to create an array for the Match search key that uses the IF statements to determine the results to be used in the match condition (In the example above, the only result would be "Red", but in the actual sheet, it could be an array like "Red, Blue, Yellow").
My second attempt attempts to first determine which cells we are using (based on IF conditions), then if the cell matches the IF then we will OFFSET the particular cell.
The errors I have noticed is that match doesn't seem to like my array in the search key. The first attempt results with an error saying it cannot find " in the range. (I guess I don't understand why it was searching for that...?). The 2nd attempt seems to fail because it uses the ENTIRE range ($A$2:$E$2), instead of only the cells that match the criteria.
I have tried such things as: VLookUp, HLookUp, LookUp (but it is not a sorted list). I've tried some other weird tactics, but non seem to give me what I need.
So far, the best I have come up with is:
=ArrayFormula(SUM(IF($A$1:$E$1="Up",IF($A3:$E3>"$0",0,OFFSET('Sheet 2'!$B$1,MATCH($A2:$E2,'Sheet 2'!$A$1:$A$5,0),0)),0)))
In my sheet, it found 3 cells that matched the criteria, but instead of adding Cell 1 value, Cell 2 value, and Cell 3 value, it just added Cell 1 value 3 times. So it seems it was only using $A2 (the first option in the range for the match search key) and repeated it for every cell that matched.
I have also tried to include the Filter function (instead of IF) to eliminate cells that do not have "up" as their value ($A$1:$E$1).
Use SUMPRODUCT:
=SUMPRODUCT(($A$1:$E$1="Up")*(VLOOKUP($A$2:$E$2,'Sheet 2'!$A$1:$B$5,2,FALSE))*($A3:$E3>0)*(ISNUMBER($A3:$E3)))

How can I create a conditional SUMPRODUCT formula for a horizontal set of timeseries data

I'm attempting to use sumproduct on 2 sets of timeseries data that is formatted in the following way:
Array 1
| 01/01/2016 | 02/01/2016 | ...
Stock1 | Price1a | Price1b |
Stock2 | Price2a | Price2b |
Array 2
| 01/01/2016 | 02/01/2016 | ...
Stock1 | Volume1a | Volume1b |
Stock2 | Volume2a | Volume2b |
Such that for a given date, the sumproduct formula will perform price * volume for all stocks in that date.
Example:
For 01/01/2016, the formula will return Price1a * Volume1a + Price2a * Volume2a.
Appreciate any help or questions in case it is unclear.
I put the ranges on two different sheets and used this formula:
=SUMPRODUCT((INDEX($A:$C,2,MATCH(E1,$1:$1,0)):INDEX($A:$C,MATCH("ZZZZ",A:A),MATCH(E1,$1:$1,0)))*(INDEX(Sheet3!$A:$C,2,MATCH(E1,Sheet3!$1:$1,0)):INDEX(Sheet3!$A:$C,MATCH("ZZZZ",Sheet3!A:A),MATCH(E1,Sheet3!$1:$1,0))))
The Index/Match define the extents of the arrays. The tables must have the same number of rows for this to work and the Prices to Volumes must be in the same order.
In the first and third INDEX there is ,2,. The 2 is the starting row. If your starting row is different than change it to your starting row number.
The Match("ZZZ"... will find the last row with text in it, so make sure there is nothing below the tables.
Also all the $C references need to change to the extents of your data. It can be larger than your data without detriment, so if you just want to set it to something like $CC it will be fine.

Resources