用转置公式谷歌表跨多行求和

时间:2020-05-23 13:36:40

标签: arrays google-sheets google-sheets-formula google-sheets-query google-query-language

我正在尝试建立一个公式,该公式将计算日期范围内发生的类的总数。这是我可以使用的公式,但它需要包含数百行(即从B2到B500左右的“类计数”)。有什么办法可以将其转换为数组,这样我就不必拥有一页和一页长的公式了?

=countifs(unique(filter('All Data'!$A$2:$A,'All Data'!$B$2:$B='Class Counts'!$B2)),">="&'All Totals'!$N4,unique(filter('All Data'!$A$2:$A,'All Data'!$B$2:$B='Class Counts'!$B2)),"<"&'All Totals'!$N5)
+countifs(unique(filter('All Data'!$A$2:$A,'All Data'!$B$2:$B='Class Counts'!$B3)),">="&'All Totals'!$N4,unique(filter('All Data'!$A$2:$A,'All Data'!$B$2:$B='Class Counts'!$B3)),"<"&'All Totals'!$N5)
+countifs(unique(filter('All Data'!$A$2:$A,'All Data'!$B$2:$B='Class Counts'!$B4)),">="&'All Totals'!$N4,unique(filter('All Data'!$A$2:$A,'All Data'!$B$2:$B='Class Counts'!$B4)),"<"&'All Totals'!$N5)
+ ... etc

“所有数据”列A包含班级的日期,列B包含班级名称(对每个学生重复此名称,但只能计数一次)。 “类别计数”列B包含唯一类别名称的列表。 “总计”单元格N4和N5包含开始检查的月份。

目标是当且仅当一个类的每次出现均在'All Totals'的N4和N5指定的数据范围内时,才对其进行一次计数。唯一的问题是,多年来最终将有数百个课程。

我的想法是将其转换为数组公式,并在整个范围内计数,但是我的所有尝试均返回0。

我无法分享实际的工作表,因为其中包含个人信息,但是我在此处创建了一个测试版本: https://docs.google.com/spreadsheets/d/1Nf0f5Bnuwe0-dnH6zHILGntdv2JDywFOTvmTjteXVLQ/edit?usp=sharing

我要修正的公式位于“导入”选项卡上。

编辑: 我意识到可能不需要这样做的“移调”方面,因此我对此进行了一些编辑,但是仍然无法自动汇总所有“类计数”的类名(列B)。

{{1}}

谢谢!

1 个答案:

答案 0 :(得分:2)

尝试:

=QUERY('All Data'!A2:B, 
 "select B,count(B) 
  where A >= date '"&TEXT('All Totals'!N4, "yyyy-mm-dd")&"' 
    and A <  date '"&TEXT('All Totals'!N5, "yyyy-mm-dd")&"'
  group by B 
  label count(B)''")

如果只希望特定的类,请尝试:

=QUERY('All Data'!A2:B, 
 "select B,count(B) 
  where A >= date '"&TEXT('All Totals'!N4, "yyyy-mm-dd")&"' 
    and A <  date '"&TEXT('All Totals'!N5, "yyyy-mm-dd")&"'
    and B matches '"&TEXTJOIN("|", 1, 'Class Counts'!B2:B)&"' 
  group by B 
  label count(B)''")

并仅返回其总和:

=SUM(QUERY('All Data'!A2:B, 
 "select count(B) 
  where A >= date '"&TEXT('All Totals'!N4, "yyyy-mm-dd")&"' 
    and A <  date '"&TEXT('All Totals'!N5, "yyyy-mm-dd")&"'
    and B matches '"&TEXTJOIN("|", 1, 'Class Counts'!B2:B)&"' 
  group by B 
  label count(B)''"))

regex括号修复:

=INDEX(SUM(QUERY(UNIQUE('All Data'!A2:B), 
 "select count(Col2)
  where Col1 >= date '"&TEXT('All Totals'!N4, "yyyy-mm-dd")&"' 
    and Col1 <  date '"&TEXT('All Totals'!N5, "yyyy-mm-dd")&"' 
    and Col2 matches '"&
 SUBSTITUTE(SUBSTITUTE(TEXTJOIN("|", 1, 'Class Counts'!B2:B), "(", "\("), ")", "\)")&"' 
  group by Col2
  label count(Col2)''")))