Skip to Content
Docs

Calculated fields

Combine number fields and saved aggregations with arithmetic in a rule condition, such as a card's declines divided by its approvals.

A calculated field is an arithmetic expression that Pay.com evaluates for each payment using number fields and saved aggregations. Use one on either side of a rule condition, for example to compare a card's declines with its approvals or a payment's amount with the customer's average.

Build a calculated field

Build the expression in a rule condition:

  1. 1

    Open the builder

    In the rule's Logic section, set the condition's field type to Calculated Field and click Build calculated field. To use the expression as the value, choose Calculated Field as the value type on the right instead.

  2. 2

    Write the expression

    In Expression, click fields in the right-hand column to insert them, and type the operators and numbers between them. The column groups the fields by category, and Search finds a field by its name or category.

  3. 3

    Save it

    Click Save Calculated Field. The condition shows the expression, with each field as a tag.

  4. 4

    Compare it

    Choose an operator and a value, for example is greater than 0.5.

Fields you can use

The right-hand column lists the fields that hold a number:

  • Number fields of the payment, such as Amount (in USD) and Amount (in Processing currency).
  • Your merchant context fields of type Number.
  • Saved aggregations that return a number, under Aggregations. These are aggregations with the Count, Sum, or Count Unique function.

A custom aggregation can't be inserted directly. Save it first with Save in Aggregation Fields in the aggregation builder, or in Rule Inputs.

Expression syntax

An expression combines fields and numbers with these operators, shown here with two fields, A and B:

OperatorMeaningExample
+AddA + B
-Subtract, or make a value negativeA - B
*MultiplyA * 3
/DivideA / B
^Raise to a powerA ^ 2
% between two valuesRemainder of a divisionA % 10
% after a numberPercentageA * 10% (10% of A)
( and )Group terms(A + B) / 2

Numbers can be whole or decimal, such as 3, 0.5, or .5, or in exponent form, such as 1e3 for 1,000. These make the expression invalid:

  • Functions and constants, such as min, max, abs, round, or pi.
  • Multiplying without *, such as 2 directly before a field. Write 2 * before the field.
  • Thousands separators, such as 1,000.
  • Comparison and logic symbols, such as =, >, or &. The condition's operator performs the comparison.

When an expression isn't valid, Save Calculated Field shows Invalid expression. Validation evaluates the expression with every field set to 1, and rejects it if that divides by zero. For example, A / (B - 1) is rejected even when B is rarely 1.

How expressions are evaluated

Pay.com calculates the expression for each payment, then compares the result:

  • No rounding: The result is compared as calculated, so compare against a range (such as is greater than 0.5) rather than an exact value.
  • Aggregations with no past payments: Count as 0.
  • Dividing by zero: Produces an infinitely large number, which matches is greater than and does not equal. Zero divided by zero matches no operator.

If any field in the expression has no value on the payment, Pay.com can't evaluate the expression. Most operators then fail to match, except does not equal, which evaluates to true. Avoid using does not equal with calculated fields that reference fields that can be empty.

Where you can use it

A calculated field works on either side of a condition:

  • On the left: Supports equals, does not equal, is in, is not in, is less than, is greater than, is less than or equal, and is greater than or equal.
  • On the right: Offered when the left side is a number, with equals, does not equal, is less than, is greater than, is less than or equal, or is greater than or equal.

A number you type as the condition's value can't be negative. To compare with a negative number, swap the terms. For example, instead of A - B is less than -5, write B - A is greater than 5.

Examples

These examples use saved aggregations, named here by what they hold:

GoalSaved aggregationsCondition
A card declined more often than approvedCard declines: Count of Declines for the same Payment method (Card), over the Last 24 Hour(s). Card approvals: Count of Approvals, with the same grouping and period.Card declines / (Card declines + Card approvals) is greater than 0.5
A payment three times the customer's averageCustomer spend: Sum of Charges (USD) for the same Customer ID, over the Last 90 Day(s). Customer charges: Count of Charges, with the same grouping and period.Amount (in USD) is greater than Customer spend / Customer charges * 3
Disputes as a share of a BIN's chargesBIN disputes: Count of Disputes for the same Card BIN, over All time. BIN charges: Count of Charges, with the same grouping and period.BIN disputes / BIN charges * 100 is greater than 1
A customer's spend today, including this paymentSpend today: Sum of Charges (USD) for the same Customer ID, since the start of the Day.Spend today + Amount (in USD) is greater than 2000

In the average example, a customer with no past charges gives zero divided by zero, so the condition doesn't match their first payment.

Next steps

Was this page helpful?