Calculate maximum of a subrange in a table based on multiple conditions - excel-formula

I want to calculate the maximum amount of rain fallen on a day.
Column B contains the date as YYYYMMDD (value, not text)
Column C contains the number of the hour of measurement (ranging from 1 until 24)
Column N contains the amount of rain fallen in [mm] : 0 if no rain; positive integer if rain has fallen
What formula calculates the maximum amount of rain fallen on a day?
So:
for january 4th I want to sum N2:N25
for january 5th I want to sum N26:N49
etc.
and return the maximum of those calculated values
REMARK:
a value of -1 in column N indicates 'not measured' and shoudl be treated as 'no rain' (= 0)

I found one, my testing area only extended 1,000 rows, but it should scale nicely.
=MAX(SUMIFS(N2:N1000,B2:B1000,B2:B1000))
Remember to enter as an array formula by confirming with CNTRL + SHIFT + ENTER

Here's an ugly solution that takes 3 cells, but is much faster than my first formula:
=MAX(SUMIF(N2:N25,">"&0),SUMIF(N26:N49,">"&0),SUMIF(N50:N73,">"&0),SUMIF(N74:N97,">"&0),SUMIF(N98:N121,">"&0),SUMIF(N122:N145,">"&0),SUMIF(N146:N169,">"&0),SUMIF(N170:N193,">"&0),SUMIF(N194:N217,">"&0),SUMIF(N218:N241,">"&0),SUMIF(N242:N265,">"&0),SUMIF(N266:N289,">"&0),SUMIF(N290:N313,">"&0),SUMIF(N314:N337,">"&0),SUMIF(N338:N361,">"&0),SUMIF(N362:N385,">"&0),SUMIF(N386:N409,">"&0),SUMIF(N410:N433,">"&0),SUMIF(N434:N457,">"&0),SUMIF(N458:N481,">"&0),SUMIF(N482:N505,">"&0),SUMIF(N506:N529,">"&0),SUMIF(N530:N553,">"&0),SUMIF(N554:N577,">"&0),SUMIF(N578:N601,">"&0),SUMIF(N602:N625,">"&0),SUMIF(N626:N649,">"&0),SUMIF(N650:N673,">"&0),SUMIF(N674:N697,">"&0),SUMIF(N698:N721,">"&0),SUMIF(N722:N745,">"&0),SUMIF(N746:N769,">"&0),SUMIF(N770:N793,">"&0),SUMIF(N794:N817,">"&0),SUMIF(N818:N841,">"&0),SUMIF(N842:N865,">"&0),SUMIF(N866:N889,">"&0),SUMIF(N890:N913,">"&0),SUMIF(N914:N937,">"&0),SUMIF(N938:N961,">"&0),SUMIF(N962:N985,">"&0),SUMIF(N986:N1009,">"&0),SUMIF(N1010:N1033,">"&0),SUMIF(N1034:N1057,">"&0),SUMIF(N1058:N1081,">"&0),SUMIF(N1082:N1105,">"&0),SUMIF(N1106:N1129,">"&0),SUMIF(N1130:N1153,">"&0),SUMIF(N1154:N1177,">"&0),SUMIF(N1178:N1201,">"&0),SUMIF(N1202:N1225,">"&0),SUMIF(N1226:N1249,">"&0),SUMIF(N1250:N1273,">"&0),SUMIF(N1274:N1297,">"&0),SUMIF(N1298:N1321,">"&0),SUMIF(N1322:N1345,">"&0),SUMIF(N1346:N1369,">"&0),SUMIF(N1370:N1393,">"&0),SUMIF(N1394:N1417,">"&0),SUMIF(N1418:N1441,">"&0),SUMIF(N1442:N1465,">"&0),SUMIF(N1466:N1489,">"&0),SUMIF(N1490:N1513,">"&0),SUMIF(N1514:N1537,">"&0),SUMIF(N1538:N1561,">"&0),SUMIF(N1562:N1585,">"&0),SUMIF(N1586:N1609,">"&0),SUMIF(N1610:N1633,">"&0),SUMIF(N1634:N1657,">"&0),SUMIF(N1658:N1681,">"&0),SUMIF(N1682:N1705,">"&0),SUMIF(N1706:N1729,">"&0),SUMIF(N1730:N1753,">"&0),SUMIF(N1754:N1777,">"&0),SUMIF(N1778:N1801,">"&0),SUMIF(N1802:N1825,">"&0),SUMIF(N1826:N1849,">"&0),SUMIF(N1850:N1873,">"&0),SUMIF(N1874:N1897,">"&0),SUMIF(N1898:N1921,">"&0),SUMIF(N1922:N1945,">"&0),SUMIF(N1946:N1969,">"&0),SUMIF(N1970:N1993,">"&0),SUMIF(N1994:N2017,">"&0),SUMIF(N2018:N2041,">"&0),SUMIF(N2042:N2065,">"&0),SUMIF(N2066:N2089,">"&0),SUMIF(N2090:N2113,">"&0),SUMIF(N2114:N2137,">"&0),SUMIF(N2138:N2161,">"&0),SUMIF(N2162:N2185,">"&0),SUMIF(N2186:N2209,">"&0),SUMIF(N2210:N2233,">"&0),SUMIF(N2234:N2257,">"&0),SUMIF(N2258:N2281,">"&0),SUMIF(N2282:N2305,">"&0),SUMIF(N2306:N2329,">"&0),SUMIF(N2330:N2353,">"&0),SUMIF(N2354:N2377,">"&0),SUMIF(N2378:N2401,">"&0),SUMIF(N2402:N2425,">"&0),SUMIF(N2426:N2449,">"&0),SUMIF(N2450:N2473,">"&0),SUMIF(N2474:N2497,">"&0),SUMIF(N2498:N2521,">"&0),SUMIF(N2522:N2545,">"&0),SUMIF(N2546:N2569,">"&0),SUMIF(N2570:N2593,">"&0),SUMIF(N2594:N2617,">"&0),SUMIF(N2618:N2641,">"&0),SUMIF(N2642:N2665,">"&0),SUMIF(N2666:N2689,">"&0),SUMIF(N2690:N2713,">"&0),SUMIF(N2714:N2737,">"&0),SUMIF(N2738:N2761,">"&0),SUMIF(N2762:N2785,">"&0),SUMIF(N2786:N2809,">"&0),SUMIF(N2810:N2833,">"&0),SUMIF(N2834:N2857,">"&0),SUMIF(N2858:N2881,">"&0),SUMIF(N2882:N2905,">"&0),SUMIF(N2906:N2929,">"&0),SUMIF(N2930:N2953,">"&0),SUMIF(N2954:N2977,">"&0),SUMIF(N2978:N3001,">"&0),SUMIF(N3002:N3025,">"&0),SUMIF(N3026:N3049,">"&0),SUMIF(N3050:N3073,">"&0),SUMIF(N3074:N3097,">"&0),SUMIF(N3098:N3121,">"&0),SUMIF(N3122:N3145,">"&0),SUMIF(N3146:N3169,">"&0),SUMIF(N3170:N3193,">"&0),SUMIF(N3194:N3217,">"&0),SUMIF(N3218:N3241,">"&0),SUMIF(N3242:N3265,">"&0),SUMIF(N3266:N3289,">"&0),SUMIF(N3290:N3313,">"&0),SUMIF(N3314:N3337,">"&0),SUMIF(N3338:N3361,">"&0),SUMIF(N3362:N3385,">"&0),SUMIF(N3386:N3409,">"&0),SUMIF(N3410:N3433,">"&0),SUMIF(N3434:N3457,">"&0),SUMIF(N3458:N3481,">"&0),SUMIF(N3482:N3505,">"&0),SUMIF(N3506:N3529,">"&0),SUMIF(N3530:N3553,">"&0),SUMIF(N3554:N3577,">"&0),SUMIF(N3578:N3601,">"&0),SUMIF(N3602:N3625,">"&0),SUMIF(N3626:N3649,">"&0),SUMIF(N3650:N3673,">"&0),SUMIF(N3674:N3697,">"&0),SUMIF(N3698:N3721,">"&0),SUMIF(N3722:N3745,">"&0),SUMIF(N3746:N3769,">"&0),SUMIF(N3770:N3793,">"&0),SUMIF(N3794:N3817,">"&0),SUMIF(N3818:N3841,">"&0),SUMIF(N3842:N3865,">"&0),SUMIF(N3866:N3889,">"&0),SUMIF(N3890:N3913,">"&0),SUMIF(N3914:N3937,">"&0),SUMIF(N3938:N3961,">"&0),SUMIF(N3962:N3985,">"&0),SUMIF(N3986:N4009,">"&0),SUMIF(N4010:N4033,">"&0),SUMIF(N4034:N4057,">"&0),SUMIF(N4058:N4081,">"&0),SUMIF(N4082:N4105,">"&0),SUMIF(N4106:N4129,">"&0),SUMIF(N4130:N4153,">"&0),SUMIF(N4154:N4177,">"&0),SUMIF(N4178:N4201,">"&0),SUMIF(N4202:N4225,">"&0),SUMIF(N4226:N4249,">"&0),SUMIF(N4250:N4273,">"&0),SUMIF(N4274:N4297,">"&0),SUMIF(N4298:N4321,">"&0),SUMIF(N4322:N4345,">"&0),SUMIF(N4346:N4369,">"&0),SUMIF(N4370:N4393,">"&0),SUMIF(N4394:N4417,">"&0),SUMIF(N4418:N4441,">"&0),SUMIF(N4442:N4465,">"&0),SUMIF(N4466:N4489,">"&0),SUMIF(N4490:N4513,">"&0),SUMIF(N4514:N4537,">"&0),SUMIF(N4538:N4561,">"&0),SUMIF(N4562:N4585,">"&0),SUMIF(N4586:N4609,">"&0),SUMIF(N4610:N4633,">"&0),SUMIF(N4634:N4657,">"&0),SUMIF(N4658:N4681,">"&0),SUMIF(N4682:N4705,">"&0),SUMIF(N4706:N4729,">"&0),SUMIF(N4730:N4753,">"&0),SUMIF(N4754:N4777,">"&0),SUMIF(N4778:N4801,">"&0),SUMIF(N4802:N4825,">"&0),SUMIF(N4826:N4849,">"&0),SUMIF(N4850:N4873,">"&0),SUMIF(N4874:N4897,">"&0),SUMIF(N4898:N4921,">"&0),SUMIF(N4922:N4945,">"&0),SUMIF(N4946:N4969,">"&0),SUMIF(N4970:N4993,">"&0),SUMIF(N4994:N5017,">"&0),SUMIF(N5018:N5041,">"&0),SUMIF(N5042:N5065,">"&0),SUMIF(N5066:N5089,">"&0),SUMIF(N5090:N5113,">"&0),SUMIF(N5114:N5137,">"&0),SUMIF(N5138:N5161,">"&0),SUMIF(N5162:N5185,">"&0),SUMIF(N5186:N5209,">"&0),SUMIF(N5210:N5233,">"&0),SUMIF(N5234:N5257,">"&0))
And in a separate cell
=MAX(SUMIF(N5258:N5281,">"&0),SUMIF(N5282:N5305,">"&0),SUMIF(N5306:N5329,">"&0),SUMIF(N5330:N5353,">"&0),SUMIF(N5354:N5377,">"&0),SUMIF(N5378:N5401,">"&0),SUMIF(N5402:N5425,">"&0),SUMIF(N5426:N5449,">"&0),SUMIF(N5450:N5473,">"&0),SUMIF(N5474:N5497,">"&0),SUMIF(N5498:N5521,">"&0),SUMIF(N5522:N5545,">"&0),SUMIF(N5546:N5569,">"&0),SUMIF(N5570:N5593,">"&0),SUMIF(N5594:N5617,">"&0),SUMIF(N5618:N5641,">"&0),SUMIF(N5642:N5665,">"&0),SUMIF(N5666:N5689,">"&0),SUMIF(N5690:N5713,">"&0),SUMIF(N5714:N5737,">"&0),SUMIF(N5738:N5761,">"&0),SUMIF(N5762:N5785,">"&0),SUMIF(N5786:N5809,">"&0),SUMIF(N5810:N5833,">"&0),SUMIF(N5834:N5857,">"&0),SUMIF(N5858:N5881,">"&0),SUMIF(N5882:N5905,">"&0),SUMIF(N5906:N5929,">"&0),SUMIF(N5930:N5953,">"&0),SUMIF(N5954:N5977,">"&0),SUMIF(N5978:N6001,">"&0),SUMIF(N6002:N6025,">"&0),SUMIF(N6026:N6049,">"&0),SUMIF(N6050:N6073,">"&0),SUMIF(N6074:N6097,">"&0),SUMIF(N6098:N6121,">"&0),SUMIF(N6122:N6145,">"&0),SUMIF(N6146:N6169,">"&0),SUMIF(N6170:N6193,">"&0),SUMIF(N6194:N6217,">"&0),SUMIF(N6218:N6241,">"&0),SUMIF(N6242:N6265,">"&0),SUMIF(N6266:N6289,">"&0),SUMIF(N6290:N6313,">"&0),SUMIF(N6314:N6337,">"&0),SUMIF(N6338:N6361,">"&0),SUMIF(N6362:N6385,">"&0),SUMIF(N6386:N6409,">"&0),SUMIF(N6410:N6433,">"&0),SUMIF(N6434:N6457,">"&0),SUMIF(N6458:N6481,">"&0),SUMIF(N6482:N6505,">"&0),SUMIF(N6506:N6529,">"&0),SUMIF(N6530:N6553,">"&0),SUMIF(N6554:N6577,">"&0),SUMIF(N6578:N6601,">"&0),SUMIF(N6602:N6625,">"&0),SUMIF(N6626:N6649,">"&0),SUMIF(N6650:N6673,">"&0),SUMIF(N6674:N6697,">"&0),SUMIF(N6698:N6721,">"&0),SUMIF(N6722:N6745,">"&0),SUMIF(N6746:N6769,">"&0),SUMIF(N6770:N6793,">"&0),SUMIF(N6794:N6817,">"&0),SUMIF(N6818:N6841,">"&0),SUMIF(N6842:N6865,">"&0),SUMIF(N6866:N6889,">"&0),SUMIF(N6890:N6913,">"&0),SUMIF(N6914:N6937,">"&0),SUMIF(N6938:N6961,">"&0),SUMIF(N6962:N6985,">"&0),SUMIF(N6986:N7009,">"&0),SUMIF(N7010:N7033,">"&0),SUMIF(N7034:N7057,">"&0),SUMIF(N7058:N7081,">"&0),SUMIF(N7082:N7105,">"&0),SUMIF(N7106:N7129,">"&0),SUMIF(N7130:N7153,">"&0),SUMIF(N7154:N7177,">"&0),SUMIF(N7178:N7201,">"&0),SUMIF(N7202:N7225,">"&0),SUMIF(N7226:N7249,">"&0),SUMIF(N7250:N7273,">"&0),SUMIF(N7274:N7297,">"&0),SUMIF(N7298:N7321,">"&0),SUMIF(N7322:N7345,">"&0),SUMIF(N7346:N7369,">"&0),SUMIF(N7370:N7393,">"&0),SUMIF(N7394:N7417,">"&0),SUMIF(N7418:N7441,">"&0),SUMIF(N7442:N7465,">"&0),SUMIF(N7466:N7489,">"&0),SUMIF(N7490:N7513,">"&0),SUMIF(N7514:N7537,">"&0),SUMIF(N7538:N7561,">"&0),SUMIF(N7562:N7585,">"&0),SUMIF(N7586:N7609,">"&0),SUMIF(N7610:N7633,">"&0),SUMIF(N7634:N7657,">"&0),SUMIF(N7658:N7681,">"&0),SUMIF(N7682:N7705,">"&0),SUMIF(N7706:N7729,">"&0),SUMIF(N7730:N7753,">"&0),SUMIF(N7754:N7777,">"&0),SUMIF(N7778:N7801,">"&0),SUMIF(N7802:N7825,">"&0),SUMIF(N7826:N7849,">"&0),SUMIF(N7850:N7873,">"&0),SUMIF(N7874:N7897,">"&0),SUMIF(N7898:N7921,">"&0),SUMIF(N7922:N7945,">"&0),SUMIF(N7946:N7969,">"&0),SUMIF(N7970:N7993,">"&0),SUMIF(N7994:N8017,">"&0),SUMIF(N8018:N8041,">"&0),SUMIF(N8042:N8065,">"&0),SUMIF(N8066:N8089,">"&0),SUMIF(N8090:N8113,">"&0),SUMIF(N8114:N8137,">"&0),SUMIF(N8138:N8161,">"&0),SUMIF(N8162:N8185,">"&0),SUMIF(N8186:N8209,">"&0),SUMIF(N8210:N8233,">"&0),SUMIF(N8234:N8257,">"&0),SUMIF(N8258:N8281,">"&0),SUMIF(N8282:N8305,">"&0),SUMIF(N8306:N8329,">"&0),SUMIF(N8330:N8353,">"&0),SUMIF(N8354:N8377,">"&0),SUMIF(N8378:N8401,">"&0),SUMIF(N8402:N8425,">"&0),SUMIF(N8426:N8449,">"&0),SUMIF(N8450:N8473,">"&0),SUMIF(N8474:N8497,">"&0),SUMIF(N8498:N8521,">"&0),SUMIF(N8522:N8545,">"&0),SUMIF(N8546:N8569,">"&0),SUMIF(N8570:N8593,">"&0),SUMIF(N8594:N8617,">"&0),SUMIF(N8618:N8641,">"&0),SUMIF(N8642:N8665,">"&0),SUMIF(N8666:N8689,">"&0),SUMIF(N8690:N8713,">"&0),SUMIF(N8714:N8737,">"&0),SUMIF(N8738:N8761,">"&0),SUMIF(N8762:N8785,">"&0))
The Max() of these two cells is the total for the year. It's brute force but it solves the speed issue.

Related

How to calculate the daywise average and multiply it to number of rows in that particular day

I have daywise excel data of four months which contains hourly data from 10 AM to 3 PM or sometimes 10 AM to 2 PM....1PM ..... like wise it is varying. The column A contains the date, Column B contains the time and column C and D has the data which i want to average for each day and multiply it to the number of occurrence.
For example If I have a data in column C and D for 17 Oct from 10 AM to 3PM (6 Hours)..Then i want to average these hourly data for a day and multiply it by 6. Since my interval of hourly data is not fixed, that is why i want a code which can average the daywise data and multiply it to the number of occurrence. I want to average the data 1 and data 2 for each day and to multiply its average by the number of occurrence of data in that particular day
I could not exactly get what does "average these hourly data" means since Average basically means summing all data within a day and dividing them to the occurrences. If you again multiply by the number of daily occurrence, you will get the summed value again.
Apart from above unclear point, un-merging all column B cells and filling all your rows with date data will be very helpful in order to form a quick sum or average formula based on the date conditions.
After doing above step, you may enter the below formula to E2 to get the sum of Col:C, for all data dated A2; 17-Oct.
=SUMIFS(C:C,A:A,A2)
And you may get the occurences on the date of your row data, you should use the formula and copy down:
=COUNTIFS(A:A,A2)
In combination of above 2 formulas, you may calculate the averages and/or date occurrences. I may write an exact solution if you may enter your requested values into COL:E and provide a screenshot.

Excel formula (Avg the difference)

I have an excel file with 5 columns Year1%,Year2,Year3%, and I added the the 4th column to get the avg for each day for all 3 years, now I need to find formula to avg the difference between each day in year1 and the Avg, year2 and the Avg, and Year3 and the Avg,this is how my columns look like:
Year1 % _ Year2% _ Year3% _ Avg% _ Avg Diff to %
Day 1
Day 2
...
Assuming your table starts in A1, this formulas goes into the first row of the 5th data column (F2) to give you the average "DIFFERENCE" from the average:
=AVERAGE(E2-B2:D2)
This is an array formula that must be saved by pressing CTRL+SHIFT+ENTER
Note that this will always equal 0 (maybe showing rounding error due to Excel working with floating point precision)
This is due to the definition of the average being the point that is, on average, the same distance from all other points. The positive and negative differences all sum to zero.
If, however, you wanted the average "DISTANCE" from the average (taking only the magnitude of the difference), you would use this formula:
=AVERAGE(ABS(E2-B2:D2))
This is also an array formula.
Statistically, this is not an effective measure of the spread of data. A more effective measure comes from squaring the difference rather than taking the absolute value:
=AVERAGE((E2-B2:D2)^2)
But this is actually just the variance:
=VAR.P(B2:D2)
The square root of which is the Standard Deviation:
=STDEV.P(B2:D2)

Formula for average entries per day

I have a very simple sheet with dates in column A and product names in column B. I just need to know how many sales I make on average per day.
Sales:
01/01/2018 PRODUCT A
01/01/2018 PRODUCT A
01/02/2018 PRODUCT A
01/02/2018 PRODUCT B
Average sales per day: 2
So I don't care what product it is. Just how many sales per day on average.
You can use counta(a2:a5) to get the count of items in a range (adjust your range to suit of course).
You can use the array formula sum(1/countif(a2:a5,a2:a5)) to get the count of unique items in a range. The way to get an array formula is to enter it with CTRLSHIFTENTER rather than just ENTER (it will show up in the formula bar with {braces}).
It should then be a simple matter of dividing the former by the latter to get the average items per unique item (sales per day and, again, this should be an array formula):
=counta(a2:a5)/sum(1/countif(a2:a5,a2:a5))
The only tricky bit there is the countif formula. The expression countif(range,value) will give you a count of all items in the range that match the value.
By making the value the same as the range, it counts (for each item in the range) the number of times an item appears.
So, if your range contains a,a,a,a,b,b,c, you'll get the array (4,4,4,4,2,2,1). Doing the sum of the reciprocals of those values for each cell in the range gives:
(1/4 + 1/4 + 1/4 + 1/4) + (1/2 + 1/2) + (1/1)
which is basically a sneaky way of counting the unique items.
Assuming dates are entered as real Date (and not just text that only looks like dates), use this
=COUNT(A:A)/SUM(--(FREQUENCY(A:A,A:A)>0))
(Frequency formula sourced from here )

How to achieve equal Sum Values horizontally and vertically in Excel

I'm working on this for a week now and still could not get the result I want. What I want is to have an equal result between the horizontal total and vertical total when summing up every columns in excel. Please consider this worksheet.
Every TSalary values in each row is equal to each values in its row in Gross Pay Columns. The same thing also in TDeductions with the columns in Deductions. However, after summing up each row and getting its overall total in AD13 & AE13, I am not getting the same value with the total when summing up every columns in gross pay and deductions.
In column AE you seem to have rounded each value to the nearer cent. In cell N13 you have summed the unrounded values.
The 4 half-cents (in column N) that have thus been rounded to a full cent cause your total to be 2 cents higher than the total of the unrounded amounts.
You need to be consistent when applying roundings if you wish your totals to be consistent.

Median Selling Price Excel Table

I have a spreadsheet with different products, listing units and retail value sold like the example below
Product Units Value
A 10 100
B 15 80
C 30 560
I'd like to compare the Average Selling Price with the Median Selling price, so I am looking for a quick formula to accurately calculate the median.
The median function requires the entire series, so for Product A above I would need 10 instances of 10 etc. How can I calculate the Median quickly considering the condensed form of my data?
Without writing your own VBA function to do this there are a couple of approaches that can be taken.
The first expands the data from its compressed frequency count format to generate the full set of observations. This can be done manually or formulaically. On the assumption the latter is required, it can be achieved using a few columns.
All the blue cells are formulae.
Column Eis simply the cumulative of column B and F is an adjusted version of this. Column H is just the values 1 to 55, the total number of observations given by cell L2. Column I uses the MATCH() with its final argument as 1 to match each observation in H against the adjusted cumulative in F. Column J uses the INDEX() function to generate the value of the observation. (Observations 1-10 have value 100, 11-25 have value 80 and 26-55 have value 560 in this example). The MEDIAN() function is used in cell M2 with column J as its argument.
This approach can be refined to take account of varying numbers of products and data points through the use of the OFFSET function to control the range arguments of the MATCH(), INDEX() and MEDIAN functions. And, of course, adjacent cells in columns I and J could be combined using a single formula - I've shown them separately for ease of explanation.
The second approach involves sorting the data by value (so in this case the data rows would become Product B in row 2, product A in row 3 and product C left as-is in row 4). It is then a case of identifying the middle observation number (if the number of observations is odd) or the middle pair of observation numbers (if the number of observations is even) and then determining the value(s) corresponding to this/these middle observation(s). In this approach the adjusted cumulative in column F is still used but rather than explicitly calculating the values in column I and J for every observation it can now be restricted to just the middle observation(s).
I think there is no way around compromises. Either using big amounts of helper cells or having the table sorted by the values.
Helper cells:
Formula in F4:AS6:
=IF(COLUMN()<COLUMN($F$4)+$B4,$C4,"end")
Formula in D2:
=MEDIAN(F4:AS6)
Sorted:
Formula in F4 downwards:
=SUM($B$3:B3)+1
Formula in D2:
=SUM(LOOKUP(INT(SUM(B4:B6)/2+{0.5,1}),F4:F6,C4:C6))/2

Resources