Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Hidden Values Example 2: Calculate Using a Date

A common calculation is to take the date of the Last Menstrual Period (LMP) and calculate the Estimated Date of Delivery (EDD).
In general terms, the way we do this is by taking the date of LMP and adding approximately 9 months to it.

In CommCare terms, we want to take the value of the question "Date of Last Menstrual Period" and add 274 days (9 months + 1 week) to it. The resulting date is the EDD!

Let's try it out!

  • Create a new hidden value by clicking on the 'Add Question' followed by Hidden Value from the question tree.
  • Change the Question ID to "edd"
  • Click on "Edit" next to the Calculate Condition box
  • Drag the question "Date of Last Menstrual Period" over to the Expression Editor (Advanced) box until you see the green check mark, and then release
  • Write a "+" because we want to add something to this date
  • Type "274." Your total expression should look like this:

  • At the beginning of the expression write date and then put parentheses around the expression like this: 

The reason you have to write date first and then put the parentheses around the expression is that when you add an integer number to the question lmp, the result is by default stored as a string, not as a date. We have to "cast" it as a date by putting date first and then parentheses around the expression. Otherwise it will just look like a long number!

  • Hit "Save" and you will be ready to go!


Awesome! I have a slightly different type of calculation but I'm not sure how to do it...

Great- we have a lot of resources for this and now that you understand the general principles, there are a lot you can do with the instructions on our Common Logic and Calculations page. If you can't figure it out using those resources, send a note to the commcare users group!

You can also use the same tools to write complex AND/OR statements for Display or Validation conditions

 

In the next part of the tutorial you will learn how to write an "if" statement.

Go to the next part

  • No labels