Try this array formula

=SUM(IF(FREQUENCY(IF(D5:D102>0,MATCH(A5:A102,A5:A102,0)),ROW(A5:A102)-ROW(A5)),1,0))

Confirm with Ctrl+Shift+Enter and not just Enter