This video explains how to find the count of particular values in the fields and pass it as an in-out parameter.
Counter values - IIF(validity = 'invalid',counter+1,counter)
Expression to set in-out parameter value - SetMaxVariable($$Validity_counter, NewField)
$$Validity_counter is an in-out parameter here, NewField is passed from the previous expression which contains the counter values.