# How to automate patient tracking

**URL:** <https://community.dhis2.org/t/how-to-automate-patient-tracking/42304>\
**Category:** Support - Assistance technique\
**Created:** [23 March 2021 13:02 UTC](https://community.dhis2.org/t/how-to-automate-patient-tracking/42304 "2021-03-23T13:02:59Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![brian](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/brian/32/48251_2.png) [@brian](https://community.dhis2.org/u/brian)\
**Post date:** [13 July 2021 11:55 UTC](https://community.dhis2.org/t/how-to-automate-patient-tracking/42304/3 "2021-07-13T11:55:05Z")

</div>

Hi there @fernando

I understand you would like to use a program rule to display a warning or block user input if a maximum number of events has been reached.

I was not able to find precisely what you are looking for: calculate within a program rule the total number of events for a particular program stage in the enrollment.

**However** , it is possible to calculate the total number of values for a particular data element across the enrollment, and show that as a warning. I would recommend you calculate this for one of the _required_ data elements in your program stage (or any auto-assigned value within the stage).

Example: on Play 2.36.3, the RMNCH program. A warning box is under the first data element in the stage showing the total number of values for “diastolic blood pressure” in the enrollment.

 ![image](https://dhis2.b-cdn.net/uploads/default/original/3X/6/0/60f14c435116a2a78422861b2ae7f5ff9cfc3586.png)

Set the program rule condition to “true” and the program rule expression uses d2:count function

 ![image](https://dhis2.b-cdn.net/uploads/default/original/3X/5/9/596435ae744d70a0a1907efacbc6b2e597ee0783.png)

You could use similar method to show an **error** if d2:count({required\_data\_element\_id}) exceeds maximum allowed stages.

Note you could also substitute the d2:count(\<required\_data\_element\_id\>) for V{event\_count}. This would calculate the total number of events in the enrollment across _ **all** _ stages – even those without a value for the “required” data element.

---

_[View the full topic](https://community.dhis2.org/t/how-to-automate-patient-tracking/42304)._
