Thank you @Clayton_Sims and with a little help from @Dev in providing me with some bread crumbs I was able to go deep in the rabbit hole of CommCare trial and error. I'm glad to say I have discovered the solution that is exactly what I wanted for my use case. I'll try my best to articulate my solution for others in the future.
Use Case:
I have a case management system with a registration form and a follow up form that an extension worker fills out and it calculates the infestation rate. The infestation rate is then written to the case file. The extension worker can now see the infestation along with other details. This works great. However, extension workers will follow up with the same farmer and fill in form that calculates infestation rates multiple times. This writes over the case file with the new infestation rate. This is not a problem usually, but extension workers want to see in the case detail the infestation rates from previous follow ups. This is where things get complicated.
What does this look like and how does it work?
Below is an screen shot of my solution.
- Every time the form is filled out for this farmer the % infested at last visit is replaced with the new rate and the old rate is written to % infested 2, and 2 is written to % infested 3, and so on
- I've also included the same for dates so extension workers can see the last infestation rate as well as date (I tested this all in the same sitting which explains the same dates in all)
- I set the limit to the last 5 visits to avoid it being too clunky - you can see that this farmer has been followed up with 7 times but we only see the 5 most recent dates and infestation rates
- If i exported the report I would have all 7 forms that was filled out.
How to do it
I don't think my solution is very elegant but it works and would welcome any suggestions.
Overview:
Key to this was incrementing a counter
- Setup case management to save counter to the case
- Setup hidden value in the form that adds 1 to the counter
- Setup form with hidden values for 5 separate infestation rates that write to case based on sliding window mentioned by @Clayton_Sims above!
it'll look like this:
last_visit_1 = current_collected_rate
last_visit_2 = #case/last_visit_1
last_visit_3 = #case/last_visit_2
last_visit_4 = #case/last_visit_3
last_visit_5 = #case/last_visit_4
Build logic on each hidden value above based on the incrementing counter. I built it such that the hidden value will display and write to the corresponding case property ( 1 to 2, 2 to 3, 3 to 4, etc.) if the counter reaches 2, 3 4, or 5.
What happens if the form is filled out more than 5 times?
Since I only wanted to see the last 5 visits I created 5 case properties to track the 5 different infestation rates. This works great until the incrementing counter reaches 5. Once it goes above that I created additional hidden values (5 more) based on the incrementing counter that converts it back to 1 through 5 and added it to the logic that writes it to the case property.
New_counter_1 = n - (n-1)
New_counter_2 = n - (n-2)
etc.
for examples: 6 - (6-1) = 1
Hopefully the above makes sense and someone might find it useful.
-Tony