Dynamic multiple criteria on sumifs - excel

I was thinking of this for 3 days now. There is a formula that will do this, but I can't figure it out. Hope someone can help me on this.
I'm trying to sum a column in the spreadsheet based on several conditions:
ID1 ID2 ID3 ID4 Value
SW A 1 X 4
SW B 2 Y 5
SE C 2 Y 6
SE A 2 X 3
NE A 0 X 2
SE A 1 X 3
I would like to sum the value column based on the following conditions:
ID1 = SW, SE
ID2 = A, C
ID3 = 1, 0
ID4 = X, Y
Based on the conditions above, sum should be 7
Seems Sumif(s) can only handle 1 dynamic multiple criteria and once I loaded the criteria of ID2, only SW of ID were totaled, formula excluded the SE. (In below formula, ID4 criteria was not yet included).
=SUMIFS(RawData!G:G,RawData!$D:$D,IF($B$7="","<>",{"SW","SE"}),RawData!$C:$C,$C21,RawData!$E:$E,IF($B$9="","<>",{"A","C"}),RawData!$F:$F,IF($B$10="","<>",{"1","0"}))
Is there any way it can handle multiple criteria which in each criteria is an array? Thanks!

SUMIFS can handle up to two criteria lists, (assuming you want to count all possible combinations) as long as one is a "row" and one is a "column" (or transposed to be so), e.g.
=SUMPRODUCT(SUMIFS(Sumrange,Critrange1,{"x","y"},Critrange2,{"a";"b"}))
Note that {"x","y"} has a comma separator (a row) while {"a";"b"} has semi-colon (a column), it has to be like that
SUMIFS produces an array of 4 values (all possible combinations) so SUMPRODUCT is used to sum those 4 values. For 3 or more criteria lists use SUMPRODUCT with MATCH, e.g.
=SUMPRODUCT(Sumrange,ISNUMBER(MATCH(Critrange1,{"SW","SE"},0)*MATCH(Critrange2,{"A","C"},0)*MATCH(Critrange3,{1,0},0)*MATCH(Critrange4,{"X","Y"},0))+0)
Where all ranges are the same dimensions
Your IF functions on the criteria complicate this, and are more difficult to accommodate in this type of formula, but you can cater for those like this, assuming that when the relevant cell is blank all non-blanks are counted:
=SUMPRODUCT(((B7="")*(Critrange1<>"")+ISNUMBER(MATCH(Critrange1,{"SW","SE"},0))>0)*((B8="")*(Critrange2<>"")+ISNUMBER(MATCH(Critrange2,{"A","C"}0))>0)*((B9="")*(Critrange3<>"")+ISNUMBER(MATCH(Critrange3,{1,0}0))>0)*((B10="")*(Critrange4<>"")+ISNUMBER(MATCH(Critrange4,{"x","y"}0))>0))
Note for {1,0} match in this formula only numeric values will be matched. If the data is text formatted quotes need to be added, e.g. {"1","0"} - this works differently in SUMIFS where you can use quotes or not and it will count both text and numbers
In this version you can use commas or semi-colons in the MATCH functions, as long as you are consistent within each MATCH

Related

Excel mapping and create a new column

Excel file columns:
A B C
2 two 3
5 five 8
3 three 10
8 eight 11
12 one 15
I want to create a new column Din same file like below:
A B C D
2 two 3 three
5 five 8 eight
3 three 10
8 eight 11
12 one 15
I want to map C and A and if there's a match D takes values of B.
Example: Value 3 in C is present in A, so D will take corresponding B value three.
Thanks!
So building on BigBen's additional suggestion of using an IFERROR, I believe you want something akin to this in Column D:
=IFERROR(VLOOKUP(C1, A:B, 2, FALSE), "")
... and then drag down the formula throughout Column D
Now, there are some assumptions being made here:
Your data does not have any header row, ala the data starts in Row 1, not Row 2
You want empty/blank values where there is no exact match (this is BigBen's IFERROR suggestion). Your current question layout seems to suggest this. Otherwise, you'll get #N/A in all those blank cells in Column D.
EDIT: To confirm, I used your data (though I started in Row 2), and here's how it looked after -
If one has DA-functionality you could use:
1) - Excluding empty cells using FILTER:
Formula in D1:
=FILTER(B1:B5,COUNTIF(C1:C5,A1:A5)>0)
2) - Including empty cells using XLOOKUP:
Formula in D1:
=XLOOKUP(C1:C5,A1:A5,B1:B5,"")
If one does not have access to DA-functionality you could use:
1) - Excluding empty cells using INDEX, MATCH and SMALL:
=IFERROR(INDEX(B$1:B$5,SMALL(IFNA(MATCH(C$1:C$5,A$1:A$5,0),""),ROW(A1))),"")
Note 1 - This needs to be array entered through CtrlShiftEnter
Note 2 - Alternatively, one could use a non-array entered approach including AGGREGATE as per #basic: =IFERROR(INDEX(B$1:B$5,AGGREGATE(15,6,MATCH(C$1:C$5,A$1:A$5,0),ROW(A1))),"")
2) - Including empty cells using VLOOKUP:
Please refer to the other answer given by #Gravity here.
Basically the difference between both approaches could be vizualised like:

EXCEL: count combinations for unique values

I'm trying to count the following in excel:
I have a number of unique values for X (say 4), each appearing twice (in random order) in the data set. I want to count the number of each combination (regardless of order) of values on a second column Y for each value of X.
Example (here in order):
X Y
1 A
1 D
2 A
2 C
3 B
3 C
4 A
4 D
As output, I need:
n A,D 2
n A,C 1
n B,C 1
n B,D 0
(Let's assume all other combinations don't exist.)
Is this possible without rearranging the data? I don't mind putting in 4 formulas (for each possible combination).
My starting point was akin to this: excel count unique combinations of columns, but I can't quite figure it out and also I know what feels like next to nothing about excel or coding...
To get the count of pairs if the data is not in order, first one needs to get the pairs by X. Create a unique list of X and use this formula:
=CHAR(AGGREGATE(15,6,CODE($B$2:$B$9)/($A$2:$A$9=D2),1))&","&CHAR(AGGREGATE(15,6,CODE($B$2:$B$9)/($A$2:$A$9=D2),2))
Note this only works if the Y is one character like your data.
Then create a unique list of that output and use a standard COUNTIF:
=COUNTIF(E:E,G2)
Or you can use a pivot table on the first helper columns to get the same without the need of getting the unique pairings.
If you use a helper column, you can combine the data into something you can count. Use the formula =IF(A2<>A1,B2,C1&","&B2) in column C. So your data then becomes:
X Y
1 A A
1 D A,D
2 A A
2 C A,C
3 B B
3 C B,C
4 A A
4 D A,D
Then it's a matter of counting up the combinations. Here's a snapshot of my example:
You can use the MMULT function for this purpose, like this (You can insert line breaks with Alt+Enter but you do not have to, it also works if you write it on the same line):
=SUMPRODUCT(
--($B$2:$B$19= LEFT($D2,FIND(",",$D2)-1) ),
MMULT(
--($A$2:$A$19=TRANSPOSE($A$2:$A$19)),
--($B$2:$B$19= MID($D2,FIND(",",$D2)+1,LEN($D2)) )))
You have to enter this as an array formula (After typing, press Ctrl+Shift+Enter, instead of just Enter; you will see the formula inside braces, like this: {=...}).
Here is a screenshot to show what the ranges in the formula mean:

Addition of hidden values from 4 columns

I have to make a system in excel wherein if I put ‘yes’ against three columns out of four columns then the total(hidden cost) must be calculated only on the basis of three columns.
Now I also have to put some numerical values which will be the cost of the respective column but this should be hidden.How to hide values
Example if I have hidden range say 10 in col 1,15 in col 2,30 in col 3 and 20 in col 4 then
if I put ‘yes’ in col 1 col 2 col 3 and ‘no’ in col 4 then cost must come out to be 55.
I am a beginner and struggling to work on it.Please advice.
have a look at the screenshot and imagine that the gray columns are hidden. To keep things simple the formula that checks for yes has been split accross 4 columns that will also be hidden =IF(B1="YES",A1,0) =IF(D1="YES",C1,0) etc. The formula in I1 is a sum of those 4 hidden columns =SUM(J1:M1)
Simpler version
This version puts all the hidden columns together and the sum can be calculated using a single formula.

Excel - Formula or Macro to fill a cell based on another cell that links to yet another cell

In Excel, I am trying to make a cell based of the values contained in two other cells.
I need Cells X and Y to have data based on Cells L and #, like so....
X Y L 1 2 3 4 5 6
A 6 1 1 6;1 6;1 7;1 7;2 7;2 8;1
B 7 2 4 6;1 6;1 7;1 7;2 7;2 8;1
So row A, has columns X and Y filled based of the values in the number columns. The specific number needed is what is filled in in column L.
I am not sure the best way to phrase this question. If my example doesn't make sense, I can try to clarify or provide more examples.
I have no idea if this can be done with fancy formulas or with a VBA macro or two. I am an excel noob.
If I've understood your question correctly you can do this with a combination of Left/Right, Index and search.
In my example images, the user inputs their value in column D, and then columns B and C use the formulea
=LEFT(INDEX($F2:$K2,1,$D2),(SEARCH(";",INDEX($F2:$K2,1,$D2))-1))
=RIGHT(INDEX($F2:$K2,1,$D2),(SEARCH(";",INDEX($F2:$K2,1,$D2))-1))
respectively
Here, the Index function returns the correct column to look at (i.e. the value chosen by the user, the Search function finds the position of the semi-colon, and the left/right functions return the values either side of the semi-colon.

Using COUNTIFS for a series of values at once

Working a step higher then COUNTIFS, I appose a challenge to write a formula without VBA code. The basic data is combined from 1000s of rows with:
Column A: rows with values from 1 to 3
Column B: rows with values from 1 to 250.
For this purpose lets say, we are looking at all cells of value "1" in column A, that suit value "5" in column B. To find all matches, we'd use COUNTIFS command.
1 1
2 5
1 5
1 7
1 10
3 45
2 12
1 2
2 1
=COUNTIFS(A1:A9;1;B1:B9;5)
The answer here is 1.
Next thing, the "5" in column B belongs to a group, e.g. group from 1 to 9. What would the best way be, to count all the matches in this example, so that for all "1"'s in column A, we'd have to find all matches with values from 1 to 9 in column B?! In the upper example that would result in "4". The obvious solution is with a series of IF commands, but that's unefficient and it easy to make a mistake, that get's easily overseen.
=COUNTIFS(A1:A9;1;B1:B9;"<="&9)
Works only as the upper limit. If I give the third criteria range and condition as ">="&1 it does not work - returns 0.
Gasper
Where the data is in A1:B9, using a lookup table in D1:E10 with letters A-J in column D and numbers 0 to 9 in column E and the following formula in B11 referencing letters entered in A11 and A12:
=COUNTIFS(A1:A9,1,B1:B9,">="&VLOOKUP(A11,$D$1:$E$10,2,FALSE),B1:B9,"<="&VLOOKUP(A12,$D$1:$E$10,2,FALSE))
works, changing the letters in A11 and A12 gives the correct count according to what they correspond to in the looku in D1:E10.
When you say give third criteria range do you mean:
=COUNTIFS(A1:A9;1;B1:B9;"<="&9,B1:B9;">=1")
If so then try:
=COUNTIFS(A1:A9;1;B1:B9;AND("<="&9,;">=1"))
ie have two conditional ranges with the second range having both conditions combined with AND()
Maybe what you want(ed) is:
=COUNTIFS(A:A;1;B:B;">=1";B:B;"<=9")
Almost there. I noticed that three criteria ranges and conditions work only if I use "=" sign in a condition. As soon as I use
=COUNTIFS(A1:A9;1;B1:B9;"<="&9,B1:B9;">=1")
it returns 0. My goal is to eventualy replace the number in a condition with a VLOOKUP command, so the final equation should be smth like
=COUNTIFS(A1:A9;1;B1:B9;"<="&VLOOKUP(...),B1:B9;">=VLOOKUP(...)")
But the "<" and ">" signs mess with this. Still looking for a solution.
This is my entire line, if it offers any further indication. The AND() commands is at the end - and it still results in 0
=COUNTIFS(INDIRECT(CONCATENATE("baza!$";SUBSTITUTE(ADDRESS(1;MATCH("card_type_id";baza!$A$1:$AAA$1;0);4);"1";"");"$2:$";SUBSTITUTE(ADDRESS(1;MATCH("card_type_id";baza!$A$1:$AAA$1;0);4);"1";"");"$15000"));IF(C6="računska";1;0);INDIRECT(CONCATENATE("baza!$";SUBSTITUTE(ADDRESS(1;MATCH(IF($C$4="CC_SI_klasifikacija";"building_classification_id";0);baza!$A$1:$AAA$1;0);4);"1";"");"$2:$";SUBSTITUTE(ADDRESS(1;MATCH(IF($C$4="CC_SI_klasifikacija";"building_classification_id";0);baza!$A$1:$AAA$1;0);4);"1";"");"$15000"));AND("<="&VLOOKUP($C$5;$K$203:$N$223;4;FALSE);">="&VLOOKUP($C$5;$K$203:$N$223;3;FALSE)))

Resources