Average top n values where next cell equals value - excel

Having below data I would like to calculate average of top n values or top n% of values in column B where values in column A = C1.
I tried many different formulas but I can't get it to working.
{=AVERAGEIFS(B1:B14,A1:A14,C1,B1:B14,(IF(B1:B14>PERCENTILE(B1:B14,0.7),B1:B14)))}
{=AVERAGE(IF(A1:A14=C1,IF(B1:B14>PERCENTILE(B1:B14,0.7),B1:B14)))}
A B C
1 a 1 a
2 a 1 cs
3 cs 1 ffs
4 a 1 .
5 a 1 .
6 ffs 1 .
7 a 1 .
8 a 1 .
9 as 1 .
10 a 1 .
11 sfaq 1 .
12 a 1 .
13 aasf 1 .
14 a 1 .
15 a 1 .
16 a 1 .
17 qw 1 .
. . . .
. . . .
. . . .
Image of worksheet

I think your AVERAGE version is on the right lines but when you calculate the PERCENTILE you also need the column A condition included within that calculation, e.g. this array formula
=AVERAGE(IF(A1:A14=C1,IF(B1:B14>PERCENTILE(IF(A1:A14=C1,B1:B14),0.7),B1:B14)))
confirmed with CTRL+SHIFT+ENTER
You can get the same result with a regular (non array) formula using AVERAGEIFS if you use AGGREGATE function to calculate the PERCENTILE, i.e. like this
=AVERAGEIFS(B1:B14,A1:A14,C1,B1:B14,">"&AGGREGATE(16,6,B1:B14/(A1:A14=C1),0.7))
I expect the two formulas to give the same results

Related

Excel Swap Column of number

I am creating an excel file to swap excel columns which contains the number corresponding to the ASCII character such as low letter, upper letter, number, special characters.
Here is the original table and the corresponding letter to the number
A B C D E F G H
1 2 3 4 5 6 7 8
I want to swap each of the cell to the end. Meaning I need to swap 1 to 8. 2 to 7. 3 to 6.
A B C D E F G H
8 7 6 5 4 3 2 1
I would want to use the excel function to do this. Is there a way to achieve this? I have 156 columns.
what about this method.
A 1
B 2
C 3
D 4
E 5
F 6
G 7
H 8
If your data is always ordered then sorting would work.
But let's assume your data is not always alphabetically or numerically sortable, you can use a formula to reverse an unsorted list:
-
A
B
C
D
E
F
G
H
1
A
B
C
D
E
F
G
H
2
1
2
3
4
5
6
7
8
3
8
7
6
5
4
3
2
1
Rows 1 and 2 are your original data. Row 3 is the reversing formula.
Add the formula below into cell A3
=INDEX($A$2:$G$2, COLUMNS(A2:$G$2))
Note (important) that the first Range of the INDEX is absolute $A$2:$G$2, but the only the last value of the columns range is absolute A2:$G$2 (no dollars)
Either drag cell A3 across to H3 or copy cell A3 and paste over B3:H3
This has an advantage over plain sorting in that it can reverse unsorted lists for you.

VLookup with Multiple Ranges

I'm trying to make a formula that would do the following: There are say 10 categories 1-10, given a number x and y, the line is in category 3 if and only if x is between 1 and 2 and y is between 5-7 for example. I don't know how to use VLookup given the multiple conditions and the two ranges that are completely different and not in a sequential order.
I tried using index match:
=INDEX(B5:B15,MATCH(1,IF(AND(K5>=C5:C15,K5<=D5:D15),1,0)*IF(AND(L5>=E5:E15,L5<=F5:F15),1,0),0))
but this returns an error where column B are the categories, K5 and L5 are x and y respectively and column C is the lower bounds for x per category with D as upper bounds and same for E and F for y.
Here's a mock representation of the data and rules:
Data
x y category
1.2 12 1
1.5 5 2
0.98 23 3
.
.
.
Rules
Category X-LB X-UB Y-LB Y-UB
1 1 2 9 15
2 1.5 1.7 1 9
3 0.8 1 20 23
.
.
.
LB is lower bound and UB is upper bound. For example given x and y above using the rules table we find the expected return column.
Thank you,
If you have only category which will fit the bill in each case, one way is to use SUMPRODUCT.
Formula in C2 and down is
=SUMPRODUCT(($B$10:$B$12<=A2)*($C$10:$C$12>=A2)*($D$10:$D$12<=B2)*($E$10:$E$12>=B2)*($A$10:$A$12))
In M5, copied down :
=INDEX($B$5:$B$15,MATCH(1,INDEX(($K5>=$C$5:$C$15)*($K5<=$D$5:$D$15)*($L5>=$E$5:$E$15)*($L5<=$F$5:$F$15),0),0))
Or,
Another shorter option.
Using SUMIFS function, formula in M5 copied down :
=SUMIFS($B:$B,$C:$C,"<="&$K5,$D:$D,">="&$K5,$E:$E,"<="&$L5,$F:$F,">="&$L5)

Excel: how to find the intersection (equilibrium point) between supply and demand?

I have a column indicating the demand over time, a column indicating the supply cumulative volume available (always the same regardless of the time) and the price corresponding to each supply volume.
It looks like this:
demand supply price
30 5 3
12 10 7
13 15 11
23 20 13
3 25 15
22 30 21
. . .
. . .
. . .
I need in a new column, for each demand volume, the clearing price of the corresponding supply volume capable to satisfy the demand (for example in the 3rd row, the cleared price=15 is the price associated to a supply=25 which is the minimum greater volume capable to cover the demand=23). This would result as:
demand supply price cleared price
18 5 3 13
12 10 7 11
13 15 11 11
23 20 13 15
3 25 15 3
20 30 21 13
. . . .
. . . .
. . . .
I am trying with functions as: LOOKUP, INDEX, MATCH...for instance tryiong to find the smallest positive difference between supply and demand, but at the moment I'm struggling in finding a solution. Anyone has ideas? Thank you in advance
if demand in column A, price in column C and cleared price (to find out) in column D you could use :
=INDEX($C$2:$C$128, IF(ISERROR(MATCH(A2,$B$2:$B$128,1)),1, IF(INDEX($B$2:$B$128,MATCH(A2,$B$2:$B$128,1))=A2,MATCH(A2,$B$2:$B$128,1),MATCH(A2,$B$2:$B$128,1)+1)))
I think I have managed now to do something close to what I want doing:
=INDEX($F$2:$F$128;IFNA(MATCH(D2;$G$2:$G$128;1)+1;1))
MATCH is used to find out which row of the supply is greater than the demand (I add 1 because it didn't work with "greater than", so I use "smaller than" and I select the next row)
IFNA corrects to 1 those values that because of the function "smaller than" were N/A, as the demand was smaller than any possible supply.
INDEX associates the price to the MATCH result.
I still have to go through the list, but it seems to be all right.

Split Excel worksheet using Python

I have excel files (hundreds of them) that look like this (sensor output):
Column1 Column2 Column3
Serial Number:
10004
Ref. Temp:
25C
Ref. Pressure:
1KPa
Time Temp. Pres.
1 21 1
2 22 1.1
3 23 1.2
. . .
. . .
. . .
I want to split this into two parts, the information section (top part) and data section (the rest), something like this:
Information section
Column1 Column2 Column3
Serial Number:
10004
Ref. Temp:
25C
Ref. Pressure:
1KPa
Data section:
Column1 Column2 Column3
Time Temp. Pres.
1 21 1
2 22 1.1
3 23 1.2
. . .
. . .
. . .
if it converts to data frame I don't want the first row and column become header and index of the data frame. I am using python 2.7 and numpy.
Make two copies of the worksheet.
In copy A, start a loop, going on the first column looking for the word Time. Once it finds it, let it delete anything before it.
Remember the row in a variable.
In copy B, delete anything after the remembered row to row number 2^20.

Repeat a specific text x number of times in one column taken from another column of Random text strings

I have some Text Values in sheet 1, A4:A400 and for each value in Column A there are 10 codes in non-consecutive cells in Row 1.
I want a formula in "sheet 2" to put each text value 10 times in column A with the code in column B and then repeat the same with a new text value with the same 10 cods.
LIke:
1
2 . . . . . . 5310 5200 3310 3456 3600
3 PK64
4 PK967
5 PK106
6 PK369
7 PK250
8 PK222
It should look like this
1 A B
2 PK64 5310
3 PK64 5200
4 PK64 3310
5 PK64 3456
6 PK64 3600
7 PK967 5310
8 PK967 5200
9 PK967 3310
10 PK967 3456
11 PK967 3600
I used =INT((ROW()-1)/3)+1 but it increments and repeats the number only.
I also used =REPT(B1,ROW()-1/5) but it repeats the text in the same cell with increased number of times as the row number increments.
Assuming that all codes are numeric and with no repetitions you can do this:
With PK's starting at A4
write in J4 the first PK (in your example "PK64")
In K4 write the formula:
=MIN(2:2)
Now write in J5 and K5 the formulas:
=IF(K5=MIN($2:$2);INDEX(A:A;MATCH(J4;A:A;0)+1);J4)
=IF(K4=MAX($2:$2);MIN($2:$2);SMALL($2:$2;RANK(K4;$2:$2;1)+1))
Copy, at the same time, both formulas down and you get the pretended result
Depending on your regional settings you may need to replace ";" by ","

Resources