Average based on criteria

cunningAce

Board Regular
Joined
Dec 21, 2017
Messages
91
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am looking for a way to calculate an average total dependant on the selections made.
So in this example the selections are 'Code' = A1 and 'Weeks Avg' = 4
The answer should be 29.66 as I want the formula to average the last 4 weeks figures for anything with code A1.

CodeWk 1Wk 2Wk 3Wk 4Wk 5Wk 6Wk 7Wk 8
A117131925313743
A128142026323844
B139152127333945
C2410162228344046
A1511172329354147
B2612182430364248
Code A1
Weeks Avg 4
Answer29.66

<colgroup><col><col span="8"></colgroup><tbody>
</tbody>
Thanks in advance

<colgroup><col><col span="8"></colgroup><tbody>
</tbody>
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You already got an answer to get the SUM of your range with criteria in this post here: https://www.mrexcel.com/forum/excel-questions/1046453-sum-if-based-selection.html
The AVERAGE is the SUM divided by the COUNT. So now you just need to count the number of records your just summed.
You can get that easily using the exact same formula given to you in the first thread, just remove the part that SUMs the range (the first argument).

So that would look like this (in that original example):
Code:
=SUMPRODUCT(($B$1:$E$1=$H$1)*($A$2:$A$6=G2))

So, just divide that first formula by this formula to get your average, i.e.
Code:
=SUMPRODUCT(($B$2:$E$6),($B$1:$E$1=$H$1)*($A$2:$A$6=G2))/SUMPRODUCT(($B$1:$E$1=$H$1)*($A$2:$A$6=G2))
 
Upvote 0
Thanks for the reply but that's not what I am trying to achieve,
I want to specify a 'Code' such as A1 and a 'Wks.' Average', such as 4 and the formula return the average. So the example above would look at the figures in Wk5, Wk6, Wk7 & Wk8 and take the average for all code A1's

Does this make sense?
 
Upvote 0
Row\Col
A​
B​
C​
D​
E​
F​
G​
H​
I​
1​
CodeWk 1Wk 2Wk 3Wk 4Wk 5Wk 6Wk 7Wk 8
2​
A1
1
7
13
19
25
31
37
43
3​
A1
2
8
14
20
26
32
38
44
4​
B1
3
9
15
21
27
33
39
45
5​
C2
4
10
16
22
28
34
40
46
6​
A1
5
11
17
23
29
35
41
47
7​
B2
6
12
18
24
30
36
42
48
8​
9​
10​
CodeA1
11​
Weeks Avg
4
12​
Answer
35.66667​

In B12 control+shift+enter, not just enter:
Rich (BB code):
=AVERAGE(IF($A$2:$A$7=B10,OFFSET(INDEX($B$2:$I$2,COLUMNS($B$1:$I$1)-B11+1),0,0,ROWS($B$2:$I$7),4)))
 
Upvote 0
Thanks Aladin,
This currently only works for the Weeks Avg as '4'.
How can I modify to accept any number and calculate the average?
 
Upvote 0
Thanks Aladin,
This currently only works for the Weeks Avg as '4'.
How can I modify to accept any number and calculate the average?

Change the value in B11. Note that I took the value in B11 to mean as the number of columns from left to right.
 
Upvote 0

Forum statistics

Threads
1,215,008
Messages
6,122,672
Members
449,091
Latest member
peppernaut

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