Cognito Forms logo

Calculations overview

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.

Getting started

1. The Basics

2. Conditional Logic

Using calculations

3. Working with Text

4. Working with Numbers

5. Working with Dates

6. Working with Yes/No

7. Working with Repeating Sections or Tables

8. Cross section calculations

9. Related topics


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 Text

Most data collected via forms is usually some form of text.  For example, the Textbox, Choice, Rating Scale, Name, Address, Phone, Email, Website, and Calculation (Text) field types all represent text values.

You can specify constant text values in your expressions by simply putting text inside quotes, "like this".  Also, text fields that are blank will have a value of null. Here are examples of basic text calculations you can perform for text fields like the Name field:

Type

Example

Results for name of John Smith

Concatenation

= Name.First + " " + Name.Last

"John" + " " + "Smith" = "John Smith"

Equals

= Name.First = "John"

"John" = "John" = true

Is Blank

= Name.First = null

"John" = null = false

Is Not Blank

= Name.First != null

"John" != null = true

Starts With

= Name.First.StartsWith("J")

"John".StartsWith("J") = true

Contains

= Name.Last.Contains("th")

"Smith".Contains("th") = true

Make Lowercase

= Name.FirstAndLast.ToLower

"John Smith".ToLower() = "john smith"

Remove Spaces

= Name.First.Trim

"John".Trim = "John"

 

This list highlights a small number of functions for manipulating text values.  See Text Functions for comprehensive documentation of supported text functions.

Working with Numbers

Cognito makes it easy to write calculations that work with numbers. Start by using the appropriate field types to represent numbers, including Number, Currency, and Calculation (Number, Percent & Currency).

For example, say you have two Number fields on your form, Adults and Children, representing the number of adults and children registering for an event. You could add a Calculation field that shows the total number of people attending the event, using the simple calculation  =Adults + Children.

Now, on your form, you can see this calculation in action:

Here are examples of basic numeric calculations you could perform with these fields:

Type

Example

Results for 2 Adults and 3 Children

Addition

=Adults + Children

2 + 3 = 5

Subtraction

=Adults - Children

2 - 3 = -1

Multiplication

=Adults * Children

2 * 3 = 6

Division

=Adults / Children

2 / 3 = 0.66667

Equals

=Adults = Children, or
=Adults == Children

2 = 3 = false

Not Equals

=Adults != Children, or
=Adults <> Children

2 != 3 = true

Greater Than

=Adults > Children

2 > 3 = false

Greater Than or Equals

=Adults >= Children

2 >= 3 = false

Less Than

=Adults < Children

2 < 3 = true

Less Than or Equals

=Adults <= Children

2 <= 3 = true

 

When performing basic mathematical calculations, Cognito will treat blank values as zeros, so for example, 2 + blank would equal 2, and 2 > blank would equal true.

Additionally, you can perform more complex mathematical calculations by calling functions starting with Math, including important functions like Min, Max, Ceiling, Floor and Round. For example, you could write the calculation  = Math.Max(Adults, Children) to calculate the maximum of these two values. See Math Functions for comprehensive documentation of supported math functions.

Working with Dates

Dates are an essential component of any form involved with scheduling activities or recording prior events. Cognito makes it easy to capture real, valid dates, while also providing a rich set of calculation options to validate and manipulate these dates. To capture Date input, just add a Date field and select the Date type (instead of Time).  You can also create Calculated fields of Date type.

Type

Example

Results for 1/1/2015 and 1/31/2015

Equals

=StartDate = EndDate

1/1/2015 = 1/31/2015 = false

Not Equals

=StartDate != EndDate, or
=StartDate <> EndDate

1/1/2015 != 1/31/2015 = true

Greater Than

=StartDate > EndDate

1/1/2015 > 1/31/2015 = false

Greater Than or Equals

=StartDate >= EndDate

1/1/2015 >= 1/31/2015 = false

Less Than

=StartDate < EndDate

1/1/2015 < 1/31/2015 = true

Less Than or Equals

=StartDate <= EndDate

1/1/2015 <= 1/31/2015 = true

Subtraction

=(EndDate – StartDate).Days

(1/31/2015 - 1/1/2015).Days = 30

Today

=DateTime.Today

DateTime.Today = 5/6/2014

Add Days

=StartDate.AddDays(10)

(1/1/2015).AddDays(10) = 1/11/2015

Day of Week

=StartDate.DayOfWeek

(1/1/2015).DayOfWeek = "Thursday"

 

This list highlights a small number of functions for manipulating dates. You can view Date/Time Functions for comprehensive documentation of supported Date and Time functions. Also see Timespan Functions for documentation of what you can do with the result of subtracting two dates or times, such as the preceding subtraction example.

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.

Working with Repeating Sections or Tables

One of the unique features of Cognito Forms is the ability to capture repeating data on forms, such as a variable list of emergency contacts or a list of job references.  To capture repeating data, just add a Repeating Section or Table (coming soon) to your form, and add the fields you want to capture in the repeating section.  The following extensive example demonstrates how to create an event signup sheet and calculate a variety of things based on who is attending:

This registration form allows families to sign up for a conference hosted at a resort hotel.  One registration form allows multiple adult or child attendees to sign up, select optional t-shirt sizes, register for golf to kick off the conference, and prepay for discounted arcade money to use throughout the resort.  Here is a detailed breakdown of all of the calculations used to implement this form, highlighting the ease with which calculations can be created for repeating data using Cognito:

Calculated Field

Calculation

Number of Adults

=Attendees.Count(AdultOrChild = "Adult")
 

Adult Registration Fee

=NumberOfAdults * 150
 

Number of Children

=Attendees.Count(AdultOrChild = "Child")
 

Child Registration Fee

=Math.Max(NumberOfChildren - NumberOfAdults, 0) * 25

or

=NumberOfChildren > NumberOfAdults ? (NumberOfChildren – NumberOfAdults) * 25 : 0
 

Number of T-Shirts

=Attendees.Count(TShirt != "None")
 

T-Shirt Fee

=Attendees.Count(TShirt.Contains("Youth")) * 15 + Attendees.Count(TShirt.Contains("Adult")) * 20
 

Number Playing Golf On Friday

=Attendees.Count(GolfOnFriday)
 

Golf Registration Fee

=NumberPlayingGolfOnFriday * 65
 

Total Arcade Bucks

=Attendees.Sum(ArcadeBucks)
 

Arcade Bucks Fee

=TotalArcadeBucks * 0.75
 

Total Registration Fee

=AdultRegistrationFee + ChildRegistrationFee + TShirtFee + GolfRegistrationFee + ArcadeBucksFee
 

 

This example makes heavy use of Count and Sum functions for repeating data, but there are a number of additional functions supported for this type of data:

Returns

Name

Description

Yes/No

All(Yes/No Calculation)

Determines whether all items of a repeating section or table satisfy a condition.

Yes/No

Any()

Determines whether a repeating section or table contains any items.

Yes/No

Any(Yes/No Calculation)

Determines whether any items in a repeating section or table satisfy a condition.

Number

Average(Numeric Calculation)

Computes the average of a numeric calculation for each item in a repeating section or table.

Number

Count()

Returns the number of items in a repeating section or table.

Number

Count(Yes/No Calculation)

Returns the number of items in a repeating section or table that satisfy a condition.

Item

First()

Returns the first item in a repeating section or table.

Item

First(Yes/No Calculation)

Returns the first item in a repeating section or table that satisfies a condition.

Item

Last()

Returns the last item in a repeating section or table.

Item

Last(Yes/No Calculation)

Returns the last item in a repeating section or table that satisfies a condition.

Item Where(Item Number = 1) Returns a specific item in a repeating section or table as denoted by the item number.

Number

Max(Numeric Calculation)

Computes the maximum value of a numeric calculation for all items in a repeating section or table.

Number

Min(Numeric Calculation)

Computes the minimum value of a numeric calculation for all items in a repeating section or table.

Number

Sum(Numeric Calculation)

Computes the summation of a numeric calculation for all items in a repeating section or table.

List

Where(Yes/No Calculation)

Filters the items in a repeating section or table based on a condition.

 

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:

Related Topics

Printable View