Reputation: 1
Example of my data
If I have the data as shown in the picture (real data has same form but is much larger), how would I count how many times a certain combination, for example the combination Dinner - Pasta, occurs per ID? Ideally I would like to make a table in another tab showing per ID the count for all possible combinations.
Thanks in advance!
Upvotes: 0
Views: 743
Reputation: 2497
Highlight your entire and Insert - Table
In the table ribbon, change your table name to "InputTable"
In the Get & Transform section of the Data ribbon, click From Table. This will bring up a PowerQuery window. In the PowerQuery window:
Create a new query (Click either Home - Manage - Reference... or Home - New Sources - Other Sources - Blank Query... it doesn't really matter, we just want to create a new query and we're going to replace its contents in the next steps anyway)
Change the name in the (right sidebar) to "ffTableForDay"
Click Home - Advanced Editor
Insert the following code:
// 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"
Create a new query
Change the query name to "Meals"
Click Home - Advanced Editor
Insert the following code:
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
Create a new query
Change the query name to "ComboCount"
Click Home - Advanced Editor
Insert the following code:
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"
Click Home - Close & Load
If your query options were set to load queries to the workbook (default), then delete the "Meals" tab, if you wish. If your query options were to NOT load queries to the workbook by default then right-click on the "ComboCount" query in the side-bar and click "Load To..."
Once we have the "Meals" query working, instead of creating a "ComboCount" query, we could have
Upvotes: 1
Reputation: 11968
Try SUMPRODUCT
:
=SUMPRODUCT((I2=$A$2:$A$7)*(J2=$B$2:$F$7)*(K2=$C$2:$G$7))
Upvotes: 1