Math operations? Embedded data calculation of employee tenure | XM Community
Skip to main content

I have an employee lifecycle survey and its participants come from the Global Directory with a metadata field called Hire Date. This is formatted as MM-DD-YYYY
(e.g. 08-23-2022)
How can I create an embedded data field in the survey flow in my survey that calculates the difference in the number of days between today (the day that the employee takes the survey) and their hire date?
How would that math operation be written?

Thanks so much! :)

helena888 you need to do date math with JavaScript, which has to be included in your license to use.
First you will want to put the following code in your survey header

  1. Open the Look and Feel menu

  2. Select general

  3. Click "edit" under Header

  4. Select "<>" in the menu and input the code below


In your Survey Flow, add Embedded Data as your first element. Put in the variables HireDate and Tenure. Make sure HireDate is calling from your sample and formatted as date (open the Options menu of the Set Embedded Data element). Also make sure Tenure and set it as type Number Set.
On any question in your survey (recommend the first one so it is easy to find) add the following JavaScript.
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
 //reads in the embedded data field named HireDate and saves it in variable hiredt
 var hiredt = moment("${e://Field/HireDate}");
//define variable today that calls in the current date
var today = moment();
 //calculate tenure using built in date difference function
var tenure = moment(today).diff(moment(hiredt),'days');
//save result in embedded varialbe Tenure
Qualtrics.SurveyEngine.setEmbeddedData('Tenure', tenure);

});


bstrahin hi sir, I was able to use this for one of my surveys! Big thanks!
Is there a way where we can group the tenure days?
Like 0-30 results will yield "Below 30 days"
30 - 60 days will yield "30 - 60 days", and so on.
Probably 5 ranges only.
Also, the survey I used this on already completed. Will it still show up values? I checked on the Data & Analysis table, and all are blank.
image.png


Nogstai your Tenure variable is blank because the variable where you saved the date is not named the same as it is in the code. The code is looking for a field named HireDate, your data set is showing that you named this variable Current Hire Date. You would have had to have these named correctly to get the Javascript to work. You have some options for fixing:

  1. Export your data and create the date math and bands you want in an external program then reimport the data to use Qualtrics for data analysis/chart generation

  2. Do all of the math and charting externally

  3. Fix your Javascript. Add in Javascript that compares the variable tenure to the ranges you want and assign this to a new variable TenureBand that you need to declare in Survey Flow. (If you aren't comfortable with Javascript you can also us branching logic in Survey Flow to compare tenure to your bands and assign it to the correct value in TenureBand). Then you would have to retake all responses to get the Javascript to run.


  1. https://community.qualtrics.com/XMcommunity/discussion/comment/54165#Comment_54165I tried this, but I have 54 questions, and added a timing question for each, and when I was trying to import I have almost 400 columns needed to revise, and it won't let me import without having to go through all of them.

  2. Do all of the math and charting externally - They want everything in Qualtrics 😥

  3. I originally fixed the Javascript to capture the Current Hire Date, I think I made a mistake at the beginning on the embedded data in Survey Flow.

So this is what I added:
image.pngSo this is how I edited the Jscript:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
 //reads in the embedded data field named HireDate and saves it in variable hiredt
 var hiredt = moment("${e://Field/Current%20Hire%20Date}");
//define variable today that calls in the current date
var today = moment();
 //calculate tenure using built in date difference function
var tenure = moment(today).diff(moment(hiredt),'days');
//save result in embedded varialbe Tenure
Qualtrics.SurveyEngine.setEmbeddedData('Tenure', tenure);

});
Unfortunately, I am not knowledgeable in Javascripting.


Nogstai how many responses do you need to correct? Is this embedded data your first element? Do you ever define Current Higher Date to the left like HireDate and Tenure? I assume you will want to recalculate Tenure date based on their actual survey completion date and not the date you are attempting to fix the problem on?
Please answer these questions to the best of your ability so I can try to help you with a solution.


https://community.qualtrics.com/XMcommunity/discussion/comment/54174#Comment_541741.) All 45k responses. The initial issue we had was the tenure date was not included in the uploaded participant list.
2.) I added the embedded data but not as the first element (first mistake)
3.) I can't search for the Current Hire Date as an embedded data. I can't find it in the drop downs.
4.) Yes, I will base it on the survey completion date.


Nogstai 45K is so many! I am at a roadblock on how to efficiently get those fixed for you. Javascript isn't going to run on pre-recorded responses. You need Javascript to do date math. Imports aren't working for you and 45K is way too many to use retake links in order to have the calculations work (even then code would have to be changed to preserve the math on the correct date and not today).
Hopefully there's an even smarter community member that can help you fix this considering you end user wants everything staying in Qualtrics.
Deepak has been very active and really helpful so maybe he can pick up with some solutions where I am stuck. I'm not sure if you are looking at having to write API to update responses or if that is even possible. API is way out of my wheel house but the last option I can come up with to possibly correct the data.


https://community.qualtrics.com/XMcommunity/discussion/comment/54182#Comment_54182Tried to understand the issue from the whole thread and came up to this conculsion.

  1. Yes, 45k is too many and the API runs ~10k at a time so need to run webservice at least 5 times I believe. You can use this API to update all your responses. By externally calculating the difference between dates for all your past responses.


Hope it helps!


You can use Qualtrics bucketing variable creation option too under Data & Analysis. You can create new field and bucket your number of days if your group list is not big.
https://www.qualtrics.com/support/survey-platform/data-and-analysis-module/data/add-new-fields/bucketing-variable-creation/
Else use update response API to update 10000 response at a time.
https://ca1.qualtrics.com/API/v3/responses/responseID


Leave a Reply