簡體   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