Skip to content

Variables and calculations

A variable is a value KyraForm works out from answers as people fill in the form, such as a total price, an age, a score or a deadline. Variables can be:

  • shown on the form with a Display Value,
  • quoted in question text or end screens with Insert answer → Variables,
  • used as validation and date limits,
  • used in conditions,
  • used as a payment amount.
  1. Open any question, then Advanced → Calculate from other answers. This opens Data calculations, which lists every variable on the form.

  2. Select Create New Variable.

  3. Fill in:

    SettingWhat to enter
    Variable NameA short name with no spaces, such as totalAmount or userAge. Required.
    TypeNumber, Text, True/False or Date.
    Description (Optional)What it calculates, for your teammates.
    Calculation ModeFormula, Function or Value mapping. See below.
  4. Select Create Variable.

Variables update immediately as respondents type. The value is recalculated on the server when the form is submitted, so it can’t be tampered with.

Write a Calculation Formula using question IDs, variables and functions. Click a question under Available Fields (Click to insert) to insert its ID, and the function buttons to insert functions.

Examples:

NeedFormula
Line totalquantity * unit_price
Total with 18% GSTsubtotal * 1.18
Average of three marksavg(maths, science, english)
Age from date of birthage(dob)
Age on a given dateageFromDate(dob, event_date)
Due date 30 days after the startaddDays(start_date, 30)
Full nameconcat(first_name, " ", last_name)
FunctionReturns
sum(a, b, …)Total of the values
avg(a, b, …)Average of the values
Math.min(a, b, …) / Math.max(a, b, …)Smallest / largest value
abs(x), floor(x), ceil(x), sqrt(x), pow(x, y)Absolute value, round down, round up, square root, x to the power y
age(dob)Whole years from a date of birth until today
ageFromDate(dob, date)Whole years from a date of birth until another date
addDays(date, n), addMonths(date, n), addYears(date, n)A date moved forward (or back, with a negative n)
concat(a, b, …)Text joined together

Use these to total a Table Grid column:

FunctionReturns
tableSum("table_id", "column_id")Sum of the column
tableAvg("table_id", "column_id")Average of the column
tableMin("table_id", "column_id") / tableMax(…)Smallest / largest value in the column
tableCount("table_id")Number of rows
tableCountFilled("table_id", "column_id")Rows where the column is filled in

Put the table’s ID and the column’s ID in quotes. Clicking them in the editor inserts them for you. The column must be a Number or Calculated column. Otherwise you see “No numeric columns — add a Number or Calculated column to use aggregate functions.”

If the formula has a mistake, the editor shows Formula error with the reason.

For logic a formula can’t express, write a small function.

  1. Under Parameters, select Add Parameter for each question you need, and give it a param name.
  2. Write the Function Body in JavaScript-style code that returns the value.
  3. Select Test Function. “Test runs with sample values (numbers = 1, text = "").”

Example, a tiered discount:

if (qty >= 100) return price * qty * 0.8;
if (qty >= 10) return price * qty * 0.9;
return price * qty;

Turn each option of a choice question into a number or text. Pick the Source Field, then fill in the Mapped Value for each option under Map Options to Values.

Example: Plan → Basic = 499, Standard = 999, Premium = 1999. Then use the variable in a total, or directly as the payment amount.

  • Name variables for what they mean (totalFee, not v1). They appear in exports and in other formulas.
  • Build in small steps: a subtotal variable, then tax = subtotal * 0.18, then total = subtotal + tax. Variables can use other variables, and KyraForm works out the order.
  • Use Preview to test with real answers before publishing.