DSUM with multiple criteria - google-sheets

I'm trying to get the sum of all items in column F:F when Column J:J = "Channel"and Column K:K = "Country"
=DSUM(Sheet1!$A$1:$K$142,Sheet1!$F$2:$F$142,{{Sheet1!K:K;"Channel"},{Sheet1!L:L;"Country"}})
The above query returns a 0 when it should return a 7.

Try:
=DSUM(Sheet1!$A$1:$L$142, Sheet1!$F$1, {{Sheet1!$K$1; "Channel"}, {Sheet1!$L$1; "Country"}})
Notice changes about the parameters:
Sheet1!$A$1:$L$142 column L:L should be included if you want to use it in your criteria.
Second parameter just needs a column name (Sheet1!$F$1) or its number (6). No need to place the whole range there.
You need to have a two strings range for criteria: first one with the column name, second with the criteria for that column. You set before the whole column K:K and L:L and placed a string to match at the bottom. Only the first tow rows were used, so you criteria was actually like this: kolumn K:K should be like the value in K2 and column L:L like the value in L2 (but this one didn't actually worked as this column was not a part of your table (the 1st parameter)).
Or you can use SUMIFS:
=SUMIFS(Sheet1!$F$2:$F$142, Sheet1!$K$2:$K$142, "Channel", Sheet1!$L$2:$L$142, "Country")
Or if you need to sum the whole column (not just down to 142 row):
=SUMIFS(Sheet1!$F:$F, Sheet1!$K:$K, "Channel", Sheet1!$L:$L, "Country")

Here's a basic proof-of-concept.
Count all vegetables
Answer: 18
Formula: =DSUM(A4:D10,"Qty",{"Category";"Vegetable"})
Count all vegetables at Safeway
Answer: 10
Formula: =DSUM(A4:D10,"Qty",{{"Category";"Vegetable"},{"Store";"Safeway"}})

Related

Sum range in Google Sheets of only unique values

I need a formula to sum a column of values. But I only want to sum the rows that have an ID that are unique among the entire range. See the example below where I have 6 rows, but there are only 3 unique IDs. My ideal outcome is a sum of 6 by adding one instance of UNIQUEID-00A, UNIQUEID-00B, and UNIQUEID-00C.
Notes:
A unique ID will always have the same value. For example, if UNIQUEID-00A were listed 1 or 100 times, the associated value will always be 1. So I don't need the formula to account for a scenario where the duplicate IDs have different values.
There may be instances where Column A (the ID) and Column B (the value) will not be sitting side-by-side. It might be that 2, 3, or more columns are in between the two columns.
The IDs and values could be 6 rows tall, or hundreds of rows tall. For that reason I'd like to use something like A2:A to target the IDs rather than having to specify a specific start and end point like A2:A7.
See the linked Google Sheet below for a copy of this.
ID
Value
UNIQUEID-00A
1
UNIQUEID-00B
2
UNIQUEID-00C
3
UNIQUEID-00A
1
UNIQUEID-00B
2
UNIQUEID-00C
3
https://docs.google.com/spreadsheets/d/1bU4J1RL5S0a_NvFjW_KVpiKi8603Tj9iVPeNzBlw-OA/edit?usp=sharing
Given the above table in A1:B7:
=sum(index(unique(A2:B7),,2))
UNIQUE returns the distinct rows of the range, INDEX (with column parameter = 2) to return only the value column, then SUM the result.
EDIT Based on the updated requirements, with the IDs in, for example, column A and the values to be summed in column C, use something like:
=sum(index(unique({A2:A,C2:C}),,2))
I'm assuming that there is no data below your input table. The UNIQUE here will return an extra blank row because of the unlimited range, but SUM will disregard it.
use:
=SUM(SORTN(B3:B, 9^9, 2, A3:A, 1))
sum B column
while returning all rows 9^9
that are unique 2
within A column
in whatever order 1

How to print different values to column based on value in another column and other conditions met?

I have a spreadsheet with several columns and I want to return a different value based on the value in Column A and if any of the other columns show a true or 1.
For example
If column A has the value "A" and any column B-N is either TRUE or 1 then I want to return "Good" to column O
If column A has the value "B" and any column B-N is either TRUE or 1 then I want to return "Best" to column O
Link to spreadsheet: https://docs.google.com/spreadsheets/d/12k9usKsOgrOUhtW5WBvfY7WB5hbfnTMSPgXtrqcRFjM/edit?usp=sharing
Try this in cell O2 (where your sample sheets only has values of TRUE, FALSE, Y or N in B2:N):
=arrayformula((trim(transpose(query(transpose(iferror(regexreplace(regexreplace(text(B2:N,),"(FALSE)|(N)",),"(Y)|(TRUE)",if(A2:A="A","Good",if(A2:A="B","Best",))),)),,columns(B2:N))))))
Alternative where values in B:N are either 0 or 1:
=arrayformula((trim(transpose(query(transpose(iferror(substitute(substitute(B2:N,0,),1,ifs(A2:A="A","Good",A2:A="B","Best")),)),,columns(B2:N))))))
B2:N is the range of cells to process.
The inner SUBSTITUTE clears all 0 values.
The outer SUBSTITUTE swaps 1 values for a test to see if values in A contain "A" or "B".
IFS does the test and returns either "Good" or "Best".
IFERROR hides any #N/A values down the sheet where the rows are empty.
TRANSPOSE transposes the data for QUERY.
QUERY is used to collapse empty cells (vertically). columns(B2:N) is used in the header part of QUERY. This is a quirk of QUERY, where the header number >= the columns of data, QUERY does the collapse.
TRANSPOSE reverts the dataset to the previous orientation.
TRIM removes leading or trailing space.
ARRAYFORMULA allows the formula to automatically cascade down the sheet, rather than you needing to drag the formula down (like with =IF(AND({B2:N2}>0, A2="A"),"Good", "")).
So, you already have this partial solution. To get a second condition to be met before printing something to your cell, just add an AND() with a new COUNTIF(), comparing A column with A, and then at the else argument, repeat your original IF(), just changing A for B and the output for each case. I will look like this:
=IF(AND(countif(A2;"A");OR(countif(A2:N2;"Y");countif(A2:N2;TRUE)));"Good";
IF(AND(countif(A2;"B");OR(countif(A2:N2;"Y");countif(A2:N2;TRUE)));"Best";"BAD"))
To use it on every row, just autofill the column O. The row numbers will change accordingly and work on its own.
If you need a new if() statement for a third or fourth case, just repeat it, nesting one IF() inside the other, leaving room for a default error message at the end.

Third Parameter in Array Formula to check for highest(max) occurence but only if the other conditions are true first

Ok, this is the updated linkI have multiple criteria to look through in my arrayformula(index(match())). The first two are simple as they reference the row the formula is calculating on. The last conditional I have is to find the highest occurrence in a given range, but ONLY if the other conditions are met...something like a filtered maxifs..any ideas?
Here is my code in column P =iferror(ArrayFormula(index($F:$F,match(1,("Fee Taken"=$C:$C)*(H12=$H:$H)(maxifs($M:$M,$H:$H,H2,$C:$C,"Fee Taken"),0))),""))
The result that I would like is to return from column F if the name matches that rows name, the transaction type is "Fee Taken" from column C, and THEN if those conditions are true I want it to find the max value from column M based on those two criterias and return the column F value for that max value row..
Ive attached some pictues to show my data.
The last part of the Match function where I have the Maxifs equaling to eachother is where I am confused; my thoughts were to see if the maxifs for the item in Column "M" can be used as a criteria..but I do not think so....I only want the highest occurence F:F if both conditions are met and it is the highest value for both those criteria in column M..
Please let me know if you need anymore info..Thanks![
Working formula will be:
=ArrayFormula(index($F:$F,match(1,--(M:M=(maxifs($M:$M,$H:$H,H2,$C:$C,"Fee Taken"))),0)))

Google Sheets: How do I create an array from a range, adding a column with a constant literal value in every row?

I want to make an array with several columns. The second and subsequent columns are specified as a range pulled from another sheet. The first column is a static constant, that is, every cell in the first column should have the very same literal string value, say 'foo'. I can't find the correct syntax. I'd have thought something like this would work:
={"foo", 'Other Sheet'!C2:F}
but I get "Function ARRAY_ROW parameter 2 has mismatched row size. Expected: 1. Actual: 999." Clearly "foo" needs to be "expanded" to a column with lots of rows. How do I do it, and where are tricks like this documented?
Maybe the answer to this question would give a start: How do I create an array containing a single column, every cell containing "foo", with the number of columns specified by a different range?
Here is an editable sheet illustrating the problem and the desired solution:
https://docs.google.com/spreadsheets/d/17myzKVFN3SDQuubWNdP-dFAbdvdlRbZFkjRpLi2Fas8/edit?usp=sharing
The exact question is this: what formula can I put in cell B9 of Sheet1 to get the current appearance of Sheet1? Notice that I don't know in advance how many rows there are in 'Other Sheet'. It's OK to assume that all rows of Other Sheet have a nonblank value in column C.
You can loop with an arrayformula and assign them to the first column, ending the array with the same size:
={ARRAYFORMULA(if(len('Other Sheet'!C2:C),"foo",)),'Other Sheet'!C2:F}
Side note: that between the {}, if you put a comma ({expr1 , expr2}), the value will be side by side, and if you put a semicolon ({expr1 ; expr2}), the values will be one above the other.
You can use QUERY for that:
=QUERY('Other Sheet'!C2:F, "select 'foo',C,D,E,F where C is not null")
If you want to remove the 'foo' column header, you can use:
=QUERY('Other Sheet'!C2:F, "select 'foo',C,D,E,F where C is not null label 'foo' ''")

Find matches between two columns, sum 3rd column.

I'm looking for a formula which lets me search for matches between two columns, and where a match is found, add the associated value from a 3rd column.
Example
If a fruit in column B matches any of the values in Column A, add the associated value from Column C. Here you have 2 grapefruit matches ($2) and 2 orange matches ($.5) so you get $5.
Is there a formula to do this automatically? Huge thanks for any help!
A simple version is
=sum(filter(C2:C8, match(B2:B8, A2:A4, 0)))
which looks up each element of B2:B8 in the range A2:A4, and if there is a match, includes the corresponding entry from C in the sum.
If you expect the entries in column A to grow (so they won't always stay A2:A4), the following may be preferable:
=sum(filter(C2:C8, match(B2:B8, filter(A2:A, len(A2:A)), 0)))
Here the fixed range A2:A4 is replaced by the output of a filter, which returns all nonempty cell in A2:A.

Resources