With the criteria written in cells the Dmin (or Dmax or Dsum) function works: Data sample
X Y Z X Y A A 1 A A A A 2 A B 2 A B