Reputation: 666
In Google Sheets, what would be the best (Possibly complex) formula to get the SUM of all cells in a table IF the value on column header (Row 1 of each cells) contains a specific word and the value of the 1st column contains an exact word?
Product ID | New York | Florida Sub | Washington | Arizona | Florida Main | Washington |
---|---|---|---|---|---|---|
PID-001 | 50 | 20 | 36 | 50 | 99 | 36 |
PID-002 | 87 | 78 | 72 | 36 | 44 | 55 |
PID-003 | 44 | 55 | 64 | 99 | 20 | 50 |
PID-004 | 52 | 10 | 36 | 24 | 87 | 87 |
Sample sheet above. Basically I wanted to get the total of all quantities for the Product ID PID-003 if the store name has the word "Florida". In the sample sheet the total should be 75.
I have tried complex formulas same as below but either I get an error or I get blank.
=SUM(FILTER($A:$G,REGEXMATCH($A1:$G1,"Florida"),REGEXMATCH($A1:$A5,"PID-003")))
After a day of working I just had to seek professional assistance. Note that this sample is a small data, and actual data is large.
Upvotes: 1
Views: 63
Reputation: 19145
The formula in the question assumes that filter()
can process horizontal and vertical criteria in one go, when it can only do one or the other. To make it work, add another filter()
, like this:
=sum(ifna(filter(
filter(B2:G, search("Florida", B1:G1)),
A2:A = "PID-003"
)))
...or, unprettified:
=sum(ifna(filter(filter(B2:G,search("Florida",B1:G1)),A2:A="PID-003")))
Upvotes: 1
Reputation: 34355
You can also use Xlookup to get a row of the 2d range, then sumif or sumifs with wildcards:
=sumifs(xlookup(I2,A2:A5,B2:G5),B1:G1,"*"&J1&"*")
where J1 contains "Florida" and I2 contains "PID-003"
Upvotes: 1
Reputation: 2486
If you are set to use the SUM
function, you may try to use:
=ARRAYFORMULA(SUM((A2:A5="PID-003")*(IFERROR(SEARCH("Florida", B1:G1)))*(B2:G5)))
Wherein the product of the conditions will result in multiplying the needed data by 1 and the unwanted data by 0 (with the help of IFERROR
and SEARCH
). However, this is already the sum of products logic so I recommend using the SUMPRODUCT
function. The new formula should look like:
=SUMPRODUCT((A2:A5="PID-003")*(IFERROR(SEARCH("Florida", B1:G1)))*(B2:G5))
Upvotes: 2
Reputation: 37050
You need nested FILTER()
then sum. Try-
=SUM(FILTER(FILTER(B2:G5,INDEX(REGEXMATCH(B1:G1,"Florida"))),A2:A5="PID-003"))
Upvotes: 2