问题描述
我的数据示例
如果我具有图片中所示的数据(实际数据具有相同的格式,但更大),我如何计算每个ID组合(例如Dinner-Pasta组合)出现多少次?理想情况下,我想在另一个标签中创建一个表格,以显示每个ID的所有可能组合的计数。
谢谢!
解决方法
尝试SUMPRODUCT
:
=SUMPRODUCT((I2=$A$2:$A$7)*(J2=$B$2:$F$7)*(K2=$C$2:$G$7))
,
-
突出显示整个内容,并插入-表
-
在表格功能区中,将表格名称更改为“ InputTable”
-
在数据功能区的获取和转换部分中,单击来自表。这将打开一个PowerQuery窗口。在PowerQuery窗口中:
-
创建新查询(单击主页-管理-引用 ...或主页-新来源-其他来源-空白查询 ...它并不重要,我们只想创建一个新查询,无论如何我们将在后续步骤中替换其内容)
-
将右侧栏中的名称更改为“ ffTableForDay”
-
点击首页-高级编辑器
-
插入以下代码:
// 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"
-
创建新查询
-
将查询名称更改为“ Meals”
-
点击首页-高级编辑器
-
插入以下代码:
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
-
创建新查询
-
将查询名称更改为“ ComboCount”
-
点击首页-高级编辑器
-
插入以下代码:
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"
-
点击首页-关闭并加载
-
如果您的查询选项设置为将查询加载到工作簿(默认),则根据需要删除“餐”选项卡。如果您的查询选项默认情况下不将查询加载到工作簿,则右键单击侧栏中的“ ComboCount”查询,然后单击“加载到...”
或者
一旦我们可以使用“ Meals”查询,就可以创建一个“ ComboCount”查询
- 将餐点加载到工作簿并完成数据透视表,或者
- 将膳食加载到数据模型并执行Power Pivot。