使用转置公式谷歌表格对许多行求和



我正在尝试构建一个公式,该公式将计算日期范围内发生的类总数。这是我可以使用的公式,但它需要包含数百行(即从 B2 到 B500 左右的"类计数"(。有没有办法把它变成一个数组,这样我就不必有一个页面和页面长的公式?

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

"所有数据"列 A 包含课程的日期,B 列包含班级名称(每个学生重复,但只能计算一次(。 "类计数"列 B 包含唯一类名的列表。 "所有总计"单元格 N4 和 N5 包含月份开始检查之间。

目标是当且仅当一个类落在"所有总计"上的 N4 和 N5 指定的数据范围内时,对类的每次出现进行一次计数。唯一的问题是,在很多年里,最终会有数百个班级。

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

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

我尝试修复的公式位于"导入"选项卡上。

编辑: 我意识到这可能没有必要的"转置"方面,所以我将公式编辑了一点,但仍然无法在所有"类计数"类名(B 列(中自动求和。

=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

谢谢!

尝试:

=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)''"))

正则表达式括号修复:

=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)''")))

最新更新