【问题标题】:How can I calculate total amount using SUM () and CASE () together in Google Data Studio?如何在 Google Data Studio 中同时使用 SUM () 和 CASE () 计算总金额?
【发布时间】:2022-05-16 18:24:51
【问题描述】:

与 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

例如,我想计算绿色和红色苹果果实的总量。

所以公式应该是这样的:

SUM Amount WHERE Fruit = "Apple" AND Color = "Green" OR Color = "Red"

在本例中,总金额应为 17

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

SUM (Amount) WHERE (CASE WHEN Fruit = "Apple" AND Color = "Green" OR Color = "Red" ) then 1 else 0 END )

我收到语法错误。我该如何解决这个问题?

【问题讨论】:

    标签: google-sheets google-data-studio


    【解决方案1】:

    可以使用下面的CASE语句来实现(可以选择需要的聚合,例如Average、Sum、Count等):

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

    Google Data Studio Report 进行演示,以及使用图表级公式显示以下过程的 GIF:

    【讨论】:

      【解决方案2】:

      您可以在 Data Studio 图形上应用过滤器,选择您想要的水果和颜色!

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案3】:
      =SUMPRODUCT(QUERY({A2:D}, 
       "select Col4 
        where Col1='Apple' 
          and (Col2='Red' or Col2='Green')"))
      

      但这也适用于该示例:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        • 2022-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-21
        • 2014-12-19
        相关资源
        最近更新 更多