How to use index match with IF in excel? - excel

Table1
A B C D
1 Seq Item Re-Order Qty On-hand Qty
2 1 X 10 15
3 2 Y 10 5
4 3 Z 10 10
Other worksheet:
Table2
Expected output:
A B C
1 Seq Item Re-Order Qty
2 1 N/A N/A
3 2 Y 10
4 3 N/A N/A
In table2 I need to put in column 2 equation like this:
Index(Table1[Item],Match(table2[Seq],tabel1[Seq],0) WHERE table1[reorder qty] > table1[On-hand Qty]
I'm not sure how such requirement could be managed?

This can be done. It requires the use of an array formula in Table2.
Normally with an INDEX you simply use a range of cells as the array (first argument of the formula). In this case, we will give it a new array to return based on the results of a conditional (your WHERE clause).
I will start with the picture of results and then give the formulas. For me, Table1 is on the left, Table2 on the right.
Formulas
The formulas are very similar, the main difference is which column to return in the IF part which generates the array for INDEX. The conditional part of the IF is the same for all columns. Note that using Tables here really helps copying around the formulas since the ranges cannot change under us.
These are all array formulas and need to be entered with CTRL+SHIFT+ENTER.
Table2[Item]
=INDEX(IF(Table1[Re-Order Qty]>Table1[On-hand Qty],Table1[Item],"N/A"), MATCH([#Seq],Table1[Seq],0))
Table2[Re-Order Qty]
=INDEX(IF(Table1[Re-Order Qty]>Table1[On-hand Qty],Table1[Re-Order Qty],"N/A"), MATCH([#Seq],Table1[Seq],0))
Table2[On-hand Qty]
=INDEX(IF(Table1[Re-Order Qty]>Table1[On-hand Qty],Table1[On-hand Qty],"N/A"), MATCH([#Seq],Table1[Seq],0))
The main idea behind these formulas is:
Return a new array based on the conditional. This new array will return the desired column (Item, Re-order, ...) or it will return N/A if the conditional is FALSE. This requires the array formula entry since it is going row by row in the IF.
The MATCH part of the formula to get the row number is "standard". We are simply looking for the Seq number in Table1. This determines which row of the new array to return.

Related

Why excel SUMIF with range criteria does not work as expected?

I have 3 columns A, B, C. Now, I want to get the sum for each cell Ai where
Bi < Ci
Example:
A B C
-----------
2 1 3
4 5 4
3 2 7
6 6 6
-----------
Expected result = A1 + A3 = 2 + 3 = 5
I tried the following formula: =SUMIF(B1:B4,"<C1:C4",A1:A4),
but it gives me 0 !
Anyone could help ?
The SUMIF function accepts 3 arguments: range, criteria, and sum_range.
If you check the documentation, you'll notice that while range and sum_range both accept a "range of cells" (i.e. multiple values), criteria is a single value, the same thing compared against all of the cells in range. If you do put a Range or Array in, then it will only use the first item.
{EDIT} Unless the entire function is part of an Array Calculation, in which case you will get an array of SUMIF for each item in the Array - e.g. {=SUM(SUMIF(A1:A4,{1,2}))} is the same as {=SUM({SUMIF(A1:A4,1), SUMIF(A1:A4,2)})}
This means, you can do =SUM(B1:B4,"<" & MIN(C1:C4),A1:A4) to add up where the values in column A where the value in column B is smaller than all the values in column C, but for a row-by-row comparison you will need an Array Formula or SUMPRODUCT, like so:
=SUMPRODUCT(A1:A4*--(B1:B4<C1:C4))
You can use this array formula:
{=SUM(IF(B1:B4<C1:C4,A1:A4))}
You'll need to apply it with Ctrl + Shift + Enter.
You know that you set the array formula correctly when you see the curly brackets around the formula

How to select certain rows in Excel that meet logical criteria of A & B

I have an excel sheet in CSV that has 8 columns A-H and thousands of rows with values 0 or 1 depending on truth value.
I'm looking for the Excel function in which I can select rows where column A and B are true so that I can check another columns probability given A&B. IE P((A&B)|D) (where | means given).
I'm really new to excel and having difficulties finding how to only select rows that meet this criteria.
The following formula entered in I1 will return a 1 if both A1 and B1 are true.
=IF(AND($A1=1,$B1=1),1,0)
Copy it down or autofill to identify all rows where A and B are true.
The $ sign before A and B make the column references absolute meaning if you drag the formula to the right, the references to columns A and B will remain.
Because Excel implicitly interprets 0 = FALSE and 1 (or any other number) = TRUE the formula could be shortened to:
=IF(AND($A1,$B1),1,0)
The probability of C being 1 given that A and B are 1 can be calculated by counting all rows where A, B and C are all 1 and dividing by the number of rows where both A and B are 1:
=COUNTIFS($A:$A,"1",$B:$B,"1",C:C,"1")/COUNTIFS($A:$A,"1",$B:$B,"1")
Again, references to A and B are absolute, while C is relative so you can drag right to get probabilities for columns D to H.
COUNTIFS only counts the rows where all of the criteria are met and allows you to specify up to 127 range/criteria pairs.
EDIT
You could also use:
=AVERAGEIFS(C:C,$A:$A,1,$B:$B,1)
to get the probability.

Formula returning Column A value for row containing MAX value of a range

Assume I have the following table:
A B C
1 Week 1 Week 2
2 Melissa 114.7 82.8
3 Mike 105.5 122.5
4 Andrew 102.3 87.5
5 Rich 105.3 65.2
The names are in column A, the Week values are in Row 1. (So A1 is blank, B1 = Week 1, and A2 = Melissa.)
I'm trying to build a formula that looks at all the values in a known range (in this example, B2:C5), chooses the highest value of the bunch (here, 122.5) and returns the name of the person from Column A that got that value. If I use this formula, it works for the values in range B2:B5:
=INDEX(A2:A5,MATCH(MAX(B2:B5),B2:B5,0))
That returns Melissa but if I expand the range to include more than just column B's values, I get an #N/A returned:
=INDEX(A2:A5,MATCH(MAX(B2:C5),B2:C5,0))
The weird part (to my simple brain) is that the MATCH portion of the formula works fine, if I just put in this formula, it returns the highest value of 122.5 from C3:
=MAX(B2:C5,B2:C5,0)
So clearly something it going wrong when I'm using either the MATCH or INDEX commands.
Hopefully this makes sense and someone can point out my error?
Try this:
=INDEX(A:A,MAX((B2:C5=MAX(B2:C5))*ROW(B2:C5)))
This is an array formula and must be confirmed with Ctrl+Shift+Enter.
Note: Match can only search one vector at a time. It can be one row or one column or one array. It cannot be two or more rows or columns or a 2D array.
Do it "twice"? Please try:
=INDEX(A2:A5,IFERROR(MATCH(MAX(B2:C5),B2:B5,0),MATCH(MAX(B2:C5),C2:C5,0)))
If you are going to have up to 52/53 weeks to cope with I'd suggest instead inserting a helper column with the MAX for each row. Make that an new (inserted) ColumnA (say =MAX(C2:BC2) etc.) and a simple VLOOKUP should serve, say:
=VLOOKUP(MAX(A:A),A:B,2,0)

Count with criteria for changing column in excel

I have a data looks like this:
a b c
1 3 4
2 3 3
4 1 2
2 4 2
In another worksheet, I want to do the following calculation:
whenever A1 returns a (header of data worksheet), count number of items that are smaller and equal to 2 in column "a". (result will be 2)
if A1 returns b, count number of items that are smaller and equal to 2 in column "b". (result will be 1).
A1 has already been preset with formula such that it will show a or b or c as conditions changed.
I need the formula to be lean... I actually have 6 headers, so if I keep on using if functions, I will probably have to set 6 if functions in one cell...that can be overwhelming. index match cannot provide a range to work on...Any suggestion? thanks
I don't know vba. If you could provide a workable vba code, i don't mind. but i don't know how to read it...>.< please provide user manual for that. lol, thank you~
If your data is found on Sheet1 and the a is found on column a, b is found on column b etc. enter this formula on then next sheet on b1 when a1 is the column value:
=COUNTIF(INDIRECT("Sheet1!"&a1&":"&a1),"<=2")
The Indirect is for adding text to your reference.
If your data sheet is Sheet1, you could try the array formula:-
=SUM((Sheet1!A1:C1=$A$1)*(Sheet1!A2:C5<=2))
Must be entered with CtrlShiftEnter
(actually there are 3 items less than or equal to 2 in column A)
Or you can use the SUMPRODUCT version if you prefer not to use an array formula:-
=SUMPRODUCT((Sheet1!A1:C1=$A$1)*(Sheet1!A2:C5<=2))
Or you can use this INDEX/MATCH method which is probably more efficient:-
=COUNTIF(INDEX(Sheet1!A2:C5,,MATCH(A1,Sheet1!A1:C1,0)),"<="&2)

Consolidate two or more columns of data into one column using formula

I need to collect Sch Code from different columns into one column as shown below.
First priority is by formula or UDF Function if possible.
My Data:
Column A Column B Column C Column D Column E Column F Column G
SCH Code Value SCH Code Value Rating SCH Code Value
C01-3-1 4 C01-4-1 8 300 C02-3-1 8
300 C02-3-5 9
C01-3-2 5 C01-4-2 12 300 C02-3-2 12
C01-3-3 6 C01-4-3 21 300 C02-3-3 21
300 C02-3-6 10
C01-3-4 7 C01-4-4 4 300 C02-3-4 4
Required Result (Only Sch Code required in summary sheet but it is required by formula or VBA UDF function) :
Column A
C01-3-1
C01-3-2
C01-3-3
C01-3-4
C01-4-1
C01-4-2
C01-4-3
C01-4-4
C02-3-1
C02-3-5
C02-3-2
C02-3-3
C02-3-6
C02-3-4
You can collect unique non-blank values from column A with an array formula e.g. =INDEX($A$2:$A$99,MATCH(0, IF(LEN($A$2:$A$99),COUNTIF(I$1:I1,$A$2:$A$99),1),0)). Since this returns #N/A where it has no more values to return from its column, you can pass control over to a similar formula that references another column with IFERROR.
    
To choose from your three columns of SCH Codes, you would need to stack this 3 deep. The formula in I2 is:
=IF(LEN(I1),IFERROR(INDEX($A$2:$A$99,MATCH(0, IF(LEN($A$2:$A$99),COUNTIF(I$1:I1,$A$2:$A$99),1),0)),IFERROR(INDEX($C$2:$C$99,MATCH(0, IF(LEN($C$2:$C$99),COUNTIF(I$1:I1,$C$2:$C$99),1),0)),IFERROR(INDEX($F$2:$F$99,MATCH(0, IF(LEN($F$2:$F$99),COUNTIF(I$1:I1,$F$2:$F$99),1),0)),""))),"")
This array formula requires Ctrl+Shift+Enter, not just Enter. Once entered correctly, it can be filled down to catch all possible values. I would fill down for at least three times as many rows as you have in order that the blanks would have a place if they were filled in at a later date.
In theory, you could stack this much deeper but for practical purposes, I wouldn't go much deeper than this. Array formulas eat up calculation resources at a logarithmic rate so the size of your data is going to be a key factor on whether this is a suitable solution.
One solution:
Copy and paste all values in column C below column A data. Highlight column A and go to Data>Remove duplicates then Data>Sort.

Resources