Cognito Forms supports robust calculations that allow you to create powerful and customized forms. Calculations can be used to compute field values, set default values, conditionally control visibility of fields/sections, and implement rich validation, like conditionally required fields and calculated ranges.

## The basics

In the Calculation box of a field, you can enter a constant/literal value, like so:

Or, you can enter a more complex calculation. Calculations must start with an = in order to tell Cognito that you are trying to calculate a value:

For example, for the default value of a Number field, you could either enter 2 or enter = 1 + 1. Both are equivalent, but the first is a constant value and the second is a calculation. For the default value for a Date field, you could either enter “1/1/2015” or enter “Today”. The first would always be the constant date you specified, whereas the second is a calculation that always represents the current date.

Cognito supports auto-completion to show you the values that are appropriate to enter in the calculation, including field names, names of functions, etc.

Similarly, you can type a “.” to access a field or function on a section or value.

Cognito also supports an error indicator to help you know when a calculation is not working and exactly where the calculation stopped working.

## Conditional logic

Cognito provides a builder for creating complex conditional calculations (those that evaluate to true or false), such as when you are controlling the visibility of a section or making a field conditionally required. The builder lets you specify conditions for one or more fields, in an intuitive user interface that eliminates the complexities of writing the calculations yourself. Though, you can always switch to the Advanced Editor to see what these calculations look like behind the user interface. Learn more about what you can do with conditional logic.

## Working with Yes/No

Cognito makes it easy to add Yes/No fields to any form, but the real secret here is that these fields are also great to use in calculations. Unlike Choice fields that are really just Text values, Yes/No fields represent actual Yes/No values, called Boolean values. Also, many of the calculation operations and functions return Yes/No values, so it is important to understand what you can do with these types of values within your calculations.

Type | Example | Results for GolfOnFriday = false |
---|---|---|

Equals | `=GolfOnFriday = true` |
false = true = false |

Not Equals | `=GolfOnFriday != true` |
false != true = true |

Not or Negation | `=!GolfOnFriday` |
!false = true |

Anding | `=GolfOnFriday and true` or `=GolfOnFriday && true` |
false and true = false |

Oring | `=GolfOnFriday or true` or `=GolfOnFriday !=true` |
false or true = true |

If Yes Then This Else That | `=GolfOnFriday ? 25 : 0` or `=if GolfOnFriday then 25 else 0` |
False ? 25 : 0 = 0 |

Also, just like mathematical calculations, you should put parenthesis () around your Yes/No calculations, especially when using Or (a or b), or And (a and b), or the (Yes/No ? Yes Value : No Value) operations.

## Cross-section calculations

When inside of a section, you can reference form data from other sections or the main form. Say that you have a section with a number field in it, and you need to reference a different number field that’s in the main form area:

You can access fields/sections at the top level of the form by typing “Form.” inside calculations. After entering Form., you can choose which field in the main form you want to reference.

Now, the final result will pull the data from the main form (Number 1) into a section (Number 2):

### Referencing parent/sibling sections

Referencing other sections essentially works the same way; just as before, type “Form.” into your calculation. When the list of fields/sections appears, first select whichever section you need to reference. Then, you can choose the specific field that you’re pulling data from.

You can follow this same process down as many levels of sections as you need:

### Referencing a repeating section

What if you need to reference fields from a repeating section in another section within it? This works very similarly to referencing regular sections, except now you’re going to type in “ParentSection.” instead of “Form.” into your calculation. When the list of fields appears, select the one you need.

Now, the field in the section can pull the data from a field in the repeating section: