SUMIF statement returns a value from the incorrect row - excel

My current formula on Worksheet1 is below:
SUMIF(Setup!$C$5:$C$375,"C-R",'PMS Input'!$K$13:$K$416)
On the Setup worksheet, I have confirmed that there is only one C-R in column C.
Row C39 C-R CAMPSITES
Row C40 G-S GROUP SHELTER
Row C41 G-S PICNIC SHELTER
Row C42 P-A SQUIRREL'S NEST
Row C43 P-A MISC. PARK REVENUE/VENDING
Row C44 P-A MINIATURE GOLF
Row C45 P-A CANOE RENTALS
Row C46 P-A BICYCLE RENTALS
Row C47 P-A PROGRAMMING FEES
Row C48 P-A MISC. PARK REVENUE/VENDING
On the PMS Input worksheet, the rows are as follows:
Row K48 CAMPSITES 9 18.25
Row K49 GROUP SHELTER - -
Row K50 PICNIC SHELTER - -
Row K51 SQUIRREL'S NEST - (195.00)
Row K55 BICYCLE RENTALS - -
Row K57 PROGRAMMING FEES - 108.00
Row K58 MISC. PARK REVENUE/VENDING - -
On Worksheet1, the value returned for the above formula looking for C-R is 0.
The actual responses should be C-R = 18.25. The incorrect response in consistent throughout the file.
I have three other locations using the same file, with no issues using the above formula.
Another formula issue:
=SUMIF($B$63:$D$82,"TIPS",R$63:R$82)
This formula is returning a value of 17, however there is no 17 in column R. The 17 is actually in column T. This same error is repeating on several other days.
I have compared, checked and re-checked the formulas, but to no avail.
Could this just be a problem with the overall worksheet, or have I been staring at my numbers too long?

On the second issue you need to know that SUMIF always works on a "one to one" basis. That means that both ranges need to be the same size and shape. If the second range isn't the same size and shape as the first then Excel implicitly makes it so, starting from the top left cell of range 2.
In short that means that with your formula like this:
=SUMIF($B$63:$D$82,"TIPS",R$63:R$82)
The sum range is extended to be the same size as the criteria range so that formula will do the same as this
=SUMIF($B$63:$D$82,"TIPS",R$63:T$82)
Note: second range covers 3 columns like the first. If any value in colums C and D are "TIPS" it will sum the relevant value from column S or T, so that's probably where your 17 comes from.
Can you explain in words what you are trying to do with this formula? Do you want to sum column R only if any value in B, C or D is equal to "TIPS" - what if all 3 cells are equal to "TIPS" in one row, do you want to sum column R 3 times for that row?

Your lookup and your values are not aligned correctly. C-R is on C39, and the value is on K48. They are 9 rows apart. However the lookup and values in your equation start eight rows apart.
So either you have an extra row in PMS or missing a row in Setup, or you just started at the wrong row and something like this would fix it:
SUMIF(Setup!$C$5:$C$375,"C-R",'PMS Input'!$K$14:$K$416)
SUMIF(Setup!$C$4:$C$375,"C-R",'PMS Input'!$K$13:$K$416)

to fix the second issue you'll need the following formula (based on the great detail provided by barry in his answer)
=SUMIF($B$63:$B$82,"TIPS",$R$63:$R$82)+SUMIF($C$63:$C$82,"TIPS",$R$63:$R$82)+SUMIF($D$63:$D$82,"TIPS",$R$63:$R$82)

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

All combinations of 4 out of 7 columns with totals using excel

I have 7 columns to choose from and I need to pick 4 of those columns and generate a total for each row. I also need every combination of 4, which means I'll have 35 new columns with the totals for each of those combinations showing in each row. I need the code for this and if it can be done only using Excel. Here is an image of the columns and the grayed ones are the 7 columns I'm talking about. My knowledge of Excel is very limited. There are over 1,500 rows if that matters.
multi step approach that is going to use some helper rows. there may be a more elegant formula that will do this, and much slicker options in VBA, but this is a formula only approach.
Step 1 - Generate List of Column Combination
To generate the list 4 helper rows will need to be insert at the top of your data. either above or below you header row. These 4 rows will represent the column number you are going to pick. To keep the math simpler for me I just assumed the 1 for the first column and 7 for the last column. those numbers will get converted to later to account for column in between in your spreadsheet. For the sake of this example The first combination sum will occur in column AO and the first helper row will be row 1. The first combination will be hard coded and it will seed the pattern for the remainder of column combinations. Enter the following values in the corresponding cells:
AO1 = 1
AO2 = 2
AO3 = 3
AO4 = 4
In the adjacent column a formula will be placed and copied to the right. It will automatically augment the bottom value by 1 until it hits its maximum value at which point the value in the row above will increase by 1 and the the value of the current will be 1 more than the cell above. This will produce a pattern that covers all 35 combinations by the time column BW is reached. Place the formulas below in the appropriate cell and copy to the right:
AP1
=IF(AO2=5,AO1+1,AO1)
AP2
=IF(AO2=5,AP1+1,IF(AO3=6,AO2+1,AO2))
AP3
=IF(AO3=6,AP2+1,IF(AO4=7,AO3+1,AO3))
AP4
=IF(AO4=7,AP3+1,AO4+1)
Step2 - Sum The Appropriate Columns
I was hoping to use a some sort of array type operation to read through the column reference numbers above, but I could not get my head around it. Since it was just 4 entries to worry about I simply added each reference manually in a SUM function. Now the important thing to note is that we will be using the INDEX function over the 13 columns that cover the range of your columns so to convert the index number we figured out above, to something that will work to grab every second row, the number that was calculated will be multiplied by 2 and then 1 will be subtracted. That means 1,2,3,4 for the first column combination becomes 1,3,5,7. You can see this in the following formula. Place the following formula in the appropriate cell and copy down and to the right as needed.
AO5
=INDEX($AB5:$AN5,AO$1*2-1)+INDEX($AB5:$AN5,AO$2*2-1)+INDEX($AB5:$AN5,AO$3*2-1)+INDEX($AB5:$AN5,AO$4*2-1)
pay careful attention to the $ which will lock row or column reference and prevent them from changing as the formula is copied.
Now you may need to adjust the cell references to match your sheet.

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)

Index/Match Multiple Criteria, Perform Calculation for Each Match

I have two columns in a table that are known as S and D, where S is a date, and D is a duration.
E.g.:
'S' Column
January
February
March
April
'D' Column
60
30
45
30
On a separate sheet, imagine that Row 1 contains a sequence of dates (variable, depends on user menu selection).
Row 2 requires the following calculation:
[(x - s1)/d1 + (x-s2)/d2 + ... + (x-sn)/dn] / n
...where "x" is any date along Row 1.
The calculation would only be done when multiple criteria are matched.
My initial attempt involved creating a separate table, but I think this can be done in a one-cell formula in Row 2. I don't think a sum(index(match)) type would work here considering d1, d2, ..., dn are denominators with different values.
Here is an example attempt:
=SUM((SelectedDate-INDEX(Table[StartDates],MATCH(Criteria1&Criteria2,Range1&Range2,0))/INDEX(Table[Durations],MATCH(Criteria1&Criteria2,Range1&Range2,0))))
It may be important to note that I am able to do this in a two-step fashion. First, I create a table that does the calculation on each row. Then, I reference the table. It would be nice if I can eliminate the need of a "calculation table" in favour of an array-type formula.
I took a guess before most of the comments and suggest the following mainly in case it provides ay ideas or is of help in specifying the requirement more closely. This is "two-step" because the average formula is separate from that for each column, so I appreciate may well be not what is required.
Assuming S and D are labelled data in Sheet2 ColumnsA:B.
Assuming Row1 data rows are labelled in ColumnA.
Assuming dates are not strings (eg January is actually Jan 1) and the differences are in days and always positive, etc.
=(B$1-CHOOSE(COLUMN(),Sheet2!$A1,Sheet2!$A2,Sheet2!$A3,Sheet2!$A4))/CHOOSE(COLUMN(),Sheet2!$B1,Sheet2!$B2,Sheet2!$B3,Sheet2!$B4)
in Row2 to suit and if four data columns, in F2 and copied down to suit :
=AVERAGE(B2:E2)

If column A matches column C input column D into column B

I have a sales tracking sheet where column A contains the profit margin of a particular job (i.e.33%), Column C is the profit margin range(i.e. 31-40%), and Column D is the corresponding commission to that specific range identified in Column C (i.e. 31-40% = 3% commission).
What I want is a formula that will automatically pull the Commission from Column D into Column B when I enter the profit margin of that particular job in Column A.
Any ideas/does that make sense?
Assuming that the values in column A are formatted as percentage, you could use something like this:
=INDEX(D$1:D$10,MATCH(A1*100,1*LEFT(C$1:C$10,FIND("-",C$1:C$10)-1),1))
And press Ctrl+Shift+Enter after entering the formula instead of Enter alone.
This will return a value from range D$1:D$10 where the value from A1 (multiplied by 100 to remove the decimals) is less than the lower bound of the margin range in range C$1:C$10.
Change the ranges accordingly.
In B1, put:
=IF(A1=C1,D1,0)
You can obviously change row numbers to work as needed.
The IF statement has 3 parts, the condition:
=IF(A1=C1,
Here I'm testing to see if the expression is TRUE or FALSE. I can do anything I want here, as long as it evaluates to either a True or False condition.
Next, we specify the "True" result, and the "false" result, which are, respectively, what happens when those conditions are met. For the TRUE condition, we just want to use the value in cell D:
D1,
For the FALSE condition, I don't know what you want, so I just put in a 0.
0)
Note that all 3 parts of the IF statement are separated by commas - play around, you can do a LOT of different things!
EDIT: Just noticed that column C is a range, while A is a singular value. You're going to need to do something like #Jerry did with parsing out the range string.
I am assuming that columns A and B will be indefinitely long just based on how much data is collected, whereas columns C and D are just a reference table with 10 rows each for the 10 ranges (0 - 10%, 11 - 20%, 21 - 30%, etc.). Is this correct?
As an alternative to storing the profit margin range and corresponding commission in columns C and D as you now do, you could incorporate them directly into an IF statement that you use in column B. For example if 91-100% corresponds to 8% commission, 81-90% is 7% commission and so on, then you could insert this formula:
=IF(A2>90,0.08,IF(A2>80,0.07,IF(A2>70,0.06,IF(A2>60,0.05, ...
The advantage to this compared to using and index-match combination which references numbers extracted from the text ("11-20%"), is that you don't run the risk of losing data when the text ranges are altered in some way. (I.e. userproof.)

Resources