Let me try to provide some pointers and let the PB experts chip in later.
To me the simplest way to do this is to have ProcessBook point to an AF attribute or a PI Perf Equation instead of calculating this within the application.
If you want to do this within the confines of PB, then PI ProcessBook contains Microsoft's Visual Basic for Applications (VBA) scripting support. You can use the PIExpression Dataset's Expression Property
where the expression would be EventCount('<tag>', '<starttime>', '<endtime>')
The programming guide can be accessed from PB Help menu
Thank you !