: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): stop hide weekend data from corrupting aggregation (#62405)
Sam Pennington2cc2154fa431
diff --git a/frontend/src/queries/nodes/InsightViz/InsightResultMetadata.tsx b/frontend/src/queries/nodes/InsightViz/InsightResultMetadata.tsxindex d7bcdf6b184a..b3ff1852534f 100644--- a/frontend/src/queries/nodes/InsightViz/InsightResultMetadata.tsx+++ b/frontend/src/queries/nodes/InsightViz/InsightResultMetadata.tsx@@ -31,7 +31,7 @@ export const InsightResultMetadata = ({{trendsFilter?.hideWeekends && featureFlags[FEATURE_FLAGS.PRODUCT_ANALYTICS_HIDE_WEEKENDS] ? (<span className="text-secondary"><span className="mx-1">•</span>- Weekend data excluded+ Weekends hidden</span>) : null}</>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 1dcc7f2b9660..c0ce5a8a89ee 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@@ -7506,11 +7506,11 @@ class TestTrendsQueryRunner(ClickhouseTestMixin, APIBaseTest):trends_filters=TrendsFilter(hideWeekends=True),)- # Weekly buckets are preserved, only event counts change- assert len(response_hidden.results[0]["days"]) == len(response_normal.results[0]["days"])- # 10 total events, 5 on weekends (Jan 11 Sat, Jan 12 Sun x3, Jan 19 Sun)- assert response_normal.results[0]["count"] == 10.0- assert response_hidden.results[0]["count"] == 5.0+ # Week buckets span weekends, so hiding weekends is a no-op here — buckets and counts+ # are identical to the normal response (we never drop weekend events from aggregation).+ assert response_hidden.results[0]["days"] == response_normal.results[0]["days"]+ assert response_hidden.results[0]["data"] == response_normal.results[0]["data"]+ assert response_hidden.results[0]["count"] == response_normal.results[0]["count"] == 10.0def test_hide_weekends_with_compare(self):self._create_test_events()@@ -7562,6 +7562,57 @@ class TestTrendsQueryRunner(ClickhouseTestMixin, APIBaseTest):assert len(formula_result["data"]) == len(formula_result["days"])assert formula_result["count"] == sum(formula_result["data"])+ @parameterized.expand(+ [+ # Weekly active users: each weekday's sliding-window value must still count weekend+ # events, so the weekday values match the non-hidden query (would drop under the bug).+ (+ "weekly_active",+ "2020-01-09",+ "2020-01-20",+ [EventsNode(event="$pageview", math=BaseMathType.WEEKLY_ACTIVE)],+ None,+ [+ "2020-01-09",+ "2020-01-10",+ "2020-01-13",+ "2020-01-14",+ "2020-01-15",+ "2020-01-16",+ "2020-01-17",+ "2020-01-20",+ ],+ [1, 1, 3, 3, 4, 4, 4, 2],+ ),+ # Cumulative: the running total keeps weekend events folded in, so Monday 2020-01-13+ # is already 6 (would be lower if weekend events were filtered out of the aggregation).+ (+ "cumulative",+ "2020-01-09",+ "2020-01-19",+ [EventsNode(event="$pageview")],+ ChartDisplayType.ACTIONS_LINE_GRAPH_CUMULATIVE,+ ["2020-01-09", "2020-01-10", "2020-01-13", "2020-01-14", "2020-01-15", "2020-01-16", "2020-01-17"],+ [1, 1, 6, 6, 8, 8, 9],+ ),+ ]+ )+ def test_hide_weekends_does_not_corrupt_windowed_math(+ self, _name, date_from, date_to, series, display, expected_days, expected_data+ ):+ self._create_test_events()++ response = self._run_trends_query(+ date_from,+ date_to,+ IntervalType.DAY,+ series,+ trends_filters=TrendsFilter(hideWeekends=True, display=display),+ )++ assert response.results[0]["days"] == expected_days+ assert response.results[0]["data"] == expected_data+@parameterized.expand([(diff --git a/posthog/hogql_queries/insights/trends/trends_query_builder.py b/posthog/hogql_queries/insights/trends/trends_query_builder.pyindex 59241278b009..6f12e9abb312 100644--- a/posthog/hogql_queries/insights/trends/trends_query_builder.py+++ b/posthog/hogql_queries/insights/trends/trends_query_builder.py@@ -854,10 +854,6 @@ class TrendsQueryBuilder(DataWarehouseInsightQueryMixin):))- # Hide weekends — exclude Saturday (6) and Sunday (7) events- if self.query.trendsFilter and self.query.trendsFilter.hideWeekends:- filters.append(parse_expr("toDayOfWeek(timestamp) <= 5"))-if len(filters) == 0:return ast.Constant(value=True)diff --git a/posthog/hogql_queries/insights/trends/trends_query_runner.py b/posthog/hogql_queries/insights/trends/trends_query_runner.pyindex ed687cefe433..5bce70a20f7c 100644--- a/posthog/hogql_queries/insights/trends/trends_query_runner.py+++ b/posthog/hogql_queries/insights/trends/trends_query_runner.py@@ -512,9 +512,9 @@ class TrendsQueryRunner(AnalyticsQueryRunner[TrendsQueryResponse]):final_result = [item for item in final_result if not self._is_other_breakdown(item["breakdown_value"])]has_more = True- # Weekend filtering is two layers: the WHERE clause (in trends_query_builder) excludes- # weekend events from aggregation, and this post-processor removes weekend date buckets- # from the response so the chart x-axis shows only weekdays.+ # Hiding weekends is purely a display concern: we keep weekend events in the aggregation+ # (so windowed math like WAU/MAU, cumulative, and smoothing stay correct) and only drop the+ # weekend date buckets from the response so the chart x-axis shows weekdays.# For week/month intervals we keep all buckets since they span multiple days.# For hour/minute intervals we skip bucket removal to avoid discarding all data on weekends.if (