I have the yellow table shown below, and I'm trying to get the blue table, which aggregates columns B:F by value, and then counts the number of 'x' symbols for each row value of column A.
Is there some basic SQL/array magic formula to get this, please? There must be.
Use this new functions formula
=BYROW(B2:4, LAMBDA(v, COUNTIF(v, "=x")))
Used:
BYROW, LAMBDA, COUNTIF
v is the array_or_range
Update
={ A2:A4, BYROW(B2:4, LAMBDA(vv, COUNTIF(vv, "=x")))}
For fun
Update 02
=ArrayFormula(TRANSPOSE(QUERY({
QUERY(TRANSPOSE(IF(A1:4<>"x",A1:4,1)),
" Select * Where Col1 is not null ", 1)},
" Select (Col1),sum(Col2),sum(Col3),sum(Col4) Group by Col1 ", 1)))
Related
I'm trying to dynamically offset data of multiple rows to match the header column in Google Sheets. The first tab contains data of multiple fruits and how many are harvested on a particular day. Each fruit starts harvesting on different dates, and the dates might not be continuous.
The second tab, "Fruit bank", shows how many fruits are harvested in total for each day. Column D is a continuous set of dates. In cell E1, a QUERY formula dynamically pulls the names of the fruits so whenever a new fruit is added, it shows up here as well. In cell E2 and the rest of the row, I use VLOOKUP formulas to pull the data from the first tab. What I need help with is to write a formula on cell E2 that expands to the rest of the row so I don't have to manually type in the lookup range every time a new fruit is added.
Also, I suspect there're better functions to use than the VLOOKUP because the way VLOOKUP pulls data is very slow. I could literally see it loading even with this small dataset.
Yellow cells contain formula.
I appreciate anyone who can take a look at my spreadsheet (linked below) and see what's the best solution for this. The 3rd tab is editable.
https://docs.google.com/spreadsheets/d/14GeJKgxadInNWVVyft2gilae7HOIEvKXRop-Kz_On-Q/edit#gid=53523977
Thanks! J
Use filter(), like this:
=arrayformula(
ifna(
vlookup(
$D2:$D,
{
filter(
'Fruits data'!$B2:$AA,
'Fruits data'!$A1:$Z1 = E1
),
filter(
'Fruits data'!$A2:$Z,
'Fruits data'!$A1:$Z1 = E1
)
},
2, false
)
)
)
See your sample spreadsheet.
you can try either. added solutions here and here
=BYROW(D2:D,LAMBDA(dx,IF(dx="",,BYCOL(E1:1,LAMBDA(ex,IF(ex="",,XLOOKUP(dx,FILTER('Fruits data'!1:46,COLUMN('Fruits data'!1:1)=MATCH(ex,'Fruits data'!1:1,0)-1),FILTER('Fruits data'!1:46,'Fruits data'!1:1=ex),)))))))
OR
=MAKEARRAY(COUNTA(D2:D),COUNTA(E1:1),LAMBDA(r,c,XLOOKUP(INDEX(D2:D,r),FILTER('Fruits data'!1:46,COLUMN('Fruits data'!1:1)=MATCH(INDEX(E1:1,,c),'Fruits data'!1:1,0)-1),FILTER('Fruits data'!1:46,'Fruits data'!1:1=INDEX(E1:1,,c)),)))
try this with dates:
=ARRAYFORMULA(QUERY({
FLATTEN(FILTER('Fruits data'!A2:100, ISODD(COLUMN('Fruits data'!A2:2)))),
FLATTEN(FILTER('Fruits data'!A2:100, ISEVEN(COLUMN('Fruits data'!A2:2)))),
FLATTEN(IF(FILTER('Fruits data'!A2:100, ISEVEN(COLUMN('Fruits data'!A2:2)))="",,
FILTER('Fruits data'!A1:1, ISEVEN(COLUMN('Fruits data'!A2:2)))))},
"select Col1,sum(Col2) where Col2 is not null group by Col1 pivot Col3"))
which could be simplified:
=ARRAYFORMULA(LAMBDA(x, QUERY({
FLATTEN(FILTER(x, ISODD(COLUMN(X)))),
FLATTEN(FILTER(x, ISEVEN(COLUMN(x)))),
FLATTEN(IF(FILTER(x, ISEVEN(COLUMN(x)))="",,
FILTER(OFFSET(x, -1,,1), ISEVEN(COLUMN(x)))))},
"select Col1,sum(Col2) where Col2 is not null group by Col1
pivot Col3 label Col1'Date'"))('Fruits data'!A2:100))
I have a Google sheet which has columns with the same name and there are different values under each column. I want to count the same value that appear under the same column name.
1
2
3
1
2
R
B
C
R
D
D
C
R
B
D
For example, I would like to get the number "R" that appear under column "1", so I would expect a count of 2 for "R" appearing under columns 1.
Here is a link to Google Sheet with actual data.
I have tried countif and countifs in Google Sheets, but can't figure out how to get the count right based on column name.
Try this formula, it outputs an array which shows how many of each letters are contained in each column name:
=LAMBDA(NUMBERS,LETTERS,
LAMBDA(UNUM,ULET,
{
{"",TRANSPOSE(UNUM)};
{ULET,
MAKEARRAY(COUNTA(ULET),COUNTA(UNUM),LAMBDA(ROW,COL,
COUNTIF(FILTER(LETTERS,NUMBERS=INDEX(UNUM,COL)),INDEX(ULET,ROW))
))
}
}
)(UNIQUE(FLATTEN(NUMBERS)),UNIQUE(FLATTEN(LETTERS)))
)($A$1:$AE$1,$A$2:$AE$18)
Assume that your sample datarange is A1:AE18.
apply UNIQUE() and FLATTEN() to A1:AE1, to get the unique entries of column names.
apply UNIQUE() and FLATTEN() to A2:AE18, to get the unique entries of data.
use LAMBDA() to name the dataranges and output of step 1 & 2 as:
NUMBERS (=A1:AE1),
LETTERS (=A2:AE18),
UNUM (=UNIQUE(FLATTEN(NUMBERS))),
ULET (=UNIQUE(FLATTEN(LETTERS))).
create Arrays with {}, which...
1st column's value is a blank, followed by TRANSPOSE(UNUM) in the row,
1st row's value is a blank, followed by ULET in the column.
inside the above said range, use MAKEARRAY() to create results.
MAKEARRAY() set an array by defining the length of ROW and COL, which we uses...
COUNTA(ULET) as the number of rows and,
COUNTA(UNUM) as the number of columns.
inside MAKEARRAY(), you also need a LAMBDA() to apply what to do with each CELL of the new created array, each CELL is accessed by the ROW and COL index.
in our case, we set up the row and col number of the new array using ULET and UNUM. Therefor, the index of each CELL of the new array will be equal to the index of each value inside ULET and UNUM, we can than take that as reference and use COUNTIF() with FILTER() to calculate the number of repeats of each letter in each column name.
You can try this:
= ARRAYFORMULA(
query(
query(
SPLIT(TRANSPOSE(SPLIT(
QUERY(
TRANSPOSE(
QUERY(
TRANSPOSE(
IF(Original!A2:AE18<>"",
"😊"&Original!A1:AE1&"♥"&Original!A2:AE18, )
),,999^99)
),,999^99),
"😊")),
"♥"),
"Select Col1,Col2,count(Col2) group by Col1,Col2"),
"Select max(Col2),Col3 group by Col2,Col3 pivot Col1")
)
Note: (Got inspired by player0 useful answers)
Output:
We can read from the table that: R is appearing 40 times under the column named '1', 24 times under the colum named '2', etc...
The following is a more compact approach than the previous answers:
=arrayformula(query(split(flatten(A1:AE1&"|"&A2:AE18),"|"),"select Col2,count(Col2) group by Col2 pivot Col1"))
N.B. I'm assuming the order of the grouped values in each column is irrelevant, so the QUERY default of lexicographical ordering is fine.
I'm pretty new with ArrayFormula, have been trying but sometime the formula works, sometimes does not. What I'm trying to do is the combination of ArrayFormula, Countif for searching partial text.
As shown in the worksheet below, there are 10 subjects (column A), each subject has at least one of 4 samples (A,B,C,D) summarized as a string (column B). What I'm trying to do is to find which subject has sample A or B or C or D.
I have tried single formula for each sample, eg cell D3
=IF(COUNTIF($B3,"*"&$D$2&"*")>0,$A3,"")
it returns the correct results. However, when I try arrayformula in cell I3,
=arrayformula(IF(COUNTIF($B3:B,"*"&$D$2&"*")>0,$A3:A,""))
The answers are weird. For example: Subjects (Gamma, Zeta, Eta, Theta) who don't have the sample "A" are shown to have sample "A". And this applies to sample B,C,D too
Not sure what went wrong in here. Here is the link to the worksheet
I wouldn't use Countifs or an array formula. Use filter instead. Put this formula in cell i3.
=Filter(if(REGEXMATCH(B3:B,$D$2),A3:A,""),B3:B<>"")
try:
=INDEX(QUERY(IFERROR(TRIM(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B3:B, ","))="",,
SPLIT(B3:B, ",")&"×"&A3:A)), "×"))),
"select max(Col2) where Col2 is not null group by Col2 pivot Col1"))
or use in row 2 if you want to sort it as in your example:
=INDEX(IFNA(VLOOKUP(A2:A, QUERY(IFERROR(TRIM(SPLIT(FLATTEN(
IF(IFERROR(SPLIT(B3:B, ","))="",,SPLIT(B3:B, ",")&"×"&A3:A)), "×"))),
"select Col2,max(Col2) where Col2 is not null group by Col2
pivot Col1 label Col2'Subjects'"), {2,3,4,5}, 0)))
You can accomplish all four columns of results with a single formula.
Delete all formulas from I3:L3.
Place the following formula into I3:
=ArrayFormula(IF(REGEXMATCH(B3:B,I2:L2),A3:A,))
In plain speech, this read "If anything in B3:B matches a value found in I2:L2, return A3:A in the matching columns(s) at the matching row(s); if not, return null."
I have a Google Sheet tab ("Data") with columns A:Z and need to create a tab that pulls in all rows from ("Data") if columns A:N are NOT null in any of these columns.
I've tried query importrange, query+importrange, but can't crack the code:
Attempt #1: =IF(isblank("Data!A1:N1"),Importrange("https:...
","Data!A1:Z100"),,))
Attempt #2: =QUERY(IMPORTRANGE("https:...",
" Data!A1:Z1"), "select Col1 where Col1 is not Null", 0). <<this pulls the data in for that specific cell but I can't drag it down or across to get the other rows to populate
Help!
join columns A:N into one column and use TRIM
=INDEX(TRIM(FLATTEN(QUERY(TRANSPOSE(IMPORTRANGE("url", "Data!A1:N")),,9^9))))
next, add this column to the range A:Z as the last column of array {} and QUERY it
=INDEX(ARRAY_CONSTRAIN(QUERY({
IMPORTRANGE("url", "Data!A1:Z"), TRIM(FLATTEN(QUERY(TRANSPOSE(
IMPORTRANGE("url", "Data!A1:N")),,9^9)))},
"where Col27 is not null"), 9^9, 26))
I am trying to get the some of all values in row B that contain a certain value in row A. Pretty simple problem I guess.
Here is my query:
=QUERY('Sheet1'!$A$16:D, "Select sum(D) Where C contains '"&C5&"' ", -1)
But what that gives me is the actual word "sum" in all the fields where C contains the value.
So I get a lot of "sum" in almost all my rows.
Did the "sum" statement change for queries in google spreadsheets?
It looks like you are using more than one query formula: apparently there is a column with query, each referring to a cell such as C5. In this case there is no room for column label "sum" that the formula wants to insert: the output must be a single cell. Solution: change the column label to empty string with label sum(D) ''.
=QUERY('Sheet1'!$A$16:D, "Select sum(D) Where C contains '"&C5&"' label sum(D) ''", -1)