Skip to content
Advertisement

Running total with several conditions in bigquery

I need to calculate a running total but need to reset the total on a condition (when expected reached = 0 and product_group and product changes). Got help here without the two extra fields: Calculate a running total with a condition in BigQuery I have this table and may use product_group and product as integers or strings as below.

Any suggestions how to adapt this query (answer from the other post) that works fine without the two extra fields-

The problem is that COUNTIF(Expected_reached = 0) OVER(ORDER BYdate) grp starts over when the product_group or product changes and I get non unique groups so the running total SUM(Expected_reached) OVER(PARTITION BY grp ORDER BYdate) Running_Total doesnt calculate correctly.

Advertisement

Answer

You just simply need to add PARTITION BY Product_group, Product to both analytic functions

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement