> ## Documentation Index
> Fetch the complete documentation index at: https://learn.kubit.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Query formulas

> Combine Query measures into calculated metrics such as ratios and differences with formulas like A / B, and the limits per chart type.

A formula combines the measures in a Query report into a calculated metric with an arithmetic expression. Instead of reading raw Count or Sum values one at a time, you compute ratios, differences and other derived metrics in the report. This page covers how formulas reference measures, the supported operators, limits per chart type, when a formula is required and how formulas are validated.

Add a formula under **Formula(s)** in the report builder by clicking **Add formula**.

![Report builder with Measure A counting events and Measure B counting unique users, and the Formula(s) section highlighted with formula F1 set to A/B.](https://mintcdn.com/kubit/9i8QHxqek50tMBGy/images/reports/query/formula.png?fit=max\&auto=format\&n=9i8QHxqek50tMBGy\&q=85\&s=05b3b93c1e9b21cf563611800b022b32)

## When to use

| Use case | Example |
| - | - |
| Ratio of two measures | Successful events divided by total events per day |
| Per-user metric | Sum of revenue divided by count distinct users |
| Difference between measures | Sum of revenue minus sum of refunds |
| Single value for a chart type that needs one | A Histogram or Heatmap built from several measures |

## How formulas work

Each measure in a report gets a letter: the first measure is A, the second is B, the third is C, and so on. A formula uses these letters to define the calculation.

| Example formula | What it computes |
| - | - |
| `A / B` | Ratio of measure A to measure B |
| `(A + B) / C` | Sum of A and B, divided by C |
| `A * 100 / B` | A as a percentage of B |
| `A - B` | Difference between A and B |

Formulas are case-insensitive. Typing `a/b` is the same as `A/B`.

## Supported operators

| Operator | Description |
| - | - |
| `+` | Addition |
| `-` | Subtraction |
| `*` | Multiplication |
| `/` | Division |
| `( )` | Parentheses for grouping |

## Limits

| Chart type | Max formulas |
| - | - |
| Histogram | **1** |
| Heatmap | **1** |
| All others | **3** |

**Availability:** a formula can reference only measure letters (A, B, C and so on). It can't reference another formula.

## When a formula is required

Some chart types need a formula when the report has several measures (a compound measure), because the chart shows a single computed value.

| Chart type | Formula with several measures |
| - | - |
| Histogram | **Required** |
| Heatmap | **Required** |
| Treemap | **Required** |
| Waterfall | **Required** |
| Bar, Line, Combo, KPI | Optional. Each measure renders on its own. |

[Chart types](https://learn.kubit.ai/product-analytics/reports/query/chart-types.md) lists the measures each chart type needs.

## Division formulas

A formula with the `/` operator changes two settings.

| Setting | What happens |
| - | - |
| **Overall** toggle in **Group By** | Turns on automatically, since ratio calculations across groups need it |
| Pie chart percentage view | Turns off, since a ratio can't be shown as a percentage of the total |

## Validation

Kubit checks a formula as you type. If the expression is invalid, for example with unbalanced parentheses or a letter that isn't a measure, an error message appears. The report doesn't run until the formula is fixed.

## MCP prompt examples

> "Show me the ratio of successful events to total events per day over the last 14 days"

> "Create a line chart of (sum revenue minus sum refunds) divided by count distinct users, weekly, last 3 months"

## Related

* [Query report](https://learn.kubit.ai/product-analytics/reports/query.md): build the measures a formula uses.
* [Advanced Query analysis](https://learn.kubit.ai/product-analytics/reports/query/advanced-analysis.md): build formulas from rolling measures.
* [Group by and binning](https://learn.kubit.ai/product-analytics/reports/common/group-by-and-binning.md): the Overall toggle and breakdowns.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.