Using query in google sheet to call certain rows - google-sheets

Hi everyone,
I want to get all the rows where A=1, A=2. The query is very simple but I'm not sure why there is no output. I guess is the problem of the format for column A but I'm not sure what should I change in my formula so that the formula can work. Appreciate any help or advice!

A can't be both: 2 and 1. Instead try
=query(A3:C, "Where A = 2 or A = 1", 0)

It should have been OR not AND - it's impossible for any row in a particular column to have two values simultaneously:
=query(A3:C,"select * where A=1 or A=2",0)

QUERY() is good choice. You can also use FILTER() function like-
=FILTER(A3:C,A3:A>=1,A3:A<=2)

Related

EXCEL/Google Sheets. Best way to SUM multiple VLOOKUP?

could you guys help me with a project. I was able to find a solution for my problem and the formula looks like this:
=IFERROR(VLOOKUP(E4;A8:B13;2;FALSE);0)+IFERROR(VLOOKUP(F4;A8:B13;2;FALSE);0)+IFERROR(VLOOKUP(G4;A8:B13;2;FALSE);0)
I have a category (e.g. Fruits) and need to import a sheet with different kind of fruits and non fruits. I use keywords which define what is a fruit and what not. I need to SUM all values which match to a keyword. My formula works but it will be more and more work when i need to add more keywords.
Are there a better way to realise this?
I build this example sheet for better understanding : )
Spreadsheet link
Thank you in advance : )
use:
=SUMPRODUCT(IFNA(VLOOKUP(E4:G4; A8:B13; 2; )))
you can even use E4:4 or E4:G5 or E4:5
row-wise it would be:
=INDEX(BYROW(E4:G5; LAMBDA(x; SUM(IFNA(VLOOKUP(x; A8:B13; 2; ))))))
use this
=ArrayFormula(SUM( IFERROR( VLOOKUP(E4:G4;A9:B;2;0))))
Usign sumif
=ArrayFormula(SUM(SUMIF(A9:A;"="&E4:G4;B9:B)))
Xlookup
=ArrayFormula(SUM(XLOOKUP(E4:G4;A9:A14;B9:B14;"";1)))
Us XLOOUP instead of IFERROR(VLOOKUP()) can shortens the formula.
=SUMPRODUCT(XLOOKUP($E4:4,$A$9:$A,$B$9:$B,0))

Can I use an arrayformula with a split arrayformula inside it?

Trying to break apart rows containing numbers like "198,183,158,315,274" by their comma, and then average them out and divide them by a singular number; using arrayformula. It only produces one row of result and it's incorrect though?
Here is my test sheet, editable
Thanks for any help.
try:
=ARRAYFORMULA({"Average"; IF(A4:A="",,
IFNA((MMULT(1*IFERROR(SPLIT(INDIRECT("A4:A"&
MAX(IF(A4:A="",,ROW(A4:A)))), ",")),
ROW(INDIRECT("A1:A"&COLUMNS(SPLIT(A4:A, ","))))^0)/
(1+LEN(REGEXREPLACE(A4:A&"", "[0-9\. ]", ))))/B1))})
spreadsheet demo
Another solution:
=ArrayFormula({"Average";(ArrayFormula(mmult(N(array_constrain(ArrayFormula(IFERROR(SPLIT(A4:A8,","))),MATCH(2,1/(A4:A8<>""),1),5)),sequence(Columns(ArrayFormula(IFERROR(SPLIT(A4:A8,",")))),1)^0)/mmult(N(array_constrain(if(ArrayFormula(IFERROR(SPLIT(A4:A8,",")))>0,1,0),MATCH(2,1/(A4:A8<>""),1),5)),sequence(columns(ArrayFormula(IFERROR(SPLIT(A4:A8,",")))),1)^0)))/$B$1})

What method can filter two pairs of data using filter() or other recommanded way?

I want to use filter() to search from B2:B corresponding to
the range like F2:F3
I use =filter(A2:A,B2:B = F2:F3),but there is just one data Jack
I know this example can using FILTER to do the same thing like this.
But now if there is a lot of data, it may not meet my needs.
Then I use =ArrayFormula(filter(A2:A,B2:B = F2:F3))
And it's not useful, too. (I don't know how to use arrayformula() methods)
What should I fix the formula and parameters?
Use COUNTIF to check whether the values B2:B are included in the range F2:F3:
=FILTER(A2:A,COUNTIF(F2:F3,B2:B))
try:
=FILTER(A2:B; REGEXMATCH(B2:B; TEXTJOIN("|"; 1; F2:F)))
or:
=QUERY(A2:B; "select A,B where B matches '"&TEXTJOIN("|"; 1; F2:F)&"'"; 0)

How can I search a column with alphanumeric entries and return a 1 for unique with an array formula?

I need something that auto-fills for however many rows are in the spreadsheet, but I'm not sure if an array is the best way.
In my example below, I want Column C to show a 1 if the corresponding entry in Column A is unique, and a 0 if it isn't.
I had hoped it would be as easy as using ARRAYFORMULA(IF(UNIQUE(A1:A),1,0)), but forgot that IF wouldn't work with the text.
Here's my example with the most recent formula I tried.
Thank you!
Please use the following formula:
=ArrayFormula(IF(LEN(A1:A)<>0,
IF(COUNTIF(A1:A,A1:A)>1,0,1)
,""))
Functions used:
COUNTIF
ArrayFormula
LEN
You can combine ARRAYFORMULA and IF with IFERROR and COUNTIF
Sample:
=ARRAYFORMULA(IF(IFERROR(COUNTIF(A1:A,A1:A)=1,0)=TRUE,1,0))

arrayformula sum in Google spreadsheet

How do you arrayformula() a sum() such as:
=sum(A1:H1)
I need to go down 1000 rows.
Another option:
=ArrayFormula(SUMIF(IF(COLUMN(A1:H1),ROW(A1:A1000)),ROW(A1:A1000),A1:H1000))
Of the two answers that work, Jacob Jan Tuinstra and AdamL, Jacob gives a better answer. Jacob's runs faster and is easier to remember.
However, why use crazy formulas when it is much easier to use Google Sheets to the fullest?
=ARRAYFORMULA(A2:A+B2:B+C2:C+D2:D+E2:E+F2:F+G2:G+H2:H)
In the foregoing formula, you can use named ranges instead of the ranges by reference.
=ARRAYFORMULA(range1+range2+range3+range4+range5+range6+range7+range8)
As well, you can sum across rows that span sheets rather than being stuck working with columns within the same sheet.
To fix the formula to block returning zeros for blank rows, use this:
=arrayFormula(if(isNumber(A2:A),A2:A+B2:B+C2:C+D2:D+E2:E+F2:F,G2:G,H2:H))
See: See Ahab's answer on Google Forums
For a cool sum accumulation formula, see Otávio Alves Ribeiro's answer on Google Forums
This is what you are looking for:
=MMULT(A1:H1000,TRANSPOSE(ARRAYFORMULA(COLUMN(A1:H1000)^0)))
See this answer on Web Application I gave: https://webapps.stackexchange.com/a/53419/29140
Note: tried it on the new Google Spreadsheet, without succes.
with new functions in google sheets (since 20 Sep, 2022) all you need is:
=BYROW(A:H; LAMBDA(x; SUM(x)))
Summing A-H horizontal and running down for 523 lines:
=ARRAYFORMULA(iferror(mmult(A1:H523;TRANSPOSE(column(A1:H1))^0)))
if I look at this formula I really think the following might be simpler. Add this to Tools > Script Editor:
function row_sum(range_to_sum_per_row) {
var result_column = [];
for (var r = 0; r < range_to_sum_per_row.length; r++) {
var row_sum = parseFloat(0);
for (var c = 0; c < range_to_sum_per_row[r].length; c++) {
row_sum += range_to_sum_per_row[r][c] || 0;
}
result_column.push([row_sum]);
}
return result_column;
}
use this like so for performance reasons, where C:H is the range you want to sum up and A:A is a column that does not contain an empty string:
=row_sum(filter(C2:H, len(A2:A)>0))
If you want to be able to add rows and sum to the last row for all values in A1:H, you can use:
=ArrayFormula(SUMIF(IF(COLUMN(A1:H1),ROW(A1:A)),ROW(A1:A),A1:H))
Alternatively, if you want be be able to add rows and columns to the spreadsheet and sum to the last of both this can also be done. Paste the following code into any cell and it will create a column of summed values for all cells in each row below and to the right of pasted cell:
=arrayformula(SUMIF(IF(COLUMN(indirect(concatenate(REGEXREPLACE(address(row(),column()+1),"[^[:alpha:]]", ""),VALUE(REGEXREPLACE(address(row(),column()),"[^[:digit:]]", "")),":",VALUE(REGEXREPLACE(address(row(),column()),"[^[:digit:]]", ""))))),ROW(indirect(concatenate(REGEXREPLACE(address(row(),column()+1),"[^[:alpha:]]", ""),VALUE(REGEXREPLACE(address(row(),column()),"[^[:digit:]]", "")),":",REGEXREPLACE(address(row(),column()+1),"[^[:alpha:]]", ""))))),ROW(indirect(concatenate(REGEXREPLACE(address(row(),column()+1),"[^[:alpha:]]", ""),VALUE(REGEXREPLACE(address(row(),column()),"[^[:digit:]]", "")),":",REGEXREPLACE(address(row(),column()+1),"[^[:alpha:]]", "")))),indirect(concatenate(concatenate(REGEXREPLACE(address(row(),column()+1),"[^[:alpha:]]", ""),VALUE(REGEXREPLACE(address(row(),column()),"[^[:digit:]]", "")),":"),address(rows($A:$A),columns($1:$1))))))
Using Query
=INDEX(TRANSPOSE(
QUERY(TRANSPOSE(FILTER(A2:H,A2:A<>"")),
"select sum(Col"&JOIN("), sum(Col",SEQUENCE(COUNTA(A2:A)))&")",0)
),,2)
notes:
generating query string on the fly
Using DSUM:
=ARRAYFORMULA(DSUM(
TRANSPOSE(FILTER({A2:A,A2:H},A2:A<>"")),
SEQUENCE(COUNTA(A2:A)),{IFERROR(1/0);IFERROR(1/0)}))
notes:
{IFERROR(1/0);IFERROR(1/0)} is to make zero creteria for DSUM.
{A2:A,A2:H} -- added fake column for DSUM to mimic header column.
may be able to cahnge the formula into DMAX or DAVERAGE
Answer similar to #adamL suggestion, but removing the internal if.
=ArrayFormula(
sumif(ROW(A1:A10)*COLUMN(A1:H1)^0,ROW(A1:A10),A1:A10)
)
In this case I use ROW(A1:A10) * COLUMN(A1:H1)^0 to generate the row number matriz. To understand how it works, you can test just this part in your Google sheets:
= ArrayFormula(ROW(A1:A10) * COLUMN(A1:H1)^0)
So, with your row matrix, sumif can operate for each line, is the line has the matched row number, it will be summed up.
Let us not complicate this.
Simply put parenthesis in each of the array in the range.
=arrayformula( Sum( (A1:A):(H1:H) )
This spans not only upto 1000 rows but upto infiinity.
If you really want to limit then go
=arrayformula( Sum( (A1:A1000):(H1:H1000) )

Resources