ArrayFormula, SumProduct and Google Sheets returning only 1 value - google-sheets

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.

Related

Arrayformula with Vlookup matching Data in range

I am trying to use an ArrayFormula along with Vlookup with a range to match the data from another Worksheet. In my demo, the source sheet with Col B with orange and which is Col A is Stack but the destination sheet is not matching where orange exists. I can't understand where is my fault.
I have tried but it's wrong pulled.
=ARRAYFORMULA(VLOOKUP(B:B,IMPORTRANGE("13UCvlMfCse9A_fyVOPlQiwL6JADKZ3O2kMjUfznI-q0", "Source!A:B")},1))
Worksheet demo
I am expecting in the destination sheet that Col A matches Col B.
When using VLOOKUP, the first column of the range must be the search column. And you shouldn't omit the last parameter if you are looking for an exact match.
=ARRAYFORMULA(IFNA(VLOOKUP(B:B,QUERY(TO_TEXT(IMPORTRANGE("13UCvlMfCse9A_fyVOPlQiwL6JADKZ3O2kMjUfznI-q0", "Source!A:B")),"SELECT Col2, Col1"),2,0)))
update
=ARRAYFORMULA(IFNA(VLOOKUP(B:B,MAP({2,1},LAMBDA(col,INDEX(IMPORTRANGE("13UCvlMfCse9A_fyVOPlQiwL6JADKZ3O2kMjUfznI-q0", "Source!A:B"),,col))),2,0)))
With dynamic array formula MAP(), try-
=MAP(B1:INDEX(B1:B,COUNTA(B1:B)),LAMBDA(x,IFERROR(QUERY(INDEX(TO_TEXT(IMPORTRANGE("13UCvlMfCse9A_fyVOPlQiwL6JADKZ3O2kMjUfznI-q0", "Source!A:B"))),"select Col1 where Col2='" & x & "'"),"")))

Google Sheets: Arrayformula for summarizing textjoined arrays (query/filter...)

I have a problem in Google Sheets summarizing values from a column in one single cell using an Arrayformula.
I made an easy example how my data looks like (approximately) and how I want to summarize it.
In table1 you can see project_members stuffed on different projects.
Table2 shows how I want to visualize my data. In column D the unique project_ids are listed, so far so good. Now I want column E to show every member stuffed on the project I can find next to them.
[Example][1]
[1]: https://i.stack.imgur.com/1v1mk.png
I have managed to get what I want using this:
=TEXTJOIN(" , ";1;FILTER(B:B;A:A=D2))
or
=TEXTJOIN(" , ";1;QUERY(A:D;"Select B where A contains '"& D2 &"' ";0))
Now my actual dataset is way bigger than this example and data is beeing added constantly. This is why I need the formula in column E to be automated.
I have tried this:
=ARRAYFORMULA(TEXTJOIN(" , ";1;QUERY(A:D;"Select B where A contains '"& D:D &"' ";0)))
But that doesnt work.
I´m thankful for help!
solution
Try this unique arrayformula that will take into account added lines (German Notation)
=ARRAY_CONSTRAIN( transpose({transpose(unique(A2:A));arrayformula(trim(query(arrayformula(if(A2:A=transpose(unique(A2:A));B2:B&",";));;9^9)))});counta(unique(A2:A));2)
explanation about the construction
the most important step is in D2 (US notation)=arrayformula(if(A2:A10=transpose(unique(A2:A10)),B2:B10,)), then apply query(,,9*9) to gather all items in each column
See my newly added sheet ("Erik Help") and this formula there:
=ArrayFormula({A1:B1;REGEXREPLACE(TRIM(SPLIT(FLATTEN(QUERY(QUERY({A2:A&"~"\B2:B&","}; "Select MAX(Col2) where Col2 IS NOT NULL GROUP BY Col2 PIVOT Col1");; 9^9));"~"));"[,\s]+$";"")})
This formula is complex, and so I invite anyone who is interested to take it apart and put it back together for understanding.

Google Sheet | Excel | Array Formula + CountIf + Partial Text Problem

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

Summing values based on multiple criteria. Columns are different sizes

I have a Google Sheet (Test Sheet 2) with two sheets in it, Sheet 1 and Sheet 2. Sheet 2 is where all of the data is and I need to get the sums of the counts of that column based on three criteria into sheet 1 column C. The name, week, year and count need to match up. I used the formula
=arrayformula(iferror(vlookup(A2:A&2020&B2:B, {Sheet2!A2:A&Sheet2!B2:B&Sheet2!C2:C, Sheet2!D2:D}, 2, FALSE)))
but that only works for unique rows. In the example sheet I am providing, the formula works well for Bill, Lisa, Katie and Jon because they all have one value for 'count' from Sheet2 when the parameters of name, week and year match up. But Mike has two rows matching the criteria. This formula returns the first match which is 3. The other value is 4 so I would like the count in Sheet1 to show 7 instead of 3. I need it to add them up.
I also tried to use sumifs but the columns are two different sizes so that didn't work.
Any idea? I did try to combine sumif with the above formula but that did not work either.
Link to Test Sheet
Solution with ARRAYFORMULA and SUMIF
If you need to use ARRAYFORMULA to improve the performances you can use this tweak of the SUMIF statement: basically you can concatenate the conditions to make them create a single AND condition.
This would be a possible solution for the formula in your comment:
=ARRAYFORMULA(SUMIF(Sheet2!A2:A&Sheet2!B2:B,A2:A&2020,Sheet2!D2:D))
Solution with SUMIFS
If you are looking for a solution with the SUMIFS formula you can use this:
SUMIFS('sum_range', 'criteria_range', condition, ['criteria_range_2', condition_2])
In your case this will translate to:
=SUMIFS(Sheet2!D2:D, Sheet2!A2:A, A2, Sheet2!B2:B, 2020)
In this case the ranges dimensions won't affect the formula execution.
Just drag this formula for the Sheet1 table column and you will get the results. The drawback is that you cannot use ARRAYFORMULA with SUMIFS. Performance wise, if you have a lot of rows in the Sheet1 I suggest using the ARRAYFORMULA solution, since this will trigger a lot of formula calls instead of just one.
Try this query()
=query(Sheet2!A:D, "Select A, C, sum(D) where B = 2020 group by A, C label C 'Week', sum(D) 'Count'", 1)
UPDATED:
If you really need to use vlookup in arrayformula() you can always ise the query (that deals with the summing) as the lookup range. In the spreadsheet I used
=ArrayFormula(if(len(A2:A), iferror(vlookup(A2:A&year(D2:D)&E2:E, query({Sheet2!A:A&Sheet2!B:B&Sheet2!C:C, Sheet2!D:D}, "Select Col1, sum(Col2) where Col1 <>'' group by Col1", 1), 2, 0)),) )
and see if that helps?

Add Filter to Vlookup formula Google sheets

I know verry little about formulas
I have a Vlookup formula which is working
=ArrayFormula(iferror(vlookup(ConnectionHelper!M2:M, ConnectionHelper!$D$2:$E, {2,1}, false)))
I want to FILTER out rows if a cell in column D is empty
But not getting how or ever if possible!
I am trying
=Filter(ArrayFormula(iferror(vlookup(ConnectionHelper!M2:M, ConnectionHelper!$D$2:$E, {2,1}, false))),D2:D<>'')
Thanks
Try
=query(ArrayFormula(iferror(vlookup(ConnectionHelper!M2:M, ConnectionHelper!$D$2:$E, {2,1}, false))), "where Col2 <>''")
(assuming your data in column D is text).

Resources