sqlexcelgoogle-bigquerylooker-studio

How can I replicate a cumulative field?


I am trying to replicate the data from an XLS that I have prepared inside of Looker Studio.

enter image description here

The dimension is YYYYMM and the output is MonthlyRate. Basically CumulativeResponse/CumulativeMeetings = MonthlyRate

How should I prepare the calculated field and graph inside of Looker Studio?

The calculated field i am currently using is Sum(Response) / Count(Meetings) and using the dimension YYYYMM(YearMonth). with a running average on the field, however the end results in a slightly skewed figure. E.g for 201809 0.45 becomes 0.47.


Solution

  • Below example is for BigQuery Standard SQL

    #standardSQL
    WITH `project.dataset.table` AS (
      SELECT '201712' yyyymm, 4580 SumResponse, 6741 CountMeetings UNION ALL
      SELECT '201801', 3574, 6926 UNION ALL
      SELECT '201802', 2020, 6433 UNION ALL
      SELECT '201803', 1895, 6635 UNION ALL
      SELECT '201804', 2174, 6163 UNION ALL
      SELECT '201805', 3058, 7697 UNION ALL
      SELECT '201806', 3313, 7838 UNION ALL
      SELECT '201807', 4043, 8586 UNION ALL
      SELECT '201808', 5053, 9355 UNION ALL
      SELECT '201809', 1122, 1300 
    )
    SELECT 
      yyyymm, 
      SumResponse, 
      SUM(SumResponse) OVER(ORDER BY yyyymm) CumulativeResponse , 
      CountMeetings,
      SUM(CountMeetings) OVER(ORDER BY yyyymm) CumulativeMeetings,
      SUM(SumResponse) OVER(ORDER BY yyyymm)/SUM(CountMeetings) OVER(ORDER BY yyyymm) MonthlyRate 
    FROM `project.dataset.table`
    ORDER BY yyyymm   
    

    with result:

    Row yyyymm  SumResponse CumulativeResponse  CountMeetings   CumulativeMeetings  MonthlyRate  
    1   201712  4580        4580                6741            6741                0.6794244177421748   
    2   201801  3574        8154                6926            13667               0.596619594644033    
    3   201802  2020        10174               6433            20100               0.5061691542288557   
    4   201803  1895        12069               6635            26735               0.45143070880867775  
    5   201804  2174        14243               6163            32898               0.4329442519302085   
    6   201805  3058        17301               7697            40595               0.4261854908239931   
    7   201806  3313        20614               7838            48433               0.4256188962071315   
    8   201807  4043        24657               8586            57019               0.4324348024342763   
    9   201808  5053        29710               9355            66374               0.4476150299816193   
    10  201809  1122        30832               1300            67674               0.4555959452670154