使用 ARRAYFORMULA 從查找表中查找源表中的所有匹配行/值 (use ARRAYFORMULA to find all matching rows/values in a source table from a lookup table)


問題描述

使用 ARRAYFORMULA 從查找表中查找源表中的所有匹配行/值 (use ARRAYFORMULA to find all matching rows/values in a source table from a lookup table)

我有一個像這樣的源表。如您所見,每個 Lookup Value 可以有一個或多個 Result

| Lookup Value | Result |
|‑‑‑‑‑‑‑‑‑‑‑‑‑‑|‑‑‑‑‑‑‑‑|
| a            | a1     |
| a            | a2     |
| a            | a3     |
| b            | b1     |
| b            | b2     |
| c            | c1     |
| c            | c2     |

然後我有一個像這樣的輸入表:

| queries | results |         |         |         |         |         |
|‑‑‑‑‑‑‑‑‑|‑‑‑‑‑‑‑‑‑|‑‑‑‑‑‑‑‑‑|‑‑‑‑‑‑‑‑‑|‑‑‑‑‑‑‑‑‑|‑‑‑‑‑‑‑‑‑|‑‑‑‑‑‑‑‑‑|
| a       | ...     | ...     | ...     | ...     | ...     | ...     |
| c       | ...     | ...     | ...     | ...     | ...     | ...     |

每一行的 ... 應該是查找表中的轉置值。因此,例如,上表如下所示:

| queries | results |    |    |
|‑‑‑‑‑‑‑‑‑|‑‑‑‑‑‑‑‑‑|‑‑‑‑|‑‑‑‑|
| a       | a1      | a2 | a3 |
| c       | c1      | c2 |    |

現在我必須使用多個公式,如下所示:

在此處輸入圖片描述

我正在嘗試用單個 ARRAYFORMULA 替換它,但它沒有 t 似乎工作。

在此處輸入圖片描述

還有其他方法嗎?基本上是從查找表中查找所有匹配的行,然後轉置它們?


參考解法

方法 1:

Suppose your "Lookup Value" and "Result" data run from A1:B (with headers in A1 and B1). And suppose that your "queries" list is in D1:D (header in D1) with the "results" header in E1.

Depending on the maximum number of possible matches in B:B for any value in A:A, you could use this in E2:

=ArrayFormula(IFERROR(VLOOKUP(D2:D,QUERY(FILTER({A2:B,COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))},A2:A<>""),"Select Col1, MAX(Col2) Group By Col1 Pivot Col3"),SEQUENCE(1,10,2),0)))

If your maximum possible matches is fewer than 10 or more than 10, feel free to edit the second argument of the SEQUENCE function accordingly.

Understand that, with such an array formula that is asked to process a range, you wouldn't be able to put other data anywhere below or to the right of your "queries and results" that you've asked the array formula to assess or fill. So if you want data under it, you'll need to limit your VLOOKUP from D2:D to, say, D2:D50 (or whatever your max queries range would be). Likewise, if that second argument of the SEQUENCE function is 10, you'll have "reserved" 10 columns (i.e., E:N) for possible results, and you won't be able to put data there or you'll "break" the array formula. That being the case, you may want to give yourself some sort of visual line of demarcation around the area you've reserved for the formula's use (e.g., change the background color of the block or place a border around it, etc.).

方法 2:

try:

=ARRAYFORMULA(IFERROR(VLOOKUP(D:D, SPLIT(TRANSPOSE(QUERY(TRANSPOSE(IF(ISNUMBER(
 QUERY(QUERY(A:B, "select A,count(A) where A is not null group by A pivot B"), "offset 1", 0)), 
 QUERY(QUERY(A:B, "select A,count(A) where A is not null group by A pivot B"), "limit  0", 1), 
 QUERY(QUERY(A:B, "select A,count(A) where A is not null group by A pivot B"), "offset 1", 0)))
 ,,999^99)), " "), TRANSPOSE(ROW(INDIRECT("A2:A"&COUNTUNIQUE(B:B)))), 0)))

0

(by IMTheNachoManErik Tylerplayer0)

參考文件

  1. use ARRAYFORMULA to find all matching rows/values in a source table from a lookup table (CC BY‑SA 2.5/3.0/4.0)

#google-sheets-query #google-sheets #transpose #google-sheets-formula #array-formulas






相關問題

IMPORTRANGE 將多個 Google 表格導入一個垂直列? (IMPORTRANGE to import multiple Google Sheets into one vertical column?)

如何使用度量單位對類似項目進行分組和添加 (How to group and add similar items with a unit of measurement)

如何在 Google 表格中創建多個工作表引用的 INDIRECT 數組字符串? (How to create INDIRECT array string of multiple sheet references in Google Sheets?)

如何匯總 Google 表格中列中的前 5 個值 (How can I sum top 5 values from column in Google Sheets)

使用查詢和過濾但“無列:Col15”錯誤 (Use Query and filter But "No Column: Col15" error)

如何從 Google 表格中的字符串中刪除一組數字 (how to remove a set of digits from string in Google Sheets)

如何在 Google 表格中使用 ArrayFormula 計算總數和唯一性(基於 2 個單獨的列)? (How to count totals and uniques (based on 2 separate columns) using ArrayFormula in Google Sheets?)

從其他行的組合創建行 (Create rows from combination of other rows)

將一對多數據轉換為列 (Transform one to many data to columns)

希望從右到左計算零值,直到出現非零值 (Looking to count zero values from right to left until non-zero values appears)

使用 ImportHTML 或 ImportXML 在 Google 表格中選擇不連續的列、刪除行和添加排序列 (Using ImportHTML or ImportXML to Select Non-Consecutive Columns In Google Sheets, Remove Rows, & Add A Sort Column)

使用 ARRAYFORMULA 從查找表中查找源表中的所有匹配行/值 (use ARRAYFORMULA to find all matching rows/values in a source table from a lookup table)







留言討論