You can't embed an aggregation function into another (sum or avg into count), you need to use advanced aggregation to do something like this (check the aggr() function for more details).
The exact syntax is depending on the dimensions you want to use for the advanced aggregations, but it will look maybe like
Count
(
aggr
(
if(Minimum='NA' or (Sum(Maximum)-Avg(TotalSample) )<= (Avg(TotalSample)-Sum(Minimum)) , Sum(Maximum)-Avg(TotalSample), Avg(TotalSample)-Sum(Minimum) ) /3/Stdev(TotalSample)