How to generate an array of sorted results by omitting each element of operation (in this case AVERAGE)?

Rnkhch

Well-known Member
Joined
Apr 28, 2018
Messages
528
Office Version
  1. 365
Platform
  1. Windows
Hello,

Part1:
I have 3 sets of 4 numbers as follows, such that I take the first number of each set and generate an average, then another average from the second numbers, an average from the third numbers, and finally an average from the fourth numbers. Then I take those "four" averages, and calculate a final average from those, and this is the final calculation that I need, which is so far, of course, very easy to do.

initial:
Q3=AVERAGE(B3,G3,L3)
R3=AVERAGE(C3,H3,M3)
S3=AVERAGE(D3,I3,N3)
T3=AVERAGE(E3,J3,O3)

Final:
V3=AVERAGE(Q3,R3,S3,T3)

A/1​
B​
C​
D​
E​
F​
G​
H​
I​
J​
K​
L​
M​
N​
O​
P​
2​
1​
2​
3​
4​
1​
2​
3​
4​
1​
2​
3​
4​
3​
43​
65​
98​
70​
32​
56​
71​
52​
78​
92​
23​
18​
4​


Part2:
Now, I want to exclude each of these 12 numbers, and see what the final result is, and this is the hard part (which I think would need some kind of array formula). I want to write a formula that I can spread in 12 cells, i.e. X3:AI3, such that each one of these 12 cells calculates the entire operation by excluding each number (in order). For example in cell X3, number B3 (i.e. 43) which is the first of the 12 numbers will be excluded from the operation, and the operation will become like this:

Final:
X3=AVERAGE(AVERAGE(--,G3,L3),AVERAGE(C3,H3,M3),AVERAGE(D3,I3,N3),AVERAGE(E3,J3,O3))
Y3=AVERAGE(AVERAGE(B3,--,L3),AVERAGE(C3,H3,M3),AVERAGE(D3,I3,N3),AVERAGE(E3,J3,O3))
.
.
.
AI3=AVERAGE(AVERAGE(B3,G3,L3),AVERAGE(C3,H3,M3),AVERAGE(D3,I3,N3),AVERAGE(E3,J3,--))

Part3:
Finally, in another set of 12 cells, i.e. AK3:AV3, I want the results of the previous step to be displayed in the order of larger to smaller starting at AK3. I want this step to be also done with a formula that I can spread from AK3 to AV3. (I hope to be able to handle this part with an INDEX formula which includes AGGREGATE which I saw at this link ():
)

I would highly appreciate any suggestions! Of course, if it is possible to combine parts 2 and 3 in a single range of 12 cells at X3:AI3, that's gonna be even enormously more fabulous ?

Thanks much!!!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
ABCDEFGHIJKLMNOPQRSTUVW
1
2436598703256715278922318
3
4CHECKAv1Av2Av3Av4
559.1759.1755.0071.0064.0046.676598703256715278922318
658.9258.9251.0074.0064.0046.674398703256715278922318
753.9253.9251.0071.0047.0046.674365703256715278922318
855.2555.2551.0071.0064.0035.004365983256715278922318
960.5460.5460.5071.0064.0046.674365987056715278922318
1060.0460.0451.0078.5064.0046.674365987032715278922318
1157.2957.2951.0071.0060.5046.674365987032565278922318
1257.5057.5051.0071.0064.0044.004365987032567178922318
1354.7954.7937.5071.0064.0046.674365987032567152922318
1455.5455.5451.0060.5064.0046.674365987032567152782318
1563.2963.2951.0071.0084.5046.674365987032567152789218
1661.7561.7551.0071.0064.0061.004365987032567152789223
17
Sheet7
Cell Formulas
RangeFormula
B5:B16B5=LET(nos,FILTER(J2:W2,J2:W2<>""),wts,{0,1,1,1,1.5,1,1,1,1.5,1,1,1},MMULT(wts*INDEX(nos,MOD(SEQUENCE(,12)+SEQUENCE(12)-2,12)+1),SEQUENCE(12,,,0)))/12
D5:D16D5=AVERAGE(E5:H5)
E5:H16E5=AVERAGE(J5,O5,T5)
Dynamic array formulas.


If you like, you can wrap the formula in a SORT to do everything in one formula.
 
Upvote 0
Wow, thank you so much! This is a marvelous formula, and I learned a lot of new Excel things from it!! There are a couple of things:

1) I noticed that either with enter or control/shift/enter, the formula automatically spreads itself down vertically. By any chance is there a trick to make it spread across horizontally?

2) As you suggested, could you please show me the SORT as well (preferably horizontally ?)

(The reason for horizontal is that I have many rows of these 12 numbers under each other, so the only room available is to do the spreading/sorting horizontally across)
 
Upvote 0
This is formula based Excel 2016 or earlier.
Not sure with later version can eliminate Part 2 or not, but VBA can. Post back if you refer to VBA.
First row is your initial data
from second row, I use random data.
Cell Formulas
RangeFormula
Q3:T9Q3=AVERAGE(B3,G3,L3)
X3:AI9X3=(SUM($Q3:$T3)-INDEX($Q3:$T3,MOD(COLUMNS($A:A)-1,4)+1)+(INDEX($Q3:$T3,MOD(COLUMNS($A:A)-1,4)+1)*3-INDEX($B3:$O3,COLUMNS($A:A)+INT((COLUMNS($A:A)-1)/4)))/2)/4
AK3:AV9AK3=LARGE($X3:$AI3,COLUMNS($A:A))
L4:O9,G4:J9,B4:E9B4=RANDBETWEEN(20,80)
V3:V9V3=AVERAGE(Q3:T3)
 
Upvote 0
Awesome, thanks much! I'll apply this to my work data tomorrow at the office and will let you know if I have any other questions.
 
Upvote 0
As you suggested, could you please show me the SORT as well (preferably horizontally ?)

Using the layout in Post #2, try:

=LET(nos,FILTER(J2:W2,J2:W2<>""),wts,{0;1;1;1;1.5;1;1;1;1.5;1;1;1},SORT(MMULT(SEQUENCE(,12,,0),wts*INDEX(nos,MOD(SEQUENCE(12)+SEQUENCE(,12)-2,12)+1))/12,,-1,1))
 
Upvote 0
BTW, you could simplify Part 1. As your groups are the same size, the result you're getting is just the simple average of all 12 numbers.
 
Upvote 0
Wow, thanks, your post #6 formula is incredible! It spreads across ?

You brought up a good point regarding same group size. I may have some data in which the groups might be a bit more irregular. I'll play around with them too tomorrow and see how thy come around, and I might need your help with them :)
 
Upvote 0
It worked really well on the data that was organized nicely in three sets ??? and the spilling was fabulous! Now I just need to take care of some data that are not as nicely organized:

A. Most of the data actually contains 12 sets of 4 numbers (so between 3 and 12 sets of 4 numbers)
B. In some sets, one or more of the four numbers are missing. (Sometimes all 4 numbers in a set are missing). I noticed that the missing cells cause the REF error in your function (from post #6)?
(C. In some data, the in between blank columns are not there, and all sets of (available) four numbers are stacked together continuously. I noticed that your formula already automatically takes care of this by the FILTER you have implemented, which is fantastic.)

So I just need to take care of A and B, and I would appreciate your help. If you could modify your post #6 such that it can take care of 12 sets of 4 numbers and any missing numbers in there (to avoid #REF), it will be fabulous! Thank you so much!! :)
 
Upvote 0
I tried to tackle this myself, but it is it a bit lengthy ?

1) First, I generated 12 sets of 4 numbers some of which are missing:

TestSort.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAX
1
2num. sets323352737141671319476178835713748331171247136466222441314
3
Sheet1


2) Then I extracted all the first, second, third, and fourth numbers making sure I get 'blank' cells for missing values:

TestSort.xlsx
ABCDEFGHIJKLMN
3
41sts3274167477113622
52nds233136148714644
63rds 7119788712413
74ths3535333716214
8
Sheet1
Cell Formulas
RangeFormula
B4:M4B4=IF((((INDEX(B2:AW2,1,SEQUENCE(,12,1,4)))/1)=0),(""),((INDEX(B2:AW2,1,SEQUENCE(,12,1,4)))/1))
B5:M5B5=IF((((INDEX(B2:AW2,1,SEQUENCE(,12,2,4)))/1)=0),(""),((INDEX(B2:AW2,1,SEQUENCE(,12,2,4)))/1))
B6:M6B6=IF((((INDEX(B2:AW2,1,SEQUENCE(,12,3,4)))/1)=0),(""),((INDEX(B2:AW2,1,SEQUENCE(,12,3,4)))/1))
B7:M7B7=IF((((INDEX(B2:AW2,1,SEQUENCE(,12,4,4)))/1)=0),(""),((INDEX(B2:AW2,1,SEQUENCE(,12,4,4)))/1))
Dynamic array formulas.


3) Then, I wrote an array of 0s and 1s for excluding each number:

TestSort.xlsx
ABCDEFGHIJKLMN
8
9excluder011111111111
10101111111111
11110111111111
12111011111111
13111101111111
14111110111111
15111111011111
16111111101111
17111111110111
18111111111011
19111111111101
20111111111110
21
Sheet1


4) Then, I multiplied each of the sets of numbers from step 2 by the exclusion array again making sure to get blank cells for 0s and errors and manually calculated the average of each row in the rightmost column:

TestSort.xlsx
ABCDEFGHIJKLMNO
21
221sts 27416747  711 362232.25
233 416747  711 362229.25
24327 6747  711 362227.5
2532741 47  711 362224.25
263274167   711 362226.75
27327416747  711 362229
28327416747  711 362229
29327416747   11 362231.75
30327416747  7  362231.25
31327416747  711 362229
32327416747  711  2228.125
33327416747  711 36 29.875
342nds 3 1361  4871 464440.85714
3523  1361  4871 464443.71429
36233 1361  4871 464438.625
37233  61  4871 464442.28571
38233 13   4871 464435.42857
39233 1361  4871 464438.625
40233 1361  4871 464438.625
41233 1361   71 464437.28571
42233 1361  48  464434
43233 1361  4871 464438.625
44233 1361  4871  4437.57143
45233 1361  4871 46 37.85714
463rds 71 1978871 24  1340.57143
47   1978871 24  1335.5
48 71 1978871 24  1340.57143
49 71  78871 24  1344.16667
50 71 19 871 24  1334.33333
51 71 1978 71 24  1346
52 71 19788  24  1335.5
53 71 1978871 24  1340.57143
54 71 1978871    1343.33333
55 71 1978871 24  1340.57143
56 71 1978871 24  1340.57143
57 71 1978871 24   45.16667
584ths     3533371 621436.33333
5935    3533371 621436.14286
6035    3533371 621436.14286
6135    3533371 621436.14286
6235    3533371 621436.14286
6335     33371 621436.33333
6435    35 3371 621441.66667
6535    353 71 621436.66667
6635    35333  621430.33333
6735    3533371 621436.14286
6835    3533371  1431.83333
6935    3533371 62 39.83333
70
Sheet1
Cell Formulas
RangeFormula
B22:M33B22=IFERROR((IF(((B$4*B9)=0),(""),(B$4*B9))),(""))
N22:N69N22=AVERAGE(B22:M22)
B34:M45B34=IFERROR((IF(((B$5*B9)=0),(""),(B$5*B9))),(""))
B46:M57B46=IFERROR((IF(((B$6*B9)=0),(""),(B$6*B9))),(""))
B58:M69B58=IFERROR((IF(((B$7*B9)=0),(""),(B$7*B9))),(""))


5) Finally I transposed the averages column and sorted it in one step:

TestSort.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAX
70
71sort. horiz. avg.4645.1666744.1666743.7142943.3333342.2857141.6666740.8571440.5714340.5714340.5714340.5714340.5714339.8333338.62538.62538.62538.62537.8571437.5714337.2857136.6666736.3333336.3333336.1428636.1428636.1428636.1428636.1428635.535.535.4285734.333333432.2531.8333331.7531.2530.3333329.87529.2529292928.12527.526.7524.25
72
Sheet1
Cell Formulas
RangeFormula
B71:AW71B71=TRANSPOSE(SORT(N22:N69,,-1,))
Dynamic array formulas.


My experience with arrays and their operations is not that high, so I'm hoping someone can condense all these operations in one or as few formulas as possible. Otherwise it will be cumbrome to write this for each of my many rows of numbers.

Thanks much for any input!
 
Upvote 0

Forum statistics

Threads
1,215,491
Messages
6,125,104
Members
449,205
Latest member
ralemanygarcia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top