Select to view content in your preferred language

Multiple calculations in single field fail (concat, substr)

152
7
Monday
GregC
by
Emerging Contributor

I've built a form in Survey123 Connect where I have a field {title} ending in a 2-digit year ("....-25").  I want to use that value and concatenate it with "20" in field to create a 4-digit year in a subsequent field {year}.  While the preview works in Survey 123 Connect, the published form does not.  Some folks troubleshooting the Connect .xls were able to replicate the problem.  

concat('20',string(substr(${title},-2,2)))

The code works as long I don't do both substring and concat, it works in the form.  It will concatenate the whole {title} to {year}, but will never add the substring in the published form.  

Has anyone experienced this or suggest a work around (maybe I need to make an interim, invisible field?).

 

 

0 Kudos
7 Replies
RyanUthoff
MVP Regular Contributor

Do you have anything else configured with your year question? I can't reproduce that behavior. Using your calculation, it seems to work fine for me (in a published survey).

RyanUthoff_0-1747661592947.png

 

0 Kudos
Neal-Kittelson
Frequent Contributor

Splitting the the calculations into an intermediate field appears to work, at least in the field app.

NealKittelson_0-1747661765479.png

 

 However,  The substring calc was doing something wonky in the the Web App.  It was taking the first 2 characters when there was only two character, 1 character when three were entered and 0 characters when 4 or more???

So, works to split if you are using the field app, not sure about the web app.

Edit - I also got it to work without splitting. Same issue in web app though.

0 Kudos
TylerGraham2
Frequent Contributor

The reason this isn't working is the substr start and end values need to be based off of the index position of the characters they want to capture starting with index 0. The numbers used in start/end parameters need to be positive integers. 

I had some other reply written out and then I tried testing in the web form with some example formulas and I got some absolutely wild behavior. Check out the attached video to see what is happening when you use the substr() formula in the webform. I've also attached the xls file.  

Substr -2,2 field uses OP's substr(${text1}, -2,2) layout. It will show the first 2 characters typed in the Example text question, but as you enter 3 or more characters it starts to subtract characters from the field.  

t = t

te = te

tes = e

test = blank

Substr 2,4 field uses properly called index values in substr to grab the 3rd and 4th characters of a string. The formula is  substr(${text1}, 2,4) starting at the 3rd character (index 2 since the first character is index position 0) and ends at 4 since it ends at index-1.  Think of that as a "less than" instead of an "equal to or less than" so you have to go 1 position over the last character index you want to capture. Behavior is as follows:

t = blank

te = blank

tes = s

test = st

testing = st

Substr -2,4 field is calculated substr(${text1}, -2,4). This shows as the following:

t = t

te = te

tes = es

test = st

testi = t

testin = blank

testing = blank

 

 

Neal-Kittelson
Frequent Contributor

This is what I was seeing in the web app. Weird that it works differently in connect and field app.

0 Kudos
Neal-Kittelson
Frequent Contributor

Going off of what @TylerGraham2  said, I looked up some substring syntax.  Try the following calc:  "concat('20',string(substr(${title},-2)))"

This should just be taking the last two characters.

 

https://www.freecodecamp.org/news/how-to-substring-a-string-in-python/

TylerGraham2
Frequent Contributor

Just checked the substr(${text1},-2) and it accepts that as taking the last 2 characters of a string in the webform.  I didn't think that would work since I can't think of any other case in Survey123 where if you skip a parameter for a function it doesn't scream an error at you.  

0 Kudos
Neal-Kittelson
Frequent Contributor

I tried it first and then went back to look at the documentation and was surprised it worked as well!

0 Kudos