Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google sheets 在Google Data Studio中同时使用SUM()和CASE()计算总金额_Google Sheets_Google Data Studio - Fatal编程技术网

Google sheets 在Google Data Studio中同时使用SUM()和CASE()计算总金额

Google sheets 在Google Data Studio中同时使用SUM()和CASE()计算总金额,google-sheets,google-data-studio,Google Sheets,Google Data Studio,与Google Data Studio连接的Google Sheets文件示例: Fruit |Color |Period |Amount ------------------------------ Apple |Green |201901 |3 Apple |Green |201902 |5 Apple |Red |201812 |5 Apple |Red |201903 |4 Grape |Red |201902 |4 Grape |P

与Google Data Studio连接的Google Sheets文件示例:

Fruit   |Color  |Period |Amount
------------------------------
Apple   |Green  |201901 |3
Apple   |Green  |201902 |5
Apple   |Red    |201812 |5
Apple   |Red    |201903 |4

Grape   |Red    |201902 |4
Grape   |Purple |201902 |6
Grape   |Purple |201903 |1
例如,我想计算绿苹果和红苹果果实的总量

因此,公式应该是这样的:水果=“苹果”和颜色=“绿色”或颜色=“红色”的总和。在本例中,总金额应为17

我尝试在计算字段中使用此公式,但不幸的是,它不起作用:

SUM (Amount) WHERE (CASE WHEN Fruit = "Apple" AND Color = "Green" OR Color = "Red" ) then 1 else 0 END )
我得到一个语法错误。我怎样才能解决这个问题

=SUMPRODUCT(QUERY({A2:D}, 
 "select Col4 
  where Col1='Apple' 
    and (Col2='Red' or Col2='Green')"))

但这也适用于该示例:

=SUMPRODUCT(QUERY({A2:D}, 
 "select Col4 
  where Col1='Apple'"))

可以使用以下语句实现(可以选择所需的聚合,例如平均值、总和、计数等):

要演示,以及使用图表级公式显示以下过程的GIF:

CASE
  WHEN Fruit = "Apple" AND (Color = "Green" OR Color = "Red") THEN Amount
  ELSE NULL
END