How to calculate a weighted product with index and match? - excel

The following sheet shows the data I have. The main idea is to sum up every student's grade, based on a set of weighted criteria.
Cells C9:G11 are populated with data from the list A1:B4, in which every element (Excellent to Poor) is related to a value (10 to 4).
I used the following formula to calculate the value of C12 (and C13 etc.). However, it is too naive. How could this calculation be improved?
=INDEX($B$1:$B$4;MATCH(C9;$A$1:$A$4;0)) + INDEX($B$1:$B$4;MATCH(C10;$A$1:$A$4;0)) + INDEX($B$1:$B$4;MATCH(C11;$A$1:$A$4;0))
UPDATE: I would like to consider the weighted calculation.
=INDEX($B$1:$B$4;MATCH(C9;$A$1:$A$4;0)) * B9 + INDEX($B$1:$B$4;MATCH(C10;$A$1:$A$4;0)) * B10 + INDEX($B$1:$B$4;MATCH(C11;$A$1:$A$4;0)) * B11

Use SUMPRODUCT(SUMIF())
=SUMPRODUCT(SUMIF($A$1:$A$4,C$9:C$11,$B$1:$B$4))
per your update:
=SUMPRODUCT(SUMIF($A$1:$A$4,C$9:C$11,$B$1:$B$4)*$B$9:$B$11)

Related

Excel - Return values between conditions of another column

I have a sheet that have Measure in meters, Value, and Distance columns (see attached screenshot). The Distance simply returns the difference between each row of the Measure column.
What I want to do is every time the Distance is greater than 10m (which I've used conditional formatting to highlight green), return in column E the measure values from the previous row up to the previous green cell, kind of like grouping them and displaying as a range. Then also return the min and max Value within that range of rows (probably in column F & G).
Ex. E6 would return something like "148000-148040" in text format, E16 would be "148090-148180", E17 would just be "148200". Then F6 = -91.09, G6 = -43.91, F16 = 15.49, G16 = 138.06 and so on.
Not sure if this would require VBA, but if it can be done with formulas that'd be great.
Sorry if this is confusing or if a similar question has been asked before.
Thank you!
I think this demonstrates the power of the new (ish) Xlookup very well (in this case, using backwards search):
For the range:
=IF(D3=10,"",XLOOKUP(TRUE,D2:D$2>10,B2:B$2,B$2,0,-1)&"-"&B2)
Min:
=IF(D3=10,"",MIN(XLOOKUP(TRUE,D2:D$2>10,C2:C$2,C$2,0,-1):C2))
Max:
=IF(D3=10,"",MAX(XLOOKUP(TRUE,D2:D$2>10,C2:C$2,C$2,0,-1):C2))

How to dynamically use the formula based on filter in excel?

I'm having a hard time figuring out how to get the total value without using the SUM function.
Example:
I can get the total value of SumSQ using SUM function like I did in the image.
Expected value is: 30323295
Formula of SumSQ: SumSQ = (SD^2 * count * (count - 1) + (SumX^2) ) / count
In excel: SumSQ =(SUMSQ(D3) * B3 * (B3-1)+(SUMSQ(C3)))/B3
Note that the SumSQ is kind of a helper column.
What I want is avoid using a helper column, in this case SumSQ. But I still want to get the total value which is 30323295.
So basically this is how it should look like without filter:
Formula for Total SumSQ: SumSQ = (total_SD^2 * total_count * (total_count - 1) + (total_SumX^2) ) / total_count
And note this must be dynamic based on filter on column A.
Example with filter in PC_Number:
It means that it should only compute what is only visible based on filter.
I have tried using SUBTOTAL() function added with OFFSET(), MIN(), and ROW() but it wont work as what I wanted to.
This is currently what I have:
--start
=SUMPRODUCT(((SUBTOTAL(9,OFFSET(U5:U256,ROW(U5:U256)-MIN(ROW(U5:U256)),,1))^2)*SUBTOTAL(9,OFFSET(M5:M256,ROW(M5:M256)-MIN(ROW(M5:M256)),,1))*(SUBTOTAL(9,OFFSET(M5:M256,ROW(M5:M256)-MIN(ROW(M5:M256)),,1))-1)+((SUBTOTAL(9,OFFSET(Q5:Q256,ROW(Q5:Q256)-MIN(ROW(Q5:Q256)),,1))*SUBTOTAL(9,OFFSET(M5:M256,ROW(M5:M256)-MIN(ROW(M5:M256)),,1)))^2))/SUBTOTAL(9,OFFSET(M5:M256,ROW(M5:M256)-MIN(ROW(M5:M256)),,1)))
--end
This is the output using the formula I have:
Which have a big difference on the total value.
Not sure I explained it well but that's the gist of it.
Normal formula, not array formula, just using SumProduct:
=SUMPRODUCT((D3:D9*D3:D9*B3:B9*(B3:B9-1)+(C3:C9*C3:C9))/B3:B9)
OR
=SUMPRODUCT((D3:D9^2*B3:B9*(B3:B9-1)+(C3:C9^2))/B3:B9)
EDIT 1: filtering
To Add some filtering by column A, suppose you have to filter by a value that you enter in A1:
=SUMPRODUCT((A3:A9 = $A$1)*((D3:D9^2*B3:B9*(B3:B9-1)+(C3:C9^2))/B3:B9))
EDIT 2: to make the filtering optional, the next formula applies the filtering unless A1 is blank, in which case it accounts for on all the rows:
=SUMPRODUCT((A3:A9=IF(ISBLANK(A1),A3:A9,A1))*((D3:D9^2*B3:B9*(B3:B9-1)+(C3:C9^2))/B3:B9))
EDIT 3: to take into account only visible (non-filtered out) rows:
=SUMPRODUCT(SUBTOTAL(2, OFFSET(B1, ROW(B3:B9)-1,0))*((D3:D9^2*B3:B9*(B3:B9-1)+C3:C9^2)/B3:B9))
Idea is that the subtotal(2) on an individual and numeric cell returns 1 (0) if the cell is visible (hidden).
You need to use an array formula, and I don't believe you can use SUMSQ(). The below prodcues the desired result. Please not this is to be entered with Shift + Ctrl + Enter.
{=SUM(((D3:D9)^2 * (B3:B9)*((B3:B9)-1)+((C3:C9)^2))/B3:B9)}

Tie breaking using Rank function

I have a student's result sheet, where I calculated the total number the student has gained. Next, I want to rank them according to the grand total, and wherever it duplicates, the student getting more number in maths(E9:E58) will be given priority,
How to accomplish it?
I have my grand total from U9 to U58, and maths score in d9 to d58.
I have used this formula:
=Rank(u9,u9:u58,1)
I am stuck in how to use the if condition within the rank function.
Please help.
Thanks in advance....
In that sort of situation, one method of adjusting is to use the SUMPRODUCT function. You will need to adapt my example to your data ranges. In the example below, you don't need columns E:F. They are only there so you can better see how the adjustment is accomplished.
Adjustment Function:
=SUMPRODUCT((C2=Totals)*(B2>Maths))
adj Rank Function
D2: =RANK(C2,Totals,1)+SUMPRODUCT((C2=Totals)*(B2>Maths))
Assuming, Total score is in Column U and Math's score is in Column E, you can use Ron's solution as:
=RANK(U9,U$9:U$58)+SUMPRODUCT((U$9:U$58=U9)*(E$9:E$58<E9))
Enter above formula in 9th row of the column where you want Rank to be displayed and then drag/copy it down till 58th row.
This is more of arithmetic approach. I would add a column called adjusted total.
AdjTotal = Total + (Maths Score / Max possible Maths Score) * 0.9
Then use this Adjusted score column to rank.
The adjustment will add a fraction under 1 to the total and will help break the tie when there is one. Since the adjustment will never be more than 0.9 it will not affect ranks when there is no tie.
Assuming Max score for Maths is 100, use this
V9 = U9 + D9/100*0.9
Copy V2 down till V59
W9 = RANK(V9, V$9:V$59, 1)
Copy W9 down.
You can do this by running the usual RANK() method and then adding in the RANK() method on a filtered set that matches the other rows tied with the current row. Since the RANK() call for the tiebreak will start at one you will need to subtract one to compensate.
The end result ends up looking like:
=RANK(U9, U$9:U$58, 1) + RANK(D9, FILTER(D$9:D$58, U$9:U$58=U9)) - 1

How to calculate average value based on range of data

I want to calculate an average value based on corresponding values which fall into a certain criteria.I have setup a online sheet to illustrate the case, hopefully you can help me to get this working
https://docs.google.com/spreadsheets/d/1Q_GQW90e0Q4KaJnvaekh8OGI1Km0Rs1uS8JKsOCqqK4/edit?usp=sharing
In C1 cell write formula as below:
=averageifs(available_data!C:C, available_data!C:C, ">"&$A3 - 0.5, available_data!C:C, "<"&$A3 + 0.5)

average with multiple criteria including sum instead of array

I'm trying to bulid a formula that could do the following: calculate an average for a variable that meets couple conditions. The problem with this case is that I want the average to be calculated when a sum of investments in specified year, and types is within high (K4) and low (J4) values.
In this example the expected result would be variable a = 15% - conditions would be year - 2015, type - b & c (from a range H8:H10, order of a,b,c may vary), sum of investments for previous criteria within 400 and 600. Here type c do not meet criteria becasue the sum of investments is 900, so out of range specified before. Type b has investments equal to 500 so the average is calculated.
Any ideas how can I handle this? Thanks.
This is a first try at it, but the formula is still a huge monster. There might be a better, more elegant way. But, at least it works.
=(IF(AND(SUMIF($D$4:$D$21,$H$8,$E$4:$E$21)>=$J$4, SUMIF($D$4:$D$21,$H$8,$E$4:$E$21)<=$K$4), SUMIFS($F$4:$F$21,$B$4:$B$21,$H$4,$C$4:$C$21,$I$4,$D$4:$D$21,$H$8), 0) + IF(AND(SUMIF($D$4:$D$21,$H$9,$E$4:$E$21)>=$J$4, SUMIF($D$4:$D$21,$H$9,$E$4:$E$21)<=$K$4), SUMIFS($F$4:$F$21,$B$4:$B$21,$H$4,$C$4:$C$21,$I$4,$D$4:$D$21,$H$9), 0) + IF(AND(SUMIF($D$4:$D$21,$H$10,$E$4:$E$21)>=$J$4, SUMIF($D$4:$D$21,$H$10,$E$4:$E$21)<=$K$4), SUMIFS($F$4:$F$21,$B$4:$B$21,$H$4,$C$4:$C$21,$I$4,$D$4:$D$21,$H$10), 0)) / (COUNTIF($D$4:$D$21,$H$8)*--(IF(AND(SUMIF($D$4:$D$21,$H$8,$E$4:$E$21)>=$J$4, SUMIF($D$4:$D$21,$H$8,$E$4:$E$21)<=$K$4), SUMIFS($F$4:$F$21,$B$4:$B$21,$H$4,$C$4:$C$21,$I$4,$D$4:$D$21,$H$8), 0)<>0) + COUNTIF($D$4:$D$21,$H$9)*--(IF(AND(SUMIF($D$4:$D$21,$H$9,$E$4:$E$21)>=$J$4, SUMIF($D$4:$D$21,$H$9,$E$4:$E$21)<=$K$4), SUMIFS($F$4:$F$21,$B$4:$B$21,$H$4,$C$4:$C$21,$I$4,$D$4:$D$21,$H$9), 0)<>0) + COUNTIF($D$4:$D$21,$H$10)*--(IF(AND(SUMIF($D$4:$D$21,$H$10,$E$4:$E$21)>=$J$4, SUMIF($D$4:$D$21,$H$10,$E$4:$E$21)<=$K$4), SUMIFS($F$4:$F$21,$B$4:$B$21,$H$4,$C$4:$C$21,$I$4,$D$4:$D$21,$H$10), 0)<>0))
Here a screenshot:
I have named the columns with ranges (type: data for column type D4:D21, invst: E4:E21, year:b4:b21, min:cell j4, max: cell k4, pcg:f4:f21) for ease of understanding.
Paste this in h8 as array formula (for a with year filter) and name this cell "suma"
=SUMIFS(invst,type,H8,year,2015) # for type a
paste this in h9 as array formula (for b) and name it sumb
=SUMIFS(invst,type,H9)
paste this in j8 (answer for type a) as array formula
=IFERROR(AVERAGE(IF(AND(suma>min,suma<max),IF(type=H8,pcg,""),"")),"")
paste this in j9 (answer for type b) as array formula
=IFERROR(AVERAGE(IF(AND(sumb>min,sumb<max),IF(type=H9,pcg,""),"")),"")

Resources