【问题标题】:How to count totals and uniques (based on 2 separate columns) using ArrayFormula in Google Sheets?如何在 Google 表格中使用 ArrayFormula 计算总数和唯一性(基于 2 个单独的列)?
【发布时间】:2020-05-15 17:02:48
【问题描述】:

我有以下电子表格:

https://docs.google.com/spreadsheets/d/1pBL21fyuxYNb5YfERsFl7Y-CTdQEH7SgJQfuOdQ0V6c/edit?usp=sharing

前 2 列计算操作的总数并对其进行排序。我想要做的也是使用相同的公式生成接下来的 2 列。第 3 列用于计算基于用户 ID 的唯一操作数,第 4 列用于计算基于用户名的唯一操作数。我可以做一个单独计算唯一性的公式,但理想情况下,我希望它与前 2 列一起使用。

非常感谢任何帮助。

【问题讨论】:

  • 你能分享一下这张表过去是如何用于历史目的的,现在它已经更新了数据。或者如果可能的话,截图。谢谢@darkhorse

标签: google-sheets google-sheets-formula array-formulas google-sheets-query gs-vlookup


【解决方案1】:

使用:

=ARRAYFORMULA(QUERY({F3:H, 
 IF((G3:G<>"")*(COUNTIFS(F3:F&G3:G, F3:F&G3:G, ROW(G3:G), "<="&ROW(G3:G))=1), F3:F&G3:G, ),
 IF((H3:H<>"")*(COUNTIFS(F3:F&H3:H, F3:F&H3:H, ROW(H3:H), "<="&ROW(H3:H))=1), F3:F&H3:H, )},
 "select Col1,count(Col1),count(Col4),count(Col5) 
  where Col1 !='' 
  group by Col1 
  order by count(Col1) desc 
  label count(Col1)'Total',
        count(Col4)'Unique (for id)',
        count(Col5)'Unique (for name)'", 1))

【讨论】:

  • 如果“Id”列在 H 列,“Name”列在 I 列怎么办?你会改变你的公式以确保它仍然正常工作?对不起,如果这是错误的地方,如果你愿意,我可以发布另一个问题。
  • 应该是:=ARRAYFORMULA(QUERY({F3:H, IF((H3:H&lt;&gt;"")*(COUNTIFS(F3:F&amp;H3:H, F3:F&amp;H3:H, ROW(H3:H), "&lt;="&amp;ROW(H3:H))=1), F3:F&amp;H3:H, ), IF((I3:I&lt;&gt;"")*(COUNTIFS(F3:F&amp;I3:I, F3:F&amp;I3:I, ROW(I3:I), "&lt;="&amp;ROW(I3:I))=1), F3:F&amp;I3:I, )}, "select Col1,count(Col1),count(Col4),count(Col5) where Col1 !='' group by Col1 order by count(Col1) desc label count(Col1)'Total', count(Col4)'Unique (for id)', count(Col5)'Unique (for name)'", 1))
  • 您更正的公式完美运行。我注意到的一件奇怪的事情是,如果动作不是字符串,那么公式就会停止工作。有没有办法将TOTEXT 合并到此公式中,以便始终将操作列转换为字符串?
  • 试试:=ARRAYFORMULA(QUERY(TO_TEXT({F3:H, IF((H3:H&lt;&gt;"")*(COUNTIFS(F3:F&amp;H3:H, F3:F&amp;H3:H, ROW(H3:H), "&lt;="&amp;ROW(H3:H))=1), F3:F&amp;H3:H, ), IF((I3:I&lt;&gt;"")*(COUNTIFS(F3:F&amp;I3:I, F3:F&amp;I3:I, ROW(I3:I), "&lt;="&amp;ROW(I3:I))=1), F3:F&amp;I3:I, )}), "select Col1,count(Col1),count(Col4),count(Col5) where Col1 !='' group by Col1 order by count(Col1) desc label count(Col1)'Total', count(Col4)'Unique (for id)', count(Col5)'Unique (for name)'", 1))
【解决方案2】:

在单元格 A10 或除 F、G 和 H 列之外的任何空闲单元格中尝试以下公式:

=arrayformula({query({F3:F,F3:F},"select Col1,count(Col2) where Col1<>'' group by Col1 label count(Col2) 'Total'",1),iferror(vlookup(query(query({F3:F,F3:F},"select Col1,count(Col2) where Col1<>'' group by Col1 label count(Col2) 'Total'",1),"select Col1"),query(query({F3:G,G3:G},"select Col1,Col2,count(Col3) where Col1<>'' group by Col1,Col2 label count(Col3) 'Total'",1),"select Col1,count(Col2) group by Col1 label count(Col2) 'Unique (for id)'",1),2,0)),iferror(vlookup(query(query({F3:F,F3:F},"select Col1,count(Col2) where Col1<>'' group by Col1 label count(Col2) 'Total'",1),"select Col1"),query(query({F3:F,H3:H,H3:H},"select Col1,Col2,count(Col3) where Col1<>'' group by Col1,Col2 label count(Col3) 'Total'",1),"select Col1,count(Col2) group by Col1 label count(Col2) 'Unique (for name)'",1),2,0))})

要获得排序(按第 2 列)的结果,请尝试:

=arrayformula({query({F3:F,F3:F},"select Col1,count(Col2) where Col1<>'' group by Col1 order by count(Col2) desc label count(Col2) 'Total'",1),iferror(vlookup(query(query({F3:F,F3:F},"select Col1,count(Col2) where Col1<>'' group by Col1 order by count(Col2) desc label count(Col2) 'Total'",1),"select Col1"),query(query({F3:G,G3:G},"select Col1,Col2,count(Col3) where Col1<>'' group by Col1,Col2 label count(Col3) 'Total'",1),"select Col1,count(Col2) group by Col1 label count(Col2) 'Unique (for id)'",1),2,0)),iferror(vlookup(query(query({F3:F,F3:F},"select Col1,count(Col2) where Col1<>'' group by Col1 order by count(Col2) desc label count(Col2) 'Total'",1),"select Col1"),query(query({F3:F,H3:H,H3:H},"select Col1,Col2,count(Col3) where Col1<>'' group by Col1,Col2 label count(Col3) 'Total'",1),"select Col1,count(Col2) group by Col1 label count(Col2) 'Unique (for name)'",1),2,0))})

【讨论】:

  • 这几乎可以完美运行,但它不会根据第二列的值(即总计)对列进行排序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-15
  • 1970-01-01
  • 2019-04-09
  • 2022-09-24
  • 2019-03-26
  • 2018-06-10
  • 2021-10-28
相关资源
最近更新 更多