title: How do I use DateDiff() to give me a difference by grouping and not just for the entire dataset?
body: <p>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. </p>
  
  <p>My attempt is as follows: </p>
  
  <p>@TimeSinceLastInteraction_Initialize</p>
  
  <pre><code>global numbervar interaction := 0; 
  if {Ticket.PatientID} &lt;&gt; previous({Ticket.PatientID}) then interaction
  else interaction :=0;
  </code></pre>
  
  <p>This formula is placed in group heading for patientID in order to reset the value for each group and seems to be working</p>
  
  <p>@TimeSinceLastInteraction</p>
  
  <pre><code>global numbervar interaction;
  interaction := DateDiff("d",maximum({Ticket.TicketDate}),CurrentDateTime);
  </code></pre>
  
  <p>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. </p>
  
  <p>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!</p>
  
  <p>Thanks in advance!</p>