Auto-increment field in a given pattern

**The problem? **
I'm trying to get away from repeat group in one my forms, but keep the 'integrity' of data as if it were within a single dataset.

I have two relevant fields, call them 'Code' and 'Number'
Each Code has an (unknown) set of 'children' ('Number') in a pattern like

A1, A2, A3, A4
B1, B2
C1, C2, C3 etc

In a previous iteration of my form the 'Number' was a repeat-group. Up until now I've just asked enumerators to fill these fields manually, but I'm trying to simplify (but making life more complicated for myself).
So I have used default values (in XLS Form) using ${last-saved# but my IF() statement isn't working - I wonder if anyone can (gently) point out my stupidity:

Code: ${last-saved#Code}

Number: if(${Code}=${last-saved#Code},${last-saved#Number}+1,'1')

I have tried replacing '1' (the else part of the statement) with int('1') and just plain old 1 but I still get a blank in 'Number' if the 'Code' changes. But it then works as expected as the 'Number' increments...

Please note that I've changed the names of records to protect the innocent - they are not really called A B and C!

Hopefully this is a simple one for the sharp of mind, and I'm sorry for posting it if it's really obvious.

1 Like

Looks pretty good to me. Maybe you could post an example .xlsx file to help us debug?

To find the problem, I'd look for potential issues with type conversion. Is Number an integer or text or calculate field? Try make it a text field and add conversions, like this:

if(${Code} = string(${last-saved#Code}),
    string(number(${last-saved#Code}) + 1),

I also have this doubt, could you please point or exemplify in XML format where I put this expression?