Use a pivot table based on your entire data set, with column T as the row field, and column AG as the data field, set to Average. Then every criterion will have an average automatically, without formulas or sorting. You can add further criteria by adding row fields and choosing which values are actually included.

Otherwise, change your formula to

=AVERAGEIF($T:$T,"Criteria B",$AG:$AG)
or enter your starting in, say cell Z2, and use

=AVERAGEIF($T:$T,Z2,$AG:$AG)\
copied down to match.

Also, take a look at AVERAGEIFS