How to calculate average value based on range of data - excel

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)

Related

Excel Calculate a running average on filtered data with criteria

I am trying to calculate a running average on a filtered data table. All other posts online use Sumproduct(Subtotal) on the entire range but do not calculate a row by row running average
I am stuck on how to calculate columns C and D.
If column B (Score) > 0, I want to sum and average it under column C (Average Win)
If column B (Score) < 0, I want to sum and average it under column D (Average Loss)
The table is filterable by column A (Type) and the results should look as follows
Progress so far:
I have figured out how to calculate a Cumulative score based on filtered data. However this does not fully solve my problem. I appreciate any help!
=SUBTOTAL(3,B3)*SUBTOTAL(9,B$3:B3)
SUBTOTAL(3,B3) checks if the current row is visible, SUBTOTAL(9,B$3:b3) sums the values.
Final update needed
Jos - Thank you for your detailed explanation on how subtotal() works. I learned a ton through your explanation and will continue to study it. This is my first time being exposed to structured referencing so some of the syntax is a bit confusing to me still
The last formula I need is a running win % column where a Win is defined by score > 0. Please see the picture below
My assumptions believe that the same formula would work, except that we average a 1 or 0 in each row instead of the [Score] column.
Using the prior solution, why can't we modify the output of your prior solution to calculate a running win %?
[...] IF([Score]>0,IF(ROW([Score])<=ROW([#Score]),[Win])))),0)
Where [Win] is a helper column with the outputs 1 for win, 0 for loss.
This could be done by saying
if([#score]>0,1,0)
Instead of averaging out the actual #Score, this would average out a column of 1's and 0's with the desired output 0%, 50%, 66%, etc.
I am aware that the solution I provided does not work but I am trying to embrace the correct logic. I still struggle to understand how these structured column references are calculated on a row by row basis.
For example: Average(If([Score]>0,[Score])
How is this calculated on a row by row basis? When A3 does If([Score] > 0,), does this equal If({-10}>0)? When on A4, does If([Score]>0) equal If({-10,20} >0)? Thank you for your patience and help thus far.
I disagree with your result for Average Loss for the last row of your unfiltered table (surely -9.33...?), but try this for Average Win:
=IFERROR(AVERAGE(IF(SUBTOTAL(3,OFFSET(INDEX([Score],1),ROW([Score])-MIN(ROW([Score])),)),IF([Score]>0,IF(ROW([Score])<=ROW([#Score]),[Score])))),0)
Same formula for Average Loss, changing [Score]>0 to [Score]<0.
Explanation:
Using the data you provided and assuming:
The table's top-left cell is in A1
The table is filtered on the Type column for "A"
In order to determine which rows are filtered, we must pass an array of range references - i.e. for each cell within a chosen column of the table - to the SUBTOTAL function. It's a touch unfortunate that such an array of range references can only be generated via a volatile function (INDIRECT or OFFSET), but here, unless we resort to helper columns, we are left with no choice.
INDEX([Score],1)
simply returns a range reference to the first cell within the Score column. When using Excel tables, it's preferable not to write formulas which include a mixture of structured and non-structured referencing, even if that results in slightly longer expressions. So here, for example, we would not reference A2 within the formula.
ROW([Score])-MIN(ROW([Score]))
generates an array of integers from 0 up to one fewer than the number of rows in the table, i.e.
{0;1;2;3;4}
and so
=IFERROR(AVERAGE(IF(SUBTOTAL(3,OFFSET(INDEX([Score],1),ROW([Score])-MIN(ROW([Score])),)),IF([Score]>0,IF(ROW([Score])<=ROW([#Score]),[Score])))),0)
becomes
=IFERROR(AVERAGE(IF(SUBTOTAL(3,OFFSET(A2,{0;1;2;3;4},)),IF([Score]>0,IF(ROW([Score])<=ROW([#Score]),[Score])))),0)
OFFSET then generates an array of range references (though note that you will not be able to 'see' this step within the Evaluate Formula window - rather, an array of #VALUE! errors is displayed):
=IFERROR(AVERAGE(IF(SUBTOTAL(3,{A2;A3;A4;A5;A6}),IF([Score]>0,IF(ROW([Score])<=ROW([#Score]),[Score])))),0)
SUBTOTAL then determines which of these range references is filtered (note that care must be given here to the choice of first parameter), returning the relevant Boolean, so that:
SUBTOTAL(3,{A2;A3;A4;A5;A6})
resolves to:
{1;1;1;0;1}
And so we now have:
=IFERROR(AVERAGE(IF({1;1;1;0;1},IF([Score]>0,IF(ROW([Score])<=ROW([#Score]),[Score])))),0)
and the rest is straightforward.
So, I would use averageifs().
=averageifs(B:B,B:B,">=1",A:A,"A")
is one example, note I have added the control of Type A in the example.
See:

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

Getting the PERCENTRANK.INC in PowerPIvot/DAX

I have been tasked with converting a a worksheet from standard Excel into PowerPivot. However, I have hit a roadblock with the PERCENTRANK.INC function which is not available in DAX. I have come close to replicating it using a formula, but there are definite differences in the calculation.
Does anyone know how Excel calculates PERCENTRANK.INC()?
Formula in cell D2: =(COUNT($A$2:$A$10)-B2)/(COUNT($A$2:$A$10)-1)
Formula in cell B2: =RANK.EQ(A2,$A$2:$A$10,0)
Formula in cell C2: =PERCENTRANK.INC($A$2:$A$10,A2)
Edit:
It's seems strange to me that there are so many "standard" ways to calculate PERCENTRANK that has have slightly different results.
Using your example of your 9-number set of 1,2,3,4,4,6,7,8,9, depending on which "authority" I used, the third value (3) had a Percent Rank of 25.0%, 27.0%, 27.8% or 30.0%.
Obviously we'll go with the one that gives your desired result, matching PERCENTRANK.INC.
   PERCENTRANK.INC is calculated as:
          [count of values lower than the given value]
                             ÷
    [count of all values in the set excluding the given value]
so, if our range of 1,2,3,4,4,6,7,8,9is in A1:A9, we could use this formula in B1:
=COUNTIF($A$1:$A$9,"<"&A1)/(COUNT($A$1:$A$9)-1)
...and copy or "fill" it down for results:
0.0%, 12.5%, 25.0%, 37.5%, 37.5%, 62.5%, 75.0%, 87.5%, 100.0%
Original Answer
I think you just want to calculate the PERCENTRANK for current row
value. Based on the logic for PERCENTRANK, we can add a RANK column in
table and achieve same logic based on this column.
Create a Rank column.
Rank = RANKX(Table6,Table6[Value])
Then create the PctRank based on the Rank column.
PctRank = (COUNTA(Table6[Name])-Table6[Rank])/(COUNTA(Table6[Name])-1)
(Source)
For reference here is the DAX formula based off ashleedawg's answer which includes ignoring cells with no values in them.
=ROUNDDOWN(COUNTROWS(FILTER('Lookup_Query','Lookup_Query'[Entrances]<EARLIER('Lookup_Query'[Entrances]) && ISBLANK('Lookup_Query'[Entrances])=FALSE()))/(COUNTROWS(FILTER('Lookup_Query',ISBLANK('Lookup_Query'[Entrances])=FALSE()))-1),3)

How to calculate a weighted product with index and match?

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)

Using VLOOKUP to return information based on the maximum value in a range

I am trying to use vlookup to return information based on the maximum value of a cell within a range.
I have a table in which each row is a sports team and each column is a weekly points total.
I have 2 cells that show the highest points scored in the current week and the name of the team that scored them.
The team name is generated by the formula
=VLOOKUP(MAX(Admin!G3:G16),Admin!G3:K16, 5, FALSE)
This works fine.
I also wish to have 2 cells that show the highest weekly points total ever achieved, along with the name of the relevant team.
However, I can not amend the above formula to work on a range instead of a row, and no matter what I do, it always returns an #N/A result for the team name.
The most logical formula I tried is
=VLOOKUP(MAX(Admin!N18:BE31),Admin!N18:BG31, 47, FALSE)
where columns N through BE are the weekly scores (which are generated by a formula, if that makes a difference?) and column BG contains the team names.
Maybe this function isn't designed to work on a range in the same way as a column, maybe I have made an error in the formula, or maybe there is another better way to retrieve the information?
I just can't work it out, so am sincerely hoping somebody can point me in the right direction.
Many thanks for any assistance.
Indeed, the vlookup function works only with columns. So does the match function. You need here to use a formula array (hit "Ctrl + Shift + Enter" once you've typed the function instead of the usual "Enter").
I came to the following function
=INDEX($A$2:$A$9,SMALL(IF($B$2:$D$9=$H$2,MATCH($A$2:$A$9,$A$2:$A$9,0),""),1))
with :
1) $A$2:$A$9 = The teams
2) $B$2:$D$9 = The weekly scores
3) $H$2 = the maximal score ever achieved
You just need to adapt the ranges and hit "Ctrl + Shift + Enter"
PS: If 2 teams achieved this score, then only the first one will show

Resources