not sure what you are asking, do you need to calculate the average for each student? As to the conditional calculation based on a code, you can use a formula. In the formula would add each value, and probably keep a counter (both shared variables) and then in the footer, display /calculate the new value if the flag is set.
shared numbervar vSum;
shared numbervar vCount;
for the detail:
if (some condition is met) then (
vCount := vCount+1;
vSum := vSum +{table.Grade};
);
for the footer:
shared numbervar vSum;
shared numbervar vCount;
if {table.field}= (flag value, like 3) then
vSum / vCount;
something like that, I am sure that there is more to the calculation, but this would be for a simple average, you can adjust as you need.
HTH