diff --git a/posthog/hogql_queries/web_analytics/web_analytics_query_runner.py b/posthog/hogql_queries/web_analytics/web_analytics_query_runner.py index ae743b6ccfd34..b73772ef79a90 100644 --- a/posthog/hogql_queries/web_analytics/web_analytics_query_runner.py +++ b/posthog/hogql_queries/web_analytics/web_analytics_query_runner.py @@ -57,32 +57,29 @@ def property_filters_without_pathname( ) -> list[Union[EventPropertyFilter, PersonPropertyFilter, SessionPropertyFilter]]: return [p for p in self.query.properties if p.key != "$pathname"] - def period_aggregate(self, function_name, column_name, start, end, alias=None, params=None, compare=False): - if compare: - expr = ast.Call( - name=function_name + "If", - params=params, - args=[ - ast.Field(chain=[column_name]), - ast.Call( - name="and", - args=[ - ast.CompareOperation( - op=ast.CompareOperationOp.GtEq, - left=ast.Field(chain=["start_timestamp"]), - right=start, - ), - ast.CompareOperation( - op=ast.CompareOperationOp.Lt, - left=ast.Field(chain=["start_timestamp"]), - right=end, - ), - ], - ), - ], - ) - else: - expr = ast.Call(name=function_name, params=params, args=[ast.Field(chain=[column_name])]) + def period_aggregate(self, function_name, column_name, start, end, alias=None, params=None): + expr = ast.Call( + name=function_name + "If", + params=params, + args=[ + ast.Field(chain=[column_name]), + ast.Call( + name="and", + args=[ + ast.CompareOperation( + op=ast.CompareOperationOp.GtEq, + left=ast.Field(chain=["start_timestamp"]), + right=start, + ), + ast.CompareOperation( + op=ast.CompareOperationOp.Lt, + left=ast.Field(chain=["start_timestamp"]), + right=end, + ), + ], + ), + ], + ) if alias is not None: return ast.Alias(alias=alias, expr=expr) diff --git a/posthog/hogql_queries/web_analytics/web_overview.py b/posthog/hogql_queries/web_analytics/web_overview.py index 11e703dc3f082..24ba05dbc8096 100644 --- a/posthog/hogql_queries/web_analytics/web_overview.py +++ b/posthog/hogql_queries/web_analytics/web_overview.py @@ -236,14 +236,16 @@ def outer_select(self) -> ast.SelectQuery: end = self.query_date_range.date_to_as_hogql() def current_period_aggregate(function_name, column_name, alias, params=None): - return self.period_aggregate( - function_name, column_name, mid, end, alias=alias, params=params, compare=self.query.compare - ) + if not self.query.compare: + return ast.Call(name=function_name, params=params, args=[ast.Field(chain=[column_name])]) + + return self.period_aggregate(function_name, column_name, mid, end, alias=alias, params=params) def previous_period_aggregate(function_name, column_name, alias, params=None): - return self.period_aggregate( - function_name, column_name, start, mid, alias=alias, params=params, compare=self.query.compare - ) + if not self.query.compare: + return ast.Alias(alias=alias, expr=ast.Constant(value=None)) + + return self.period_aggregate(function_name, column_name, start, mid, alias=alias, params=params) if self.query.conversionGoal: select = [