如何过滤 Google Sheet 中的数据,以便只包含每个名称的最后 20 个项目?

How to filter data in a Google Sheet so that just the last 20 items per name are included?

我有一个 google 传播sheet,它有超过 1000 行并且每周更新一次,每行包含名称并且做出了 3 个决定之一。我想使其自动化,并在单独的 sheet 中有一个 table,其中包含名称和计数决策,每个名称仅最后 20 个。

想要的结果应该是这样的 this screenshot.

我是 JavaScript 的初学者,非常欢迎任何有关如何使用 App Script 执行此操作的想法或结构。

您可以在单元格 Sheet1!AF1:

中使用此公式按代理创建降序
=arrayformula( 
  { 
    "Descending ordinal by agent"; 
    iferror( 1 / 
      countifs( 
        E2:E, E2:E, 
        row(E2:E), ">=" & row(E2:E) 
      ) 
    ^ -1 ) 
  } 
)

然后 插入 > Sheet 并将此公式插入新 sheet 的单元格 A1:

=query( 
  Sheet1!A1:AF, 
  "select E, count(E) 
   where AD is not null 
   and AF <= 20 
   group by E 
   pivot AD", 
  1 
)

该公式将按名称和决定给出计数,仅查看序号为 20 或更少的行。在您的示例传播sheet.

中查看新的 Solution sheet

尝试:

=INDEX({QUERY({Sheet1!E2:E, Sheet1!AD2:AD, 
 COUNTIFS(Sheet1!E2:E, Sheet1!E2:E, ROW(Sheet1!E2:E), ">="&ROW(Sheet1!E2:E))}, 
 "select Col1,count(Col1) where Col1 is not null and Col3 <= 20 
  group by Col1 pivot Col2 label Col1'Name'"), 
 {"Total"; MMULT(QUERY(QUERY({Sheet1!E2:E, Sheet1!AD2:AD, 
 COUNTIFS(Sheet1!E2:E, Sheet1!E2:E, ROW(Sheet1!E2:E), ">="&ROW(Sheet1!E2:E))}, 
 "select count(Col1) where Col1 is not null and Col3 <= 20 group by Col1 
  pivot Col2"), "offset 1", 0)*1, {1;1;1})}})