Making an instance ID using the concat() function

10 views
Skip to first unread message

David Edwards

unread,
Jun 19, 2026, 6:28:41 PM (10 days ago) Jun 19
to redcap open
Hello all,

I'm having trouble referencing a particular date from a particular instance of a repeating form in a datediff() function.

Here's what it looks like:

    datediff([randomisation_arm_1][ra_rand_dt], [treatment_arm_1][ts_last_treatment_sess_dt][aggregate-max:ts_last_treatment_sess_in:record-name], 'd')

... where [aggregate-max:ts_last_treatment_sess_in:record-name] is a smart variable that gets me the instance I need - on it's own it works fine and brings back the instance number I expect. So that's not the problem.

When I stick it in the datediff() function in the above format, it doesn't work. And I'm not really surprised by that, cos I guess it's coming out as ...

    [treatment_arm_1][ts_last_treatment_sess_dt]instance_number

...when  REDCap expects...

     [treatment_arm_1][ts_last_treatment_sess_dt][instance_number]

So I tried using the concat() function to put the square brackets around it, like so ...

    datediff([randomisation_arm_1][ra_rand_dt], concat("[treatment_arm_1][ts_last_treatment_sess_dt][", [aggregate-max:ts_last_treatment_sess_in:record-name], "]"), 'd')

... but that didn't work either.

If I stick just this part...

    concat("[treatment_arm_1][ts_last_treatment_sess_dt][", [aggregate-max:ts_last_treatment_sess_in:record-name], "]")

... into a @CALCTEXT field, it comes out like this...

    document.form.ts_last_treatment_sess_dt.valuedocument.form.treatment_arm_1.value[document.form.treatment_arm_1.value14]

I've tried swapping out thew square brackets for HTML entities or unicode equivalents, but no cigar.

I tried it with round brackets instead of square ones and it works fine, so something about those square brackets throws it for a wobbly.

I just wondered if anyone had come across this before?

Thanks,
David.

Amit Tandon

unread,
Jun 21, 2026, 10:39:44 AM (9 days ago) Jun 21
to Redcap Open
If you simplify it by creating two hidden variables. One for instance number ([aggregate-max:ts_last_treatment_sess_in:record-name]) and the next variable would be [treatment_arm_1][ts_last_treatment_sess_dt][previous_variable]. This way the overly complex formulae would become simpler. Hope it may work
Reply all
Reply to author
Forward
0 new messages