简体   繁体   中英

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

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!

Try SUMPRODUCT :

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

在此处输入图像描述

  1. Highlight your entire and Insert - Table

  2. In the table ribbon, change your table name to "InputTable"

  3. In the Get & Transform section of the Data ribbon, click From Table . This will bring up a PowerQuery window. In the PowerQuery window:

  4. 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)

  5. Change the name in the (right sidebar) to "ffTableForDay"

  6. Click Home - Advanced Editor

  7. 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"
  1. Create a new query

  2. Change the query name to "Meals"

  3. Click Home - Advanced Editor

  4. 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
  5. Create a new query

  6. Change the query name to "ComboCount"

  7. Click Home - Advanced Editor

  8. 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"
  9. Click Home - Close & Load

  10. 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..."

Alternatively

Once we have the "Meals" query working, instead of creating a "ComboCount" query, we could have

  • loaded Meals to the workbook and done a pivot table, or
  • loaded Meals to the data model and done a Power Pivot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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