How do I use DateDiff() to give me a difference by grouping and not just for the entire dataset?

I have my data grouped by provider and then by patientID, I want to find the last time a patient "interacted" with the clinic but when I use datediff it seems to give use the latest date for the entire data set instead of by group. My attempt is as follows: @TimeSinceLastInteraction_Initialize

global numbervar interaction := 0; if <> previous() then interaction else interaction :=0; 

This formula is placed in group heading for patientID in order to reset the value for each group and seems to be working @TimeSinceLastInteraction

global numbervar interaction; interaction := DateDiff("d",maximum(),CurrentDateTime); 

I have tried several different date fields but I always get the same, undesired result where the value returned for each grouping is the same. This makes me think I am not setting up interaction as a variable correctly. I have also tried local vs global variable to no avail. As a small side note, I don't really understand the difference between local and global variable declarations (I usually get away with just using global) and would appreciate an explanation on their respective uses just as much as any help on my current problem! Thanks in advance!