How create new survey Embedded Data Age from Embedded "BirthDate" for reporting purposes? | XM Community
Skip to main content

I have an embedded data field "BirthDate" set in survey flow, it is a field included on my contacts list and contacts list is imported automatically on daily basis. I want to create Age to use for reporting age ranges in my dashboard. I've gotten lost in related posts in community page. I am so confused. How do I calculate age from Embedded Data "BirthDate" (note: MM/DD/YYYY format) AND where do I put the information to create it? If anyone knows or can help, I'd greatly appreciate it!

Define the embedded data field 'age' at the beginning of the survey flow.
Add moment.js to your survey header.
Add the following JS to a question:
Qualtrics.SurveyEngine.addOnload(function() {
var bdate = moment("${e://Field/BirthDate}","MM/DD/YYYY");
Qualtrics.SurveyEngine.setEmbeddedData('age',moment().diff(bdate,'years'));
});


Thanks so much for your quick response! I tried to follow what you suggested and the "age" field is blank for test respondents/after completing a few test surveys.
1) I added moment.js in the HTML portion of Header under Look & Feel settings. Is that correct location?
2) And, is this what i put when you say "moment.js"?

">https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js">
3) In survey flow, I added age embedded data field, with "Value will be set from panel or URL", is that accurate or do i need to set the value?
4) Is the format of BirthDate value an issue (i.e., 10/12/1971)? Note: Birthdate is already in survey flow as embedded data field.


https://www.qualtrics.com/community/discussion/comment/30748#Comment_30748From what you've written, it appears you've done things correctly. I saw one typo in the code that I corrected above: 'field' should have been 'Field'.


hello I used this code which works fine except for some respondents where age is not calculated. is it that the person did not stay on the question for a long time?


https://www.qualtrics.com/community/discussion/comment/37076#Comment_37076Hard to say without seeing the specifics, but some possible reasons:

  1. BirthDate field not populated

  2. BirthDate field in wrong format or not a valid date

  3. Question containing JS is not displayed (i.e., skipped)


Strange, however, the date of birth field is present. The same format as the others. And I put the JS on a question posted to all respondents. It's very rare but it happens


https://www.qualtrics.com/community/discussion/comment/37079#Comment_37079If the BirthDates are valid and formatted properly, then perhaps those responses are subverting the JS somehow. You should take a closer look at them; there may other 'suspicious' aspects to them.


HI! I am actually having the same issue for a current survey. For some reason, out of thousands of survey participants there are about 60 people who are missing values in the new "age" field/whose age is not populating. JRKBM were you able to figure out what the issue was?


no sorry I gave up


Leave a Reply