Google Sheet | Excel | Array Formula + CountIf + Partial Text Problem - google-sheets

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."

Related

How to find the highest N values in each group in Google Sheets

I want to use a formula to find the highest N values in each group in a Google Spread Sheets.
I tried this formula from infoinspired.com (credit to Prashanth):
=ArrayFormula(QUERY({SORT(A2:B;1;true;2;false);IFERROR(row(A2:A)-match(query(SORT(A2:B;1;true;2;false);"Select Col1");query(SORT(A2:B;1;true;2;false);"Select Col1");0))};"Select Col1,Col2 where Col3<3"))
But all it return is an Array_Literal error:
This is what I expect:
What is wrong with it?
You have to put a comma, not a semi colon before IFERROR. It's creating two columns, one twice larger than the other instead of three columns ;)
=ArrayFormula(QUERY({SORT(A2:B,1,true,2,false),IFERROR(row(A2:A)-match(query(SORT(A2:B,1,true,2,false),"Select Col1"),query(SORT(A2:B,1,true,2,false),"Select Col1"),0))},"Select Col1,Col2 where Col3<3"))
alternative formula:
=QUERY(SORT({{A2:B}\MAP(A2:A;B2:B;lambda(ax;bx;IFERROR(Rank(bx;Filter(B$2:$B;A$2:$A=ax);0);IFERROR(1/0))))};1;0;3;1);"Select Col1, Col2 Where Col3<3")
-

GoogleSheets SumIFS in array formula

I'm trying to convert my Sumifs formula into an array formula
(so that the sheet auto updates)
I'm struggling to do so though
I tried using sumif formula and combining the columns, but I can't get it to work right.
I created this simplified version of my data
https://docs.google.com/spreadsheets/d/1qnoY_MZTup8XLLWCu61-Ifiv8dak6QRc1CxsGKpmFWE/edit?usp=sharing
(I know there's only two rows, but in my actual data there's a lot more and the number of brands vary)
I've shown in column C on sheet 2 how the sumifs formula works.
But ideally I'd like an array formula in cell B2 that will populate all the rows for me (instead of having to fill down the sumifs each time)
No need to use arrayformula for this. You can simply use query:
Formula (A2):
=query({Sheet1!A2:D}, "select Col1, sum(Col4) where Col1 is not null and Col2 is null and Col3 is null group by Col1 label sum(Col4) ''")
This will populate both A and B.
Output:

How to count the number of occurences of a string across a range in google sheets?

I've tried combining vlookup and countif functions - match function. But none of them are the actual solution to the problem given how they are supposed to work. I've gone through their documentation. Below is a snippet of the data I have. I have to count how many time H3, H4 and so on occur in the range from A to G columns - these columns are of unequal length. Please help me understand how I can do this.
paste in i3 cell
=ARRAYFORMULA(IFNA(VLOOKUP(H3:H, QUERY(FLATTEN(A3:G),
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''"), 2, 0)))

ArrayFormula, SumProduct and Google Sheets returning only 1 value

I have two sheets. I'm using SUMPRODUCT to sum a column based on a matching string.
=SUMPRODUCT(--(skus_campaign!A:A=A2),skus_campaign!D:D)))
This works exactly as expected, if I drag the formula to the rows below.
If I attach ARRAYFORMULA and and IF test to see if there's a blank value, it won't work.
=ARRAYFORMULA(IF(ISBLANK(A2:A), " ", SUMPRODUCT(--(skus_campaign!A:A=A2),skus_campaign!D:D)))
Am I missing something here? Is there an easier way to accomplish this while still using ARRAYFORMULA to grow and shrink the column based on the values in column A?
EDIT: Here's the link to the example Google Sheet. Column "D" under the "Data" sheet is the issue.
Try in E1
={"COGS"; ArrayFormula(if(len(A2:A), vlookup(A2:A, query(skus!A:D, "Select A, sum(D) where A <>'' group by A"), 2, 0),))}
and see if that produces the desired result. If it does, clear all values and formulas in column D and enter the formula in D1.

Using indirect function to refer to a concatenated import ranges for query function

Below is my query function in its original form:
=ArrayFormula(query({importrange("1yqTUmJcL6YxgOpfHS5Pt9nYnmpiqN3tUPQP7-Rp8xis","CPG!A2:L20");importrange("1yqTUmJcL6YxgOpfHS5Pt9nYnmpiqN3tUPQP7-Rp8xis","PUB!A2:L20")},"Select Col5, Sum (Col4) where Col6='' group by Col5 pivot Col7"))
Which I am trying to shorten the formula by using indirect to refer to concatenated import ranges by the following attempt
=ArrayFormula(query(indirect("JOIN!J3"),"Select Col5, Sum (Col4) where Col6='' group by Col5 pivot Col7"))
but come up with this error:
Error
Unable to parse query string for
Function QUERY parameter 2:
NO COLUMN: Col5
The Join!J3 cell contains the value below:
{importrange("1yqTUmJcL6YxgOpfHS5Pt9nYnmpiqN3tUPQP7-Rp8xis","CPG!A2:L20");importrange("1yqTUmJcL6YxgOpfHS5Pt9nYnmpiqN3tUPQP7-Rp8xis","PUB!A2:L20")}
I research various similar question in forums but their answers comes to no solution. Usually it is to use "Select Col1" instead of "Select A" but my formulas are all using Col1, Col2 already. Am doing anything wrong?
I have included some images for further clarification.
The original formula:
The shortening attempt:
The cell value in sheetname: JOIN cell: J3:
The cell value in J3 is actually a pasted value from cell A1:
If I understand correctly, because your formula is now referring to a specific range in the sheet instead of an array of ranges, you will need to use A, B instead of Col1, Col2, etc.
I assume the data produced by cell J3 in the sheet JOIN displays data in J3:U21 (based on the range A2:J20). You need to query all of this, not just the cell containing the formula.
Try the formula below.
(I have made assumptions about what columns the data sits in--please amend where necessary).
=ArrayFormula(query(indirect("JOIN!J3:U21"),"Select N, Sum (M) where O='' group by N pivot P")
EDIT: You seem to be using the same range and ID. You can refer to them using just two cells.
={IMPORTRANGE(D6,"SheetName1"&E6);IMPORTRANGE(D6,"SheetName2"&E6)}

Resources