To make formula variable to work, you must keep PR in rows. This is Bex limitation.
You can create a CKF by putting a constant 1 with exeception agregation of counter of all detailed values which are not null or zero with reference char as PR. This should give you count.