繁体   English   中英

Excel:如何计算多列单元格的组合?

[英]Excel: how to count combinations of cells over multiple columns?

我的数据示例

如果我有如图所示的数据(真实数据具有相同的形式但要大得多),我将如何计算每个 ID 出现特定组合的次数,例如晚餐 - 意大利面的组合? 理想情况下,我想在另一个选项卡中制作一个表格,显示每个 ID 所有可能组合的计数。

提前致谢!

试试SUMPRODUCT

=SUMPRODUCT((I2=$A$2:$A$7)*(J2=$B$2:$F$7)*(K2=$C$2:$G$7))

在此处输入图像描述

  1. 突出显示您的整个并插入 - 表格

  2. 在表格功能区中,将表格名称更改为“InputTable”

  3. 在“数据”功能区的“获取和转换”部分中,单击“从表” 这将调出 PowerQuery window。在 PowerQuery window 中:

  4. 创建一个新查询(单击主页 - 管理 - 参考...或主页 - 新来源 - 其他来源 - 空白查询...这并不重要,我们只想创建一个新查询,我们将无论如何在接下来的步骤中替换它的内容)

  5. 将(右侧栏)中的名称更改为“ffTableForDay”

  6. 单击主页 - 高级编辑器

  7. 插入以下代码:

// Called "ffTable*" because it's a Function that returns a Function that returns a Table.
// Returns a function specific to a table that takes a day.
// Returned function takes a day and returns a table of meals for that day.
(table as table) as function => (day as text) as table =>
let
    #"Type Column Name" = day & "_type",
    #"Food Column Name" = day & "_Food",
    #"Removed Other Columns" = Table.SelectColumns(table,{"ID", #"Type Column Name", #"Food Column Name"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Other Columns",{{#"Type Column Name", "Type"}, {#"Food Column Name", "Food"}}),
    #"Removed Blank Rows" = Table.SelectRows(#"Renamed Columns", each [Type] <> null and [Type] <> "" and [Food] <> null and [Food] <> ""),
    #"Add Day" = Table.AddColumn(#"Removed Blank Rows", "Day", each day, type text)
in
    #"Add Day"
  1. 创建新查询

  2. 将查询名称更改为“Meals”

  3. 单击主页 - 高级编辑器

  4. 插入以下代码:

     let Source = InputTable, Days = {"Monday", "Tuesday", "Wednesday"}, #"Function Per Day" = ffTableForDay(Source), // get list of tables per call to ffTableForDay(InputTable)(day) #"Table Per Day" = List.Transform(Days, #"Function Per Day"), Result = Table.Combine(#"Table Per Day") in Result
  5. 创建新查询

  6. 将查询名称更改为“ComboCount”

  7. 单击主页 - 高级编辑器

  8. 插入以下代码:

     let Source = Meals, // Created by clicking **Transform - Group By** and then, in the dialog box, clicking advanced and grouping by Food and Type #"Grouped Rows" = Table.Group(Source, {"Type", "Food"}, {{"Count", each Table.RowCount(_), type number}}) in #"Grouped Rows"
  9. 单击主页 - 关闭并加载

  10. 如果您的查询选项设置为将查询加载到工作簿(默认),然后删除“膳食”选项卡,如果您愿意的话。 如果您的查询选项默认情况下不将查询加载到工作簿,则右键单击侧栏中的“ComboCount”查询,然后单击“加载到...”

或者

一旦我们让“Meals”查询工作,而不是创建一个“ComboCount”查询,我们可以有

  • 将 Meals 加载到工作簿并完成 pivot 表,或
  • 将 Meals 加载到数据 model 并完成 Power Pivot。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM