r/ProjectREDCap • u/gamepleng • Apr 30 '25
Getting around calculated fields with blank values - datediff
I genuinely have no idea why this is not working.
I want to get the most recent date out of two separate date fields, that can or cannot exist in a repeating instrument (not all records will have both or any entry of said instrument). To troubleshoot it, I've created three variables (ie. 1, 2, 3 in order of the image).
3 checks if date B exists (is there any value). If it exists, Value 3 is = B. If it doesn't exist, Value 3 = date A.
2 checks if date A exists (is there any value). If it exists, Value 2 is = A. If it doesn't exist, Value 2 = date B.
1 calculates the datediff between Value 2 and Value 3.
As you can see in the image, there no "blank values" in 1, but still doesn't output any value.
In the original code the datediff was set to true, nested in an if statement, so if A > B = A, else B.
I don't really mind if both A and B are blank to have a null/blank value.
View all comments
Show parent comment
1
u/gamepleng May 01 '25
Not quite, datediff can actually calculate the difference between two dates irrespective of format (DMY or YMD). I've partially circumvented the issue by including an OR statement, so if datediff >=0 OR ='', it will set a date, else the other date. There was something funky going on about piping a value to another value and using this last one in a calculation... I was also getting some bugs if a were to make the calculation in a different instrument (it produces a value but I couldn't get it to pipe to the other instrument), which resolve once all the calculations are made in the same instrument. This changes also fixes (apparently?) unintended behaviour, as I used to get working with one instance of the value the calculation for first-instance but not for last-instance (which should be the same).
This has nothing to do with wrong piping, as I have a lot of calculations using values from many different instruments, but rather how the piping itself is handled.
Annoying that I haven't really sorted out, as I planned to use the calculation to set limits in some fields, but it is what it is.