Compare Two Columns with Complex Conditionals - excel

I can't get my head wrapped around this multi conditional between two columns. I have two columns A and B but would like to use some formulas to compare each "grouping" of column A. For example in Column A, if all "group 2" has all Column B values as Pass, it is a pass.
Edit: I've updated it with some more rules since this just a bit more complicated for me to wrap my head around.
There are only 5 criteria:
PASS, PROG, UNAVAIL, IGNORE, "BLANK"
Rules:
FAIL if subgroup has 1 or more fail
IGNORE if subgroup has 1 or more ignore
PASS if ALL PASS or combination of PASS and UNAVAIL
PROG if NOT fail and a combination of PASS, UNAVAIL, PROG
"BLANK"s are treated as UNAVAIL
Appreciate any help, thank you!

(Answer changed to reflect new criteria)
This sheet:
Was created with the following two formulas (using named ranges in A-C where the name is in the first row):
In C1 I entered (then copied)
=CONCATENATE(TRIM(A2),"-", IF(LEN(TRIM(B2)) > 0, TRIM(B2), "UNAVAIL"))
In F2 I entered (then copied)
=IF(COUNTIF(Tag, E2 &"-FAIL") >0, "FAIL",IF(COUNTIF(Tag, E2 &"-IGNORE") >0,"IGNORE",IF(COUNTIF(Group,E2) = COUNTIF(Tag, E2 &"-PASS") + COUNTIF(Tag, E2 &"-UNAVAIL"),"PASS","PROG")))
The 4th case is like an else at the bottom of a switch -- no need to explicitly check the condition.

along the same lines,
add a column C, where if the result is pass value is 0,
result is fail value will be -100000 or so (large negative)
for uncertain use some prime negative like -3/
then use a pivot table and use the sum of the values
Then you can use formulas to deduce different conditions.
the use of large negative number is to be be able to if all the results
are uncertain, as long as they don't overlap the range less than -100000 or so.
anyway you get the idea.

This formula may work (enter as an array formula CTRL-ENTER):
=IF(SUM(IF(IF($A$2:$A$23=$E2,$B$2:$B$23,"OUT_OF_GROUP")="FAIL",1,0))>0,"FAIL",IF(SUM(IF(IF($A$2:$A$23=$E2,$B$2:$B$23,"OUT_OF_GROUP")="IGNORE",1,0))>0,"IGNORE",IF(SUM(IF(IF($A$2:$A$23=$E2,$B$2:$B$23,"OUT_OF_GROUP")="PASS",1,0))+SUM(IF(IF($A$2:$A$23=$E2,$B$2:$B$23,"OUT_OF_GROUP")="UNAVAIL",1,0))+SUM(IF(IF($A$2:$A$23=$E2,$B$2:$B$23,"OUT_OF_GROUP")=0,1,0))=SUM(IF($A$2:$A$23=$E2,1,0)),"PASS","PROG")))
Here Group and Result are in $A$2:$A$23 and $B$2:$B$23, respectively. E2:E11 holds "s1" through "s10". I assumed Rule 3 meant that a combination of UNAVAIL and blanks is a PASS --- the logic could be modified to make that a PROG.

Related

Condensing nested if-statements with multiple criteria

The blue columns is the data given and the red columns is what is being calculated. Then the table to the right is what I am referencing. So, F2 will be calculated by the following steps:
Look at the Machinery column (D), if the cell contains LF, select column K, otherwise select column L
Look at the Grade column (E), if the cell contains RG, select rows 4:8, otherwise select rows 9:12.
Look at the Species column (A), if the cell contains MS, select rows 5 and 10, otherwise.......
Where every the most selected cell is in columns K and L, copy into column F.
Multiply column F by column C.
I don't want to make another column for my final result. I did in the picture to show the two steps separately. So column F should be the final answer (F2 = 107.33). The reference table can be formatted differently as well.
At first, I tried using nested-if statements, but realized that I would have like 20+ if statements for all the different outcomes. I think I would want to use the SEARCH function to find weather of not the cell contains a specific piece of information. Then I would probably use some sort of combination of match, if, v-lookup, index, search, but I am not sure how to condense these.
Any suggestion?
SUMPRODUCT is the function you need. I quickly created some test data on the lines of what you shared like this:
Then I entered the below formula in cell F2
=SUMPRODUCT(($I$4:$I$9=E2)*($J$4:$J$9=LEFT(A2,FIND(" ",A2)-1))*IF(ISERROR(FIND("LF",D2,1)),$L$4:$L$9,$K$4:$K$9))
The formula may look a little scary but is indeed very simple as each sub formula checks for a condition that you would want to evaluate. So, for example,
($I$4:$I$9=E2)
is looking for rows that match GRADE of the current row in range $I$4:$I$9 and so on. The * ensures that the arrays thus returned are multiplied and only the value where all conditions are true remains.
Since some of your conditions require looking for partial content like in Species and Machine, I have used Left and Find functions within Sumproduct
This formula simply returns the value from either column K or L based on the matching conditions and you may easily extend it or add more conditions.

COUNTIF with a positive and negative criteria

I need to count the number of cells in which the cells in "LIST A" appear in the set of values contained in column D AND the cells in "LIST B" do not appear in column E.
I am trying to use something like the following array formula, but so far have had limited success:
={SUM(COUNTIFS(A2:A21,D2:D5,B2:B21,"<>"&E2:E3))}
Please note that the data contained in this example is different to the data in my real table. The real table is considerably longer and more complex than this table.
Any suggestions?
just for your example the formula like in my comment will be enough
=SUMPRODUCT(COUNTIF(D2:D5,A2:A21)*NOT(COUNTIF(E2:E5,B2:B21)))
the problem ocures if you have multiple values you want to exclude. then you need to use a negative countif(s)
=SUMPRODUCT(1*NOT(COUNTIF(E2:E5,B2:B21)))
this would count all lines which contain anything of the exclude list. But the NOT inside the sumproduct will switch it to opposite (the 1* is needed cus it would only contain bool which can't be count)
if you have column C with bool (true/false or 1/0) you can simply add that:
=SUMPRODUCT(COUNTIF(D2:D5,A2:A21)*NOT(COUNTIF(E2:E5,B2:B21))*C2:C21)
or also (C2:C21>12) if that is what you need... but you also could include it to the include list (if A is A/B/E/F and C is (2/4/6)
=SUMPRODUCT(COUNTIFS(D2:D5,A2:A21,F2:F5,C2:C21)*NOT(COUNTIF(E2:E5,B2:B21)))
But every exclude needs its own countif (B is not X/Y and C is not 8/9/11)
=SUMPRODUCT(COUNTIFS(D2:D5,A2:A21)*NOT(COUNTIF(E2:E5,B2:B21))*NOT(COUNTIF(F2:F5,C2:C21))
As already said: Having formulas which return "" may return false counts (keep that in mind)
Best to switch to SUMPRODUCT in such cases:
=SUMPRODUCT(0+ISNUMBER(MATCH(A2:A21,D2:D5,0)),1-ISNUMBER(MATCH(B2:B21,E2:E5,0)))
Regards

Excel: match two columns and output third ... AND... there are multiple instances in each column

Working off a previous post:
Excel match two columns and output third
I have values in column A that are not unique and values in column B that are not unique, but together column A and B produce unique combinations:
A B C
1 Red Car Result#1
2 Blue Boat Result #2
3 Red Boat Result #3
4 Green Car Result #4
Let's say I want to find a match where Column A = Red and Column B = Boat which should return the corresponding value in Column C which should be Result #3.
Using the previous post's solution:
=IF(MATCH("Red",A1:A4,0)=MATCH("Boat",B1:B4,0),INDEX(C1:C4,MATCH("Boat",B1:B4,0)),0)
This would actually return value the first match for Boat in column B which would be result#2 rather than the intended result#3 where the match was true.
Any ideas on how to modify or write a function that would specify to retrieve information relative to specifically where the match was true (without using VBA)?
I've thought of a possible work around by creating another column that combines Col A and B to make a unique identifier but I was hoping to avoid that.
Thanks! Really appreciate it and sorry about the table formatting. I'm still very new at this.
You can retrieve a two column match using the AGGREGATE function to force anything that does not match into an error and ignore the errors.
      
The formula in E6 is,
=IFERROR(INDEX(C$1:C$99,AGGREGATE(15,6,ROW($1:$99)/((A$1:A$99="red")*(B$1:B$99="boat")), ROW(1:1))), "")
You are actually using the SMALL sub-function of the AGGREGATE function so you can get the second, third, etc. successive matches by increasing the k paramter. I done this above by using ROW(1:1) which equals 1 but will increase to 2, 3, etc as the formula is filled down.
Creating a third column is the most efficient solution. However if you absolutely have to avoid it, you could use a complicated formula like this:
=INDEX($C$1:$C$6,MATCH(A11,$A$1:$A$6,0) + MATCH(B11,OFFSET($A$1,MATCH(A11,$A$1:$A$6,0)-1,1,COUNTA($A$1:$A$6)-MATCH(A11,$A$1:$A$6,0),1),0)-1)
But the condition is that the lookup table is sorted for both column 1 and 2.

SumProduct, doesn't return me text and number. (Only Number)

At first thanks to answer)) (It's important for me :p )
I have a number in A3.
When there is this number in column A (Sheet1), per exemple A7 then it will take the value of the cell B7.
=SOMMEPROD(('Sheet1'!A3:A34=Sheet1!A3)*('Sheet1'!B3:B34))
I use SOMMEPROD, it's working but only with number, and sometimes I have text and number in my cell (column B).
I already changed the format of the cell but doesn't work.
Thanks a lot)))
I think you are saying that some of the values in column B are expressed as text, not as a number, probably because they were imported from another source.
if that is the case: First, convert column B into numbers, Then, use sumproduct.
For example, FIRST convert text to values:
in C3 you would enter
=value(b3)
Then copy that down to from C3 to C34.
Then
=SOMMEPROD(('Sheet1'!A3:A34=Sheet1!A3)*('Sheet1'!C3:C34))
should have the desired effect.
Note that you sometime get #N/A errors with the "value" function, for example if there are unexpected spaces. To be safe, rather than using value alone, try this:
= iferror(value(B3),0)
or to be more creative you can try:
= iferror(value(substitute(B3," ","")),0) ' deletes all spaces
= iferror(value(substitute(substitute(b3,char(160),"")," ","")),0)
'deletes all spaces and "nonbreaking spaces" copied from a web page
It depends on what you're trying to use SumProduct for. There's two main reasons this function shows up. Those are:
To sum the product of two ranges.
To sum a range of numbers that meet multiple criteria. This relies on that True = 1 and False = 0 so when want to find the combined weight of blue dogs in some cells, your formula might look like =SUMPRODUCT(1*($A$1:$A$50 = "Blue"), 1*($B$1:$B$50 = "Dog"), $C$1:$C$50). Note the 1* which guarantees the formula will know how to multiply the results. After all, True and False don't multiply. Their binary representations (1 and 0) though, do.
NB though that people also sometimes remember it as a lookup formula because of the second use, and therefore expect to be able to get text back. Such approaches are an easy misunderstanding to end up with, but really there you want to use an INDEX(<Result Array>,MATCH(1,INDEX(1 * (FIRST CONDITION) * (SECOND CONDITION) * (AND SO ON),,),0). This approach basically gives you an array formula without CTRL-SHIFT-ENTER (it's not faster, but because the condition is in an index formula, it works), and your conditions can be arbitrary (the same way as for sumproduct). But it'll give you the FIRST result, not the sum of all of them.
Hope that helps.
Edit: Crossed my mind, you might be expecting A1 to contain Erp and B1 to contain 54, and in C1 you want Erp54. This doesn't even need a function - just the & operator, which joins two strings together - so in C1, you'd just put =A1 & B1. And you're done.

VLOOKUP with two criteria?

Is there a formula that returns a value from the first line matching two or more criteria? For example, "return column C from the first line where column A = x AND column B = y". I'd like to do it without concatenating column A and column B.
Thanks.
True = 1, False = 0
D1 returns 0 because 0 * 1 * 8 = 0
D2 returns 9 because 1 * 1 * 9= 9
This should let you change the criteria:
I use INDEX/MATCH for this. Ex:
I have a table of data and want to return the value in column C where the value in column A is "c" and the value in column B is "h".
I would use the following array formula:
=INDEX($C$1:$C$5,MATCH(1,(($A$1:$A$5="c")*($B$1:$B$5="h")),0))
Commit the formula by pressing Ctrl+Shift+Enter
After entering the formula, you can use Excel's formula auditing tools to step through the evaluation to see how it calculates.
SUMPRODUCT definitely has value when the sum over multiple criteria matches is needed. But the way I read your question, you want something like VLOOKUP that returns the first match. Try this:
For your convenience the formula in G2 is as follows -- requires array entry (Ctrl+Shift+Enter)
[edit: I updated the formula here but not in the screen shot]
=INDEX($C$1:$C$6,MATCH(E2&"|"&F2,$A$1:$A$6&"|"&$B$1:$B$6,0))
Two things to note:
SUMPRODUCT won't work if the result type is not numeric
SUMPRODUCT will return the SUM of results matching the criteria, not the first match (as VLOOKUP does)
Apparently you can use the SUMPRODUCT function.
Actually, I think what he is asking is typical multiple results display option in excel. It can be done using Small, and row function in arrays.
This display all the results that matches the different criteria
Here is an answer that shows how to do this using SUMPRODUCT and table header lookups. The main advantage to this: it works with any value, numeric or otherwise.
So let's say we have headers H1, H2 and H3 on some table called MyTable. And let's say we are entering this into row 1, possibly on another sheet. And we want to match H1, H2 to x, y on that sheet, respectively, while returning the matching value in H3. Then the formula would be as follows:
=INDEX(MyTable[H3], ROUND(SUMPRODUCT(MATCH(TRUE, (MyTable[H1] & MyTable[H2]) = ($x1 & $y1),0)),0),1)
What does it do? The sum-product ensures everything is treated as arrays. So you can contatenate entire table columns together to make an array of concatenated valued, dynamically calculated. And then you can compare these to the existing values in x and y- somehow magically you can compare the concatenated array from the table to the individual concatenation of x & y. Which gives you an array of true false values. Matching that to true yields the first match of the lookup. And then all we need to do is go back and index that in the original table.
Notes
The rounding is just in there to make sure the Index function gets back an integer. I got #N/A values until I rounded.
It might be more instructive to run this through the evaluator to see what's going on...
This can easily be modified to work with a non table - just replace the table references with raw ranges. The tables are clearer though, so use them if possible. I found the original source for this here: http://dailydoseofexcel.com/archives/2009/04/21/vlookup-on-two-columns/. But there was a bug with rouding values to INTs so I fixed that.

Resources