如何复制累积字段

问题描述 投票:-1回答:1

我试图从我在Datastudio内部准备的XLS复制数据,

enter image description here

维度为YYYYMM,输出为MonthlyRate。基本上累积响应/累积会计= MonthlyRate

我应该如何准备Datastudio内的计算字段和图形?

我当前使用的计算字段是Sum(Response)/ Count(Meetings)并使用尺寸YYYYMM(YearMonth)。在球场上有一个跑动平均值,但最终会导致略微偏斜的数字。例如201809 0.45变为0.47。

sql excel google-bigquery google-data-studio
1个回答
2
投票

下面的示例适用于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   

结果:

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   
© www.soinside.com 2019 - 2024. All rights reserved.