Target update based on Latest form filled

We have a followup_form.
When patient comes for follow up CHWs fill the followup form.
Patients is determined Hypertension controlled and Hypertension uncontrolled on the basis of Bp recorded during follow up.
In Target, Hypertension Controlled and Uncontrolled count is done based on the latest follow up of the patients, but according to our code it counts total follow up of the patients.

Patients can have multiple follow up,but we only need to count latest followup form filled and its data for controlled and uncontrolled Bp.

Note : We are using the same follow form for multiple visits of patients ( e.g.2nd ,3rd, 4th and so on …)

The result due to the code below is 14 patients followed up, but 33 patients were uncontrolled and 10 patients were controlled so it does not tally with real data.

{
  id: 'controlled',
  type: 'count',
  goal: -1,
  translation_key: 'targets.controlled',
  subtitle_translation_key: 'targets.all_time.subtitle',
    context: ' user.role === "flhw"',
  appliesTo: 'reports',
  appliesToType: ['follow_up'], 
  appliesIf: function(contact, report) {
    const hypertensionDiagnosed = Utils.getField(report, 'g2.q13_1_control_uncontrol_checker'); 
    return hypertensionDiagnosed === '0';
  },
  idType: 'report',
  date: 'now',
  aggregate: true
},

  
  {
  id: 'uncontrolled',
  type: 'count',
  goal: -1,
  translation_key: 'targets.uncontrolled',
  subtitle_translation_key: 'targets.all_time.subtitle',
    context: 'user.role === "flhw" ',
  appliesTo: 'reports',
  appliesToType: ['follow_up'], 
  appliesIf: function(contact, report) {
    const hypertensionDiagnosed = Utils.getField(report, 'g2.q13_1_control_uncontrol_checker'); 
    return hypertensionDiagnosed === '1';
  },
  
  idType: 'report',
  date: 'now',
  aggregate: true
}


@Sanjit7 - thanks for posting your issue!

Is it possible for you to post a link to your XLXS form and the source of the g2.q13_1_control_uncontrol_checker() function? This will help us try and debug why there’s a count discrepancy.

I have shared the xls form and the target.js file

Please find the file from following link below :
https://drive.google.com/drive/folders/1fbV8k2nLL7llM9HiVlt7M7ECkkfQ3laP?usp=sharing

If anything required from my side , please feel free to ask.