:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(insights): compute ratio-of-sums for trends formula totals (#70653)
posthog[bot]1c670c3aac8e
diff --git a/ee/hogai/chat_agent/trends/prompts.py b/ee/hogai/chat_agent/trends/prompts.pyindex a0189609bdcd..93bae736917b 100644--- a/ee/hogai/chat_agent/trends/prompts.py+++ b/ee/hogai/chat_agent/trends/prompts.py@@ -22,6 +22,11 @@ For trends queries, use an appropriate ChartDisplayType for the output. For exam- if the data is easy to understand in a pie chart, use `ActionsPie`.- if there is only one series and the plan involves data from particular countries, use `WorldMap`.+When the plan asks for a ratio, rate, or percentage (e.g. a bounce rate, a conversion rate, or "share of X"), express it as the raw ratio and let the axis format render the percentage:+* Build the value as the plain ratio in the 0-1 range — either a `math` aggregation that returns 0-1 (e.g. `avg` of `$is_bounce`) or a formula like `A/B`. Do not multiply by 100 in the formula.+* Set `aggregationAxisFormat` to `percentage_scaled`, which multiplies the 0-1 value by 100 and appends `%` (so `0.5` renders as `50%`). Never combine `percentage_scaled` with a `*100` in the formula, as that double-scales the value (rendering `0.5` as `5000%`).+* Only use `percentage` (not `percentage_scaled`) when the value is already on the 0-100 scale.+The plan might specify insights for groups. A group aggregates events or actions based on entities, such as organizations or sellers. The plan might provide a list of group names and their numeric indexes. Instead of a group's name, always use its numeric index.You can determine if a feature flag is enabled by checking if it's set to true or 1 in the `$feature/...` property. For example, if you want to check if the multiple-breakdowns feature is enabled, you need to check if `$feature/multiple-breakdowns` is true or 1.diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.jsonindex 8aba07803f47..b75906f36866 100644--- a/frontend/src/queries/schema.json+++ b/frontend/src/queries/schema.json@@ -4440,7 +4440,7 @@"aggregationAxisFormat": {"$ref": "#/definitions/AggregationAxisFormat","default": "numeric",- "description": "Formats the trends value axis. Do not use the formatting unless you are absolutely sure that formatting will match the data. `numeric` - no formatting. Prefer this option by default. `duration` - formats the value in seconds to a human-readable duration, e.g., `132` becomes `2 minutes 12 seconds`. Use this option only if you are sure that the values are in seconds. `duration_ms` - formats the value in miliseconds to a human-readable duration, e.g., `1050` becomes `1 second 50 milliseconds`. Use this option only if you are sure that the values are in miliseconds. `percentage` - adds a percentage sign to the value, e.g., `50` becomes `50%`. `percentage_scaled` - formats the value as a percentage scaled to 0-100, e.g., `0.5` becomes `50%`. `currency` - formats the value as a currency, e.g., `1000` becomes `$1,000`."+ "description": "Formats the trends value axis. Do not use the formatting unless you are absolutely sure that formatting will match the data. `numeric` - no formatting. Prefer this option by default. `duration` - formats the value in seconds to a human-readable duration, e.g., `132` becomes `2 minutes 12 seconds`. Use this option only if you are sure that the values are in seconds. `duration_ms` - formats the value in miliseconds to a human-readable duration, e.g., `1050` becomes `1 second 50 milliseconds`. Use this option only if you are sure that the values are in miliseconds. `percentage` - appends a percentage sign to a value that is ALREADY on the 0-100 scale, e.g., `50` becomes `50%`. Only use this when the underlying value is already a percentage. `percentage_scaled` - multiplies a 0-1 value by 100 and appends a percentage sign, e.g., `0.5` becomes `50%`. Use this for ratios in the 0-1 range, such as a bounce rate (`avg($is_bounce)`) or a formula like `A/B`. Because this format already multiplies by 100, do NOT also multiply by 100 in the formula (e.g. `A/B*100`), as that would double-scale the value and render, say, `0.5` as `5000%`. `currency` - formats the value as a currency, e.g., `1000` becomes `$1,000`."},"aggregationAxisPostfix": {"description": "Custom postfix to add to the aggregation axis, e.g., ` clicks` to format 5 as `5 clicks`. You may need to add a space before postfix. Never set a postfix that `aggregationAxisFormat` already renders: `percentage` and `percentage_scaled` already append the `%` sign, so a `%` postfix would render values as `50%%`.",@@ -4478,7 +4478,7 @@"type": "string"},"formulaNodes": {- "description": "Use custom formulas to perform mathematical operations like calculating percentages or metrics. Use the following syntax: `A/B`, where `A` and `B` are the names of the series. You can combine math aggregations and formulas. When using a formula, you must:\n- Identify and specify **all** events and actions needed to solve the formula.\n- Carefully review the list of available events and actions to find appropriate entities for each part of the formula.\n- Ensure that you find events and actions corresponding to both the numerator and denominator in ratio calculations. Examples of using math formulas:\n- If you want to calculate the percentage of users who have completed onboarding, you need to find and use events or actions similar to `$identify` and `onboarding complete`, so the formula will be `A / B`, where `A` is `onboarding complete` (unique users) and `B` is `$identify` (unique users).",+ "description": "Use custom formulas to perform mathematical operations like calculating percentages or metrics. Use the following syntax: `A/B`, where `A` and `B` are the names of the series. You can combine math aggregations and formulas. When using a formula, you must:\n- Identify and specify **all** events and actions needed to solve the formula.\n- Carefully review the list of available events and actions to find appropriate entities for each part of the formula.\n- Ensure that you find events and actions corresponding to both the numerator and denominator in ratio calculations. Examples of using math formulas:\n- If you want to calculate the percentage of users who have completed onboarding, you need to find and use events or actions similar to `$identify` and `onboarding complete`, so the formula will be `A / B`, where `A` is `onboarding complete` (unique users) and `B` is `$identify` (unique users). For a ratio or percentage, keep the formula as the raw ratio (e.g. `A/B`, which is in the 0-1 range) and set `aggregationAxisFormat` to `percentage_scaled` so it renders as a percentage. Do NOT multiply the formula by 100 (e.g. `A/B*100`) when using `percentage_scaled`, or the value will be scaled twice.","items": {"$ref": "#/definitions/TrendsFormulaNode"},diff --git a/frontend/src/queries/schema/schema-assistant-queries.ts b/frontend/src/queries/schema/schema-assistant-queries.tsindex b4702797d55d..f368be43e703 100644--- a/frontend/src/queries/schema/schema-assistant-queries.ts+++ b/frontend/src/queries/schema/schema-assistant-queries.ts@@ -460,6 +460,7 @@ export interface AssistantTrendsFilter {* - Ensure that you find events and actions corresponding to both the numerator and denominator in ratio calculations.* Examples of using math formulas:* - If you want to calculate the percentage of users who have completed onboarding, you need to find and use events or actions similar to `$identify` and `onboarding complete`, so the formula will be `A / B`, where `A` is `onboarding complete` (unique users) and `B` is `$identify` (unique users).+ * For a ratio or percentage, keep the formula as the raw ratio (e.g. `A/B`, which is in the 0-1 range) and set `aggregationAxisFormat` to `percentage_scaled` so it renders as a percentage. Do NOT multiply the formula by 100 (e.g. `A/B*100`) when using `percentage_scaled`, or the value will be scaled twice.*/formulaNodes?: TrendsFormulaNode[]@@ -496,8 +497,8 @@ export interface AssistantTrendsFilter {* `numeric` - no formatting. Prefer this option by default.* `duration` - formats the value in seconds to a human-readable duration, e.g., `132` becomes `2 minutes 12 seconds`. Use this option only if you are sure that the values are in seconds.* `duration_ms` - formats the value in miliseconds to a human-readable duration, e.g., `1050` becomes `1 second 50 milliseconds`. Use this option only if you are sure that the values are in miliseconds.- * `percentage` - adds a percentage sign to the value, e.g., `50` becomes `50%`.- * `percentage_scaled` - formats the value as a percentage scaled to 0-100, e.g., `0.5` becomes `50%`.+ * `percentage` - appends a percentage sign to a value that is ALREADY on the 0-100 scale, e.g., `50` becomes `50%`. Only use this when the underlying value is already a percentage.+ * `percentage_scaled` - multiplies a 0-1 value by 100 and appends a percentage sign, e.g., `0.5` becomes `50%`. Use this for ratios in the 0-1 range, such as a bounce rate (`avg($is_bounce)`) or a formula like `A/B`. Because this format already multiplies by 100, do NOT also multiply by 100 in the formula (e.g. `A/B*100`), as that would double-scale the value and render, say, `0.5` as `5000%`.* `currency` - formats the value as a currency, e.g., `1000` becomes `$1,000`.* @default numeric*/diff --git a/posthog/hogql_queries/insights/trends/test/test_formula.py b/posthog/hogql_queries/insights/trends/test/test_formula.pyindex f0c32bf31fb3..a86a257bcc3e 100644--- a/posthog/hogql_queries/insights/trends/test/test_formula.py+++ b/posthog/hogql_queries/insights/trends/test/test_formula.py@@ -289,7 +289,9 @@ class TestFormula(ClickhouseTestMixin, APIBaseTest):self._run({"trendsFilter": {"formula": "(A/3600)/B"}})[0]["data"],[0.0, 0.0, 0.0, 0.0, 0.0, 1 / 1200, 1 / 1800, 0.0],)- self.assertEqual(self._run({"trendsFilter": {"formula": "(A/3600)/B"}})[0]["count"], 1 / 720)+ # The total is the formula applied to each series' summed value (ratio-of-sums):+ # (1800/3600)/750, not the sum of the per-interval results (1/1200 + 1/1800 = 1/720).+ self.assertEqual(self._run({"trendsFilter": {"formula": "(A/3600)/B"}})[0]["count"], 1 / 1500)self.assertEqual(self._run({"trendsFilter": {"formula": "A/0"}})[0]["data"],diff --git a/posthog/hogql_queries/insights/trends/test/test_trends_query_runner.py b/posthog/hogql_queries/insights/trends/test/test_trends_query_runner.pyindex 41f029f4f583..6f9551cee01f 100644--- a/posthog/hogql_queries/insights/trends/test/test_trends_query_runner.py+++ b/posthog/hogql_queries/insights/trends/test/test_trends_query_runner.py@@ -820,6 +820,27 @@ class TestTrendsQueryRunner(ClickhouseTestMixin, APIBaseTest):self.assertEqual("Formula (A-B)", response.results[1]["label"])self.assertEqual([1, 0, 0, 2, -2, 0, 2, -1, 1, 0, 1], response.results[1]["data"])+ def test_ratio_formula_total_is_ratio_of_sums(self):+ self._create_test_events()++ response = self._run_trends_query(+ self.default_date_from,+ self.default_date_to,+ IntervalType.DAY,+ [EventsNode(event="$pageview"), EventsNode(event="$pageleave")],+ TrendsFilter(formulas=["A/B"]),+ )++ self.assertEqual(1, len(response.results))+ # Per-interval line stays as the daily ratio (division by zero renders as 0).+ self.assertEqual(+ [0, 0, 1, 3, 1 / 3, 0, 0, 0, 0, 0, 0],+ response.results[0]["data"],+ )+ # The total is the ratio of the summed series (sum(A)/sum(B) = 10/6), not the sum of the+ # daily ratios (which would be 1 + 3 + 1/3 = 4.333 and nonsensically overshoot the ratio).+ self.assertAlmostEqual(10 / 6, response.results[0]["count"])+def test_formula_with_compare(self):self._create_test_events()@@ -1554,25 +1575,27 @@ class TestTrendsQueryRunner(ClickhouseTestMixin, APIBaseTest):response.results.sort(key=lambda r: r["count"])- assert response.results[0]["label"] == "Formula (A+B)"+ # The total is the formula applied to each series' summed values, so the constant in+ # `B+1` counts once, not once per interval: sum(B) + 1, not sum(B_i + 1).+ assert response.results[0]["label"] == "Formula (B+1)"assert response.results[0]["breakdown_value"] == cohort1.pk- assert response.results[0]["count"] == 9- assert response.results[0]["data"] == [0, 0, 2, 2, 2, 0, 1, 0, 1, 0, 1, 0]+ assert response.results[0]["count"] == 4+ assert response.results[0]["data"] == [1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1]assert response.results[1]["label"] == "Formula (B+1)"- assert response.results[1]["breakdown_value"] == cohort1.pk- assert response.results[1]["count"] == 15- assert response.results[1]["data"] == [1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1]+ assert response.results[1]["breakdown_value"] == "all"+ assert response.results[1]["count"] == 7+ assert response.results[1]["data"] == [1, 1, 2, 2, 4, 1, 1, 2, 1, 1, 1, 1]assert response.results[2]["label"] == "Formula (A+B)"- assert response.results[2]["breakdown_value"] == "all"- assert response.results[2]["count"] == 16- assert response.results[2]["data"] == [1, 0, 2, 4, 4, 0, 2, 1, 1, 0, 1, 0]+ assert response.results[2]["breakdown_value"] == cohort1.pk+ assert response.results[2]["count"] == 9+ assert response.results[2]["data"] == [0, 0, 2, 2, 2, 0, 1, 0, 1, 0, 1, 0]- assert response.results[3]["label"] == "Formula (B+1)"+ assert response.results[3]["label"] == "Formula (A+B)"assert response.results[3]["breakdown_value"] == "all"- assert response.results[3]["count"] == 18- assert response.results[3]["data"] == [1, 1, 2, 2, 4, 1, 1, 2, 1, 1, 1, 1]+ assert response.results[3]["count"] == 16+ assert response.results[3]["data"] == [1, 0, 2, 4, 4, 0, 2, 1, 1, 0, 1, 0]# action needs to be unset to display custom labelassert response.results[0]["action"] is Nonediff --git a/posthog/hogql_queries/insights/trends/trends_query_runner.py b/posthog/hogql_queries/insights/trends/trends_query_runner.pyindex 83121a00346c..0b2ae686bbde 100644--- a/posthog/hogql_queries/insights/trends/trends_query_runner.py+++ b/posthog/hogql_queries/insights/trends/trends_query_runner.py@@ -1144,7 +1144,15 @@ class TrendsQueryRunner(AnalyticsQueryRunner[TrendsQueryResponse]):series_data = [s["data"] for s in results_group]new_series_data = FormulaAST(series_data).call(formula)base_result["data"] = new_series_data- base_result["count"] = float(sum(new_series_data))+ # The total for a time-series formula is the formula applied to each series' own total+ # (ratio-of-sums), not the sum of the per-interval results. Summing per-interval ratios+ # like `A/B` overcounts and can push a total that should be <=100% well past it. Each+ # input series' total is the sum of its interval values, matching what that series'+ # own total column would show and how the aggregate_values branch above evaluates+ # formulas. This applies to every formula, not just ratios: a constant term in `B+1`+ # counts once in the total, not once per interval.+ series_totals = [[sum(value for value in (series or []) if value is not None)] for series in series_data]+ base_result["count"] = float(FormulaAST(series_totals).call(formula)[0])return base_resultdiff --git a/posthog/schema.py b/posthog/schema.pyindex 8104d78fa6c7..9018cba6f05e 100644--- a/posthog/schema.py+++ b/posthog/schema.py@@ -3963,10 +3963,16 @@ class AssistantTrendsFilter(BaseModel):" seconds. `duration_ms` - formats the value in miliseconds to a"" human-readable duration, e.g., `1050` becomes `1 second 50 milliseconds`."" Use this option only if you are sure that the values are in miliseconds."- " `percentage` - adds a percentage sign to the value, e.g., `50` becomes"- " `50%`. `percentage_scaled` - formats the value as a percentage scaled to"- " 0-100, e.g., `0.5` becomes `50%`. `currency` - formats the value as a"- " currency, e.g., `1000` becomes `$1,000`."+ " `percentage` - appends a percentage sign to a value that is ALREADY on"+ " the 0-100 scale, e.g., `50` becomes `50%`. Only use this when the"+ " underlying value is already a percentage. `percentage_scaled` -"+ " multiplies a 0-1 value by 100 and appends a percentage sign, e.g., `0.5`"+ " becomes `50%`. Use this for ratios in the 0-1 range, such as a bounce"+ " rate (`avg($is_bounce)`) or a formula like `A/B`. Because this format"+ " already multiplies by 100, do NOT also multiply by 100 in the formula"+ " (e.g. `A/B*100`), as that would double-scale the value and render, say,"+ " `0.5` as `5000%`. `currency` - formats the value as a currency, e.g.,"+ " `1000` becomes `$1,000`."),)aggregationAxisPostfix: str | None = Field(@@ -4029,7 +4035,12 @@ class AssistantTrendsFilter(BaseModel):" calculate the percentage of users who have completed onboarding, you need"" to find and use events or actions similar to `$identify` and `onboarding"" complete`, so the formula will be `A / B`, where `A` is `onboarding"- " complete` (unique users) and `B` is `$identify` (unique users)."+ " complete` (unique users) and `B` is `$identify` (unique users). For a"+ " ratio or percentage, keep the formula as the raw ratio (e.g. `A/B`, which"+ " is in the 0-1 range) and set `aggregationAxisFormat` to"+ " `percentage_scaled` so it renders as a percentage. Do NOT multiply the"+ " formula by 100 (e.g. `A/B*100`) when using `percentage_scaled`, or the"+ " value will be scaled twice."),)metricChangeDecreaseColor: str | None = Field(diff --git a/services/mcp/src/tools/generated/query-wrappers.ts b/services/mcp/src/tools/generated/query-wrappers.tsindex 800070efaf88..31de4e9cf2d9 100644--- a/services/mcp/src/tools/generated/query-wrappers.ts+++ b/services/mcp/src/tools/generated/query-wrappers.ts@@ -471,7 +471,7 @@ const TrendsFormulaNode = z.object({const AssistantTrendsFilter = z.object({aggregationAxisFormat: AggregationAxisFormat.describe(- 'Formats the trends value axis. Do not use the formatting unless you are absolutely sure that formatting will match the data. `numeric` - no formatting. Prefer this option by default. `duration` - formats the value in seconds to a human-readable duration, e.g., `132` becomes `2 minutes 12 seconds`. Use this option only if you are sure that the values are in seconds. `duration_ms` - formats the value in miliseconds to a human-readable duration, e.g., `1050` becomes `1 second 50 milliseconds`. Use this option only if you are sure that the values are in miliseconds. `percentage` - adds a percentage sign to the value, e.g., `50` becomes `50%`. `percentage_scaled` - formats the value as a percentage scaled to 0-100, e.g., `0.5` becomes `50%`. `currency` - formats the value as a currency, e.g., `1000` becomes `$1,000`.'+ 'Formats the trends value axis. Do not use the formatting unless you are absolutely sure that formatting will match the data. `numeric` - no formatting. Prefer this option by default. `duration` - formats the value in seconds to a human-readable duration, e.g., `132` becomes `2 minutes 12 seconds`. Use this option only if you are sure that the values are in seconds. `duration_ms` - formats the value in miliseconds to a human-readable duration, e.g., `1050` becomes `1 second 50 milliseconds`. Use this option only if you are sure that the values are in miliseconds. `percentage` - appends a percentage sign to a value that is ALREADY on the 0-100 scale, e.g., `50` becomes `50%`. Only use this when the underlying value is already a percentage. `percentage_scaled` - multiplies a 0-1 value by 100 and appends a percentage sign, e.g., `0.5` becomes `50%`. Use this for ratios in the 0-1 range, such as a bounce rate (`avg($is_bounce)`) or a formula like `A/B`. Because this format already multiplies by 100, do NOT also multiply by 100 in the formula (e.g. `A/B*100`), as that would double-scale the value and render, say, `0.5` as `5000%`. `currency` - formats the value as a currency, e.g., `1000` becomes `$1,000`.').default('numeric').optional(),@@ -520,7 +520,7 @@ const AssistantTrendsFilter = z.object({formulaNodes: z.array(TrendsFormulaNode).describe(- 'Use custom formulas to perform mathematical operations like calculating percentages or metrics. Use the following syntax: `A/B`, where `A` and `B` are the names of the series. You can combine math aggregations and formulas. When using a formula, you must:\n- Identify and specify **all** events and actions needed to solve the formula.\n- Carefully review the list of available events and actions to find appropriate entities for each part of the formula.\n- Ensure that you find events and actions corresponding to both the numerator and denominator in ratio calculations. Examples of using math formulas:\n- If you want to calculate the percentage of users who have completed onboarding, you need to find and use events or actions similar to `$identify` and `onboarding complete`, so the formula will be `A / B`, where `A` is `onboarding complete` (unique users) and `B` is `$identify` (unique users).'+ 'Use custom formulas to perform mathematical operations like calculating percentages or metrics. Use the following syntax: `A/B`, where `A` and `B` are the names of the series. You can combine math aggregations and formulas. When using a formula, you must:\n- Identify and specify **all** events and actions needed to solve the formula.\n- Carefully review the list of available events and actions to find appropriate entities for each part of the formula.\n- Ensure that you find events and actions corresponding to both the numerator and denominator in ratio calculations. Examples of using math formulas:\n- If you want to calculate the percentage of users who have completed onboarding, you need to find and use events or actions similar to `$identify` and `onboarding complete`, so the formula will be `A / B`, where `A` is `onboarding complete` (unique users) and `B` is `$identify` (unique users). For a ratio or percentage, keep the formula as the raw ratio (e.g. `A/B`, which is in the 0-1 range) and set `aggregationAxisFormat` to `percentage_scaled` so it renders as a percentage. Do NOT multiply the formula by 100 (e.g. `A/B*100`) when using `percentage_scaled`, or the value will be scaled twice.').optional(),metricChangeDecreaseColor: zdiff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/query-trends-actors.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/query-trends-actors.jsonindex a79edcaddcde..9acb57612e6a 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/query-trends-actors.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/query-trends-actors.json@@ -3769,7 +3769,7 @@"properties": {"aggregationAxisFormat": {"default": "numeric",- "description": "Formats the trends value axis. Do not use the formatting unless you are absolutely sure that formatting will match the data. `numeric` - no formatting. Prefer this option by default. `duration` - formats the value in seconds to a human-readable duration, e.g., `132` becomes `2 minutes 12 seconds`. Use this option only if you are sure that the values are in seconds. `duration_ms` - formats the value in miliseconds to a human-readable duration, e.g., `1050` becomes `1 second 50 milliseconds`. Use this option only if you are sure that the values are in miliseconds. `percentage` - adds a percentage sign to the value, e.g., `50` becomes `50%`. `percentage_scaled` - formats the value as a percentage scaled to 0-100, e.g., `0.5` becomes `50%`. `currency` - formats the value as a currency, e.g., `1000` becomes `$1,000`.",+ "description": "Formats the trends value axis. Do not use the formatting unless you are absolutely sure that formatting will match the data. `numeric` - no formatting. Prefer this option by default. `duration` - formats the value in seconds to a human-readable duration, e.g., `132` becomes `2 minutes 12 seconds`. Use this option only if you are sure that the values are in seconds. `duration_ms` - formats the value in miliseconds to a human-readable duration, e.g., `1050` becomes `1 second 50 milliseconds`. Use this option only if you are sure that the values are in miliseconds. `percentage` - appends a percentage sign to a value that is ALREADY on the 0-100 scale, e.g., `50` becomes `50%`. Only use this when the underlying value is already a percentage. `percentage_scaled` - multiplies a 0-1 value by 100 and appends a percentage sign, e.g., `0.5` becomes `50%`. Use this for ratios in the 0-1 range, such as a bounce rate (`avg($is_bounce)`) or a formula like `A/B`. Because this format already multiplies by 100, do NOT also multiply by 100 in the formula (e.g. `A/B*100`), as that would double-scale the value and render, say, `0.5` as `5000%`. `currency` - formats the value as a currency, e.g., `1000` becomes `$1,000`.","enum": ["numeric","duration",@@ -3817,7 +3817,7 @@"type": "string"},"formulaNodes": {- "description": "Use custom formulas to perform mathematical operations like calculating percentages or metrics. Use the following syntax: `A/B`, where `A` and `B` are the names of the series. You can combine math aggregations and formulas. When using a formula, you must:\n- Identify and specify **all** events and actions needed to solve the formula.\n- Carefully review the list of available events and actions to find appropriate entities for each part of the formula.\n- Ensure that you find events and actions corresponding to both the numerator and denominator in ratio calculations. Examples of using math formulas:\n- If you want to calculate the percentage of users who have completed onboarding, you need to find and use events or actions similar to `$identify` and `onboarding complete`, so the formula will be `A / B`, where `A` is `onboarding complete` (unique users) and `B` is `$identify` (unique users).",+ "description": "Use custom formulas to perform mathematical operations like calculating percentages or metrics. Use the following syntax: `A/B`, where `A` and `B` are the names of the series. You can combine math aggregations and formulas. When using a formula, you must:\n- Identify and specify **all** events and actions needed to solve the formula.\n- Carefully review the list of available events and actions to find appropriate entities for each part of the formula.\n- Ensure that you find events and actions corresponding to both the numerator and denominator in ratio calculations. Examples of using math formulas:\n- If you want to calculate the percentage of users who have completed onboarding, you need to find and use events or actions similar to `$identify` and `onboarding complete`, so the formula will be `A / B`, where `A` is `onboarding complete` (unique users) and `B` is `$identify` (unique users). For a ratio or percentage, keep the formula as the raw ratio (e.g. `A/B`, which is in the 0-1 range) and set `aggregationAxisFormat` to `percentage_scaled` so it renders as a percentage. Do NOT multiply the formula by 100 (e.g. `A/B*100`) when using `percentage_scaled`, or the value will be scaled twice.","items": {"properties": {"custom_name": {diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/query-trends.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/query-trends.jsonindex a9487a4d8b2b..d5ed819dc875 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/query-trends.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/query-trends.json@@ -3580,7 +3580,7 @@"properties": {"aggregationAxisFormat": {"default": "numeric",- "description": "Formats the trends value axis. Do not use the formatting unless you are absolutely sure that formatting will match the data. `numeric` - no formatting. Prefer this option by default. `duration` - formats the value in seconds to a human-readable duration, e.g., `132` becomes `2 minutes 12 seconds`. Use this option only if you are sure that the values are in seconds. `duration_ms` - formats the value in miliseconds to a human-readable duration, e.g., `1050` becomes `1 second 50 milliseconds`. Use this option only if you are sure that the values are in miliseconds. `percentage` - adds a percentage sign to the value, e.g., `50` becomes `50%`. `percentage_scaled` - formats the value as a percentage scaled to 0-100, e.g., `0.5` becomes `50%`. `currency` - formats the value as a currency, e.g., `1000` becomes `$1,000`.",+ "description": "Formats the trends value axis. Do not use the formatting unless you are absolutely sure that formatting will match the data. `numeric` - no formatting. Prefer this option by default. `duration` - formats the value in seconds to a human-readable duration, e.g., `132` becomes `2 minutes 12 seconds`. Use this option only if you are sure that the values are in seconds. `duration_ms` - formats the value in miliseconds to a human-readable duration, e.g., `1050` becomes `1 second 50 milliseconds`. Use this option only if you are sure that the values are in miliseconds. `percentage` - appends a percentage sign to a value that is ALREADY on the 0-100 scale, e.g., `50` becomes `50%`. Only use this when the underlying value is already a percentage. `percentage_scaled` - multiplies a 0-1 value by 100 and appends a percentage sign, e.g., `0.5` becomes `50%`. Use this for ratios in the 0-1 range, such as a bounce rate (`avg($is_bounce)`) or a formula like `A/B`. Because this format already multiplies by 100, do NOT also multiply by 100 in the formula (e.g. `A/B*100`), as that would double-scale the value and render, say, `0.5` as `5000%`. `currency` - formats the value as a currency, e.g., `1000` becomes `$1,000`.","enum": ["numeric","duration",@@ -3628,7 +3628,7 @@"type": "string"},"formulaNodes": {- "description": "Use custom formulas to perform mathematical operations like calculating percentages or metrics. Use the following syntax: `A/B`, where `A` and `B` are the names of the series. You can combine math aggregations and formulas. When using a formula, you must:\n- Identify and specify **all** events and actions needed to solve the formula.\n- Carefully review the list of available events and actions to find appropriate entities for each part of the formula.\n- Ensure that you find events and actions corresponding to both the numerator and denominator in ratio calculations. Examples of using math formulas:\n- If you want to calculate the percentage of users who have completed onboarding, you need to find and use events or actions similar to `$identify` and `onboarding complete`, so the formula will be `A / B`, where `A` is `onboarding complete` (unique users) and `B` is `$identify` (unique users).",+ "description": "Use custom formulas to perform mathematical operations like calculating percentages or metrics. Use the following syntax: `A/B`, where `A` and `B` are the names of the series. You can combine math aggregations and formulas. When using a formula, you must:\n- Identify and specify **all** events and actions needed to solve the formula.\n- Carefully review the list of available events and actions to find appropriate entities for each part of the formula.\n- Ensure that you find events and actions corresponding to both the numerator and denominator in ratio calculations. Examples of using math formulas:\n- If you want to calculate the percentage of users who have completed onboarding, you need to find and use events or actions similar to `$identify` and `onboarding complete`, so the formula will be `A / B`, where `A` is `onboarding complete` (unique users) and `B` is `$identify` (unique users). For a ratio or percentage, keep the formula as the raw ratio (e.g. `A/B`, which is in the 0-1 range) and set `aggregationAxisFormat` to `percentage_scaled` so it renders as a percentage. Do NOT multiply the formula by 100 (e.g. `A/B*100`) when using `percentage_scaled`, or the value will be scaled twice.","items": {"properties": {"custom_name": {