Summing the result of nested IF statements in one cell - excel

I've got a pretty complex conditional formula that works for each row of a column (sorry, no excel 2016 IFS) and I would like to get the sum of all instances in a range in one formula without having to make all the rows as a middle step.
Done this quite a bit with other stuff, but for some reason I'm stuck on this one.
The formula per cell is:
=IF((IF(IF(AND(ISNUMBER(Test_Samples!B2),Test_Samples!B2>0.1),Reference_Dataset!$H:$H,"")="N",Test_Samples!B2,0)<1),0,IF(IF(IF(AND(ISNUMBER(Test_Samples!B2),Test_Samples!B2>0.1),Reference_Dataset!$H:$H,"")="N",Test_Samples!B2,0)<2,1,IF(IF(IF(AND(ISNUMBER(Test_Samples!B2),Test_Samples!B2>0.1),Reference_Dataset!$H:$H,"")="N",Test_Samples!B2,0)<5,2,IF(IF(IF(AND(ISNUMBER(Test_Samples!B2),Test_Samples!B2>0.1),Reference_Dataset!$H:$H,"")="N",Test_Samples!B2,0)<13,3,IF(IF(IF(AND(ISNUMBER(Test_Samples!B2),Test_Samples!B2>0.1),Reference_Dataset!$H:$H,"")="N",Test_Samples!B2,0)<34,4,IF(IF(IF(AND(ISNUMBER(Test_Samples!B2),Test_Samples!B2>0.1),Reference_Dataset!$H:$H,"")="N",Test_Samples!B2,0)<91,5,IF(IF(IF(AND(ISNUMBER(Test_Samples!B2),Test_Samples!B2>0.1),Reference_Dataset!$H:$H,"")="N",Test_Samples!B2,0)<245,6,IF(IF(IF(AND(ISNUMBER(Test_Samples!B2),Test_Samples!B2>0.1),Reference_Dataset!$H:$H,"")="N",Test_Samples!B2,0)<666,7,))))))))
I would like to transform it to a formula that sums everything from the range B:B (or B2:B499) in one go.
I tried some SUM and SUMIF(S) stuff and changing B2 to B:B. That doesn't seem to work.
Oh, if someone has a tip to reduce the nested IF formula to something more readable, that's welcome as well. The idea of the formula is to transform counts to classes.
The datasets that are referred to look like this:
Test_Samples:
Reference_Dataset:
The If statements make up a classification as follows:
0= 0
1= 1
2= 2-4
3= 5-12
4= 13-33
5= 34-90
6= 91-244
7= 245-665
8= 666+
Here you see a count of 2 in "Test_samples", and it is labelled "N" in "Reference_dataset", so the result classifies it as "2" (to avoid confusion: if the count was 5 it would be labelled "3" according to the class criteria).
Say if there are 5 instances with result "2" in the range B2:B499, the sum should be 10.

Create a lookup table like this for your "result value < X" to summarize the If statements into a single lookup table:
Test_Samples B Value
0
1
2
5
13
34
91
245
666
In this example, i've put that on the same worksheet that the formula is placed, in cells A1:A10 (header in A1, so data values in A2:A10). Then you can simplify your formula and make it reference the range of your data like this:
=SUM(MATCH(IF(ISNUMBER(Test_Samples!$B$2:$B$499)*(Reference_Dataset!$H$2:$H$499="N"),Test_Samples!$B$2:$B$499,0),$A$2:$A$10)-1)
Note that this is an array formula and as such must be confirmed with CtrlShiftEnter (instead of just Enter).

Related

How to create a dynamic formula to find the average of a set of values for a given vector

I am trying to create a formula that gives me the average of the last 12 entries in a given dataset depending on the associated vector.
Let's make an example:
I have in column F2,G2,H2 and I2 dates, Company1, Company2 and Company3 respectively. Then from row3 to row 33 I have months dates starting from May 2016.
Date Company1 Company2 Company3
May-16 2,453,845
Jun-16 13,099,823
Jul-16 14,159,037
Aug-16 38,589,050 8,866,101
Sep-16 63,290,285 13,242,522
Oct-16 94,005,364 14,841,793
Nov-16 123,774,792 7,903,600 41,489,883
Dec-16 93,355,037 12,449,604 69,117,105
Jan-17 47,869,982 13,830,712 83,913,764
Feb-17 77,109,905 10,361,555 68,176,643
The goal is to create a formula that, when I drag it down, correctly calculates the average of the last 12 values for a given company.
So for example i would have, say in table "B2:C5":
Company1 76,856,345
Company2 11,120,859
Company3 65,674,349
And, if a new Company4 is added to the list, then I just have to drag it down the formula, to calculate the average of the last 12 months for Company4.
Until now, I have came up with this formula:
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(G:G),ROW(G:G)),ROW(INDIRECT("1:"&MIN(12,COUNT(G:G))))),ROW(G:G),G:G ))
This formula correctly calculates the average of a given column, considering only the last 12 values. The last step would be to come up with a formula that includes all the columns and then calculates the average for the given company.
Thanks!
I recommend that you use a named range to define your data in columns G:I. When a company is added, just modify the named range's specs. I used the name Target. Of course, you can replace it with $G:$I if you feel so inclined but I would rather recommend reducing the number of rows in the range, which is easier to manage when it is named.
Use the formula below to extract the company names from the first row of Target into the first column of your averages table. This is to ensure that the names are spelled identically in both locations.
=INDEX(Target,1,ROW()-2)
The number 2 indicates the number of rows above the row containing the formula. it is copied here from cell M3. There, ROW()-2 creates the number 1, counting sequentially as the formula is copied down.
Now I have the formula below in my cell N3 and copied down.
=SUM(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))
The formula simply sums up the columns G, H, and I in 3 consecutive rows.
In the final step I inserted the range definition established above, meaning excluding the SUM() function, into your existing formula.
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))),ROW(INDIRECT("1:"&MIN(12,COUNT(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))

Excel: Obtain a column by sorting anotr one values

I need to automatically obtain a sorted column of values from another given column values, like in the sample:
I have I need A unchanged, and also B obtained from A
A A B
-----------------
1 1 0
0 0 0
3 3 1
8 8 3
0 0 8
I mean if the values from A changes, the B should change accordignly...
Is that possible in MS Excel?
Here a sandbox and sample:
http://1drv.ms/1SkqMhS
If you put The formula =SMALL(A:A,ROW()) in B1 and copy down then the cells in B will be linked to the cells in A in such a way that the numbers in B will be the numbers in A in sorted order. This won't be efficient for larger ranges but will work fine for small to medium size ranges.
If you want the numbers to start in a lower row, say B2 because you have a header in B1, adjust ROW() to something like ROW()-1.
A word of warning: Use of ROW() can make a spreadsheet somewhat fragile in that formulas that involve it can change their meaning if rows are inserted or deleted or the block containing the formula is moved to somewhere else. Rather than using ROW(), there is something to be said for adding a helper column which numbers the data in A (which would then be in e.g. B) and referring to these numbers rather than small. For example, in:
If I put the formula
=SMALL($B$2:$B$5,A2)
In C1 and copy down, it works as intended. In response to a question you raised in the comments, I added still another column which gives an index where the corresponding value occurs. To do this I wrote in D2 (then copied) the formula
=MATCH(C2,$B$2:$B$5,0)
Of course. Highlight your range and in the Data tab, click "Sort", then you can choose how you want to sort your data:
If column B has information that is to be used with Column A (like next to A1 is "Car"), and you want to sort the whole table, based on Column A, then just select Columns A and B, then sort by column A.
Found the answer, thanks to John Coleman !
Just some minor details like cell value fixing (with $, like A$2)and the -1+ROW adjustment for the 1 header row!

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)

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)))

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