(Edit: Alternate solution, more intuitive, at the end of this answer.)
This should do the trick. Paste the following into Q3:
=IFERROR(SUM(($B$3:$B$11=$B3)*(C$3:C$11+F$3:F$11+I$3:I$11))/SUM(($B$3:$B$11=$B3)*((C$3:C$11<>0)+(F$3:F$11<>0)+(I$3:I$11<>0))),0)
Press Ctrl-Shift-Enter to make this an array formula.
Drag/copy the pasted formula across the two additional Specialty summation columns and the formulas' corresponding Rating data columns will be updated. While all three cells are still selected, drag/copy the three selected cells to the last row of physicians. Excel will update all the Specialty filter row numbers to the corresponding summation row.
How it works:
- First the equality test
=$B3
against the Specialty column array creates a single column boolean array. If the current formula row Specialty matches that of the Specialty column row, the array will contain TRUE
in that row of the created boolean array and FALSE
otherwise.
- A second single column array is created by combining the three single column arrays, one for each month of the current Rating. This is done by adding July+Aug+Sep together row by row to create a single column array of the three month total of each Physician for the given Rating.
- The corresponding row values in these two arrays are then multiplied together.
- Any
FALSE
boolean value is automatically converted by Excel to zero when used in a math expression. This zeros out the Specialties which are not equal to the formula row. The TRUE
boolean values are converted to one which is then multiplied by the rating summation for the corresponding row preserving the value of the 3 month sum for that Speciality in that row. The result is an array having zero in rows not part of the formula row Specialty.
- Then the resultant array is summed up with
SUM
.
- This sum is divided by a similar summation.
- The same boolean filter array is created to filter out Specialties not equivalent to the formulas' row Specialty.
- This time, each element of the second calculated array is the number of months in that particular row which have a non zero value. This is accomplished by performing a "not zero test"
<>0
for each item in each of the three single column arrays of data (one array of data for each month of the given Rating).
- With the resulting three single column boolean arrays, the items of a given row of each array are added together to create the calculated second array. Since these are boolean values returned by the expression to test for a non zero value, again Excel automatically converts the
TRUE
values to one and the FALSE
values to zero when asked to do this arithmetic addition.
- This single column array with values in each row ranging from 0 to 3 is then multiplied, row by row, with the boolean filter array, zeroing any rows in the array which are not the same Specialty as the current formula row (as described earlier).
- Again the resultant array is summed to obtain the divisor used to calculate the average.
- The
IFERROR
replaces division by zero errors with zero.
[Edit] Alternate solution. A more intuitive formula which is not CSE:
=IFERROR((
SUMIFS( C$3:C$12, $B$3:$B$12, $B3) +
SUMIFS( F$3:F$12, $B$3:$B$12, $B3) +
SUMIFS( I$3:I$12, $B$3:$B$12, $B3)
) / (
COUNTIFS( $B$3:$B$12, $B3, C$3:C$12, "<>0", C$3:C$12, "<>") +
COUNTIFS( $B$3:$B$12, $B3, F$3:F$12, "<>0", F$3:F$12, "<>") +
COUNTIFS( $B$3:$B$12, $B3, I$3:I$12, "<>0", I$3:I$12, "<>")), 0)
Actual Calculation of this Formula: For each time F does not equal 0 (even when E of the row does not equal "A"), return to SUM the AVERAGE of F (where E="A", even average in the zeros): 9(18/5) [missing red highlighting in row 25]. SUM will also get the average of G (where E="A") for every row of F=0 if the same row of G<>0 (regardless of Es value): 1(73/5). For each row with E=0 & F=0 & G<>0 then SUM the AVERAGE of G (where E="A") that number of times. All that divided by 3 (even if all else was correct, this would assume the same number of zeros per column). (9*(18/5) + 73/5)/3 = 15.67 – Ted D. – 2019-02-15T22:21:49.497
1Write your concern, why down voted, help me to make my viewpoint clear !! – Rajesh S – 2019-03-01T08:29:14.477
You may have been down voted because of my comment. Your result 15.67 is calculated as outlined in my comment 15.67, which is completely wrong and you never bothered to fix it. The formula you use does not ignore zeros as stipulated in the OP. By dividing by three, your formula is weighting every column equally, so even if the zeros had been ignored, if there were a different number of zeros in one column, this would not be right. The formula you use sums the average from the first column multiple times and never averages the third column. – Ted D. – 2019-03-01T14:53:28.207
1@TedD.,, thanks for the observation,, do you have any suggestion that where my Formula need correction? – Rajesh S – 2019-03-02T06:37:33.770
Rajesh, I will need to edit the answer with the formula analysis you have requested. The comment box is not capable of the markup required to illustrate the issues in the formula. Please accept the edits so that others will be able to better understand the flow of logic in
IF
statements and array formulas. There will be a Formula Analysis at the beginning of this answer if Rajesh sincerely wanted corrective suggestions. – Ted D. – 2019-03-02T18:03:10.4901@TedD., thanks for the time you have invested on the issue, what I do believe that you must add an alternative Formula which exactly meets OP requirements along with the Analysis as a NEW ANSWER,, instead of edit my post,, will help the OP and the community also. – Rajesh S – 2019-03-03T04:59:55.707
Rajesh, Can you not see my answer and its alternate solution? That is why I knew this answer did not meet the OP requirements. The issue was not with the OP requirements, but with the logic in the formula given in your answer. The community would benefit from seeing how the logic plays out in your formula and why it is incorrect. My suggested edits to your answer is the correct place for this analysis. – Ted D. – 2019-03-03T14:36:00.637
@TedD., check the improvise Formula, Y'day only I've sent through CHAT room,, https://chat.stackexchange.com/rooms/90503/average-calc ☺
– Rajesh S – 2019-03-04T09:36:10.270