Please try this in your upload C18:P20.
Formula: copy to clipboard
=SUM(COUNTIFS(INDEX($Q$2:$S$12,,MATCH($B18,$Q$1:$S$1,0)),"*",C2:C12,{"P","EX"}))