---
title: Table.AddMatchColumn – เพิ่มคอลัมน์จากการแมตช์ตารางอื่น
url: https://www.thepexcel.com/functions/power-query/table-functions/table-addmatchcolumn/
type: function-explainer
program: Power Query
syntax: "Table.AddMatchColumn(table as table, newColumnName as text, tableColumnPairs as list, optional joinKind as JoinKind) as table"
date: 2025-12-12
updated: 2025-12-25
scores:
  popularity: 5
  difficulty: 4
  usefulness: 6
---

# Table.AddMatchColumn – เพิ่มคอลัมน์จากการแมตช์ตารางอื่น

> Table.AddMatchColumn เพิ่มคอลัมน์ใหม่ที่มีค่าจากการแมตช์แถวในตารางอื่น โดยค้นหาค่าที่ตรงกับคีย์ เหมื

## คำอธิบาย

Table.AddMatchColumn เพิ่มคอลัมน์ใหม่ที่มีค่าจากการแมตช์แถวในตารางอื่น โดยค้นหาค่าที่ตรงกับคีย์ เหมือน VLOOKUP แต่ทำงานโดยตรงบนตารางทั้งหมด

## Syntax

```excel
Table.AddMatchColumn(table as table, newColumnName as text, tableColumnPairs as list, optional joinKind as JoinKind) as table
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table | Yes | table |  | ตารางที่ต้องการเพิ่มคอลัมน์ใหม่ |
| newColumnName | Yes | text |  | ชื่อของคอลัมน์ใหม่ที่จะสร้าง |
| tableColumnPairs | Yes | list |  | รายการคู่ตารางและคอลัมน์สำหรับการแมตช์ โครงสร้าง: {ตารางที่ต้องการหา, {{"คอลัมน์จากตารางแรก", "คอลัมน์จากตารางที่สอง"}}} |
| joinKind | No | JoinKind | JoinKind.LeftOuter | ประเภทของการเข้าร่วม: JoinKind.LeftOuter (ค่าเริ่มต้น), JoinKind.RightOuter, JoinKind.Inner, JoinKind.FullOuter |

## ตัวอย่าง

### 1. ตัวอย่างพื้นฐาน - เพิ่มชื่อลูกค้าจากตารางอื่น

```excel
let
    OrdersTable = Table.FromRecords({
        [OrderID = 1, CustomerID = 101, Amount = 500],
        [OrderID = 2, CustomerID = 102, Amount = 750],
        [OrderID = 3, CustomerID = 101, Amount = 1200]
    }),
    CustomersTable = Table.FromRecords({
        [CustomerID = 101, CustomerName = "Alice"],
        [CustomerID = 102, CustomerName = "Bob"]
    }),
    Result = Table.AddMatchColumn(
        OrdersTable,
        "CustomerName",
        {CustomersTable, {{"CustomerID", "CustomerID"}}}
    )
in
    Result
```

**ผลลัพธ์:** `OrdersTable ขยายเพิ่มคอลัมน์ CustomerName ที่มีค่า "Alice", "Bob", "Alice"`

เพิ่มชื่อลูกค้า (CustomerName) เข้าไปในตาราง OrdersTable โดยการแมตช์ CustomerID จากทั้งสองตาราง เมื่อหา CustomerID = 101 ในตาราง Customers จะได้ชื่อ "Alice" และใส่เข้าไปในแถวที่ OrderID = 1 และ 3

### 2. ตัวอย่าง - ใช้ JoinKind.Inner เพื่อเก็บเฉพาะที่แมตช์

```excel
let
    SalesTable = Table.FromRecords({
        [SaleID = 1, ProductID = "A001", Qty = 5],
        [SaleID = 2, ProductID = "B002", Qty = 3],
        [SaleID = 3, ProductID = "C999", Qty = 2]
    }),
    ProductsTable = Table.FromRecords({
        [ProductID = "A001", ProductName = "Widget A"],
        [ProductID = "B002", ProductName = "Widget B"]
    }),
    Result = Table.AddMatchColumn(
        SalesTable,
        "ProductName",
        {ProductsTable, {{"ProductID", "ProductID"}}},
        JoinKind.Inner
    )
in
    Result
```

**ผลลัพธ์:** `ตารางที่มีเฉพาะแถว SaleID = 1 และ 2 เพราะมีการแมตช์ในตาราง Products แถว SaleID = 3 (ProductID = "C999") ถูกลบออกเพราะไม่พบใน Products`

JoinKind.Inner ทำให้เหลือเฉพาะแถวที่มีการแมตช์ในทั้งสองตาราง ใช้เวลาต้องการ data quality หรือลบ orphan records ออก

### 3. ตัวอย่าง - แมตช์หลายคอลัมน์ (Composite Key)

```excel
let
    OrdersTable = Table.FromRecords({
        [OrderID = 1, CustomerID = 101, Year = 2024],
        [OrderID = 2, CustomerID = 102, Year = 2024],
        [OrderID = 3, CustomerID = 101, Year = 2023]
    }),
    DiscountTable = Table.FromRecords({
        [CustomerID = 101, Year = 2024, Discount = 0.10],
        [CustomerID = 102, Year = 2024, Discount = 0.05],
        [CustomerID = 101, Year = 2023, Discount = 0.0]
    }),
    Result = Table.AddMatchColumn(
        OrdersTable,
        "Discount",
        {DiscountTable, {{"CustomerID", "CustomerID"}, {"Year", "Year"}}}
    )
in
    Result
```

**ผลลัพธ์:** `OrdersTable ขยายเพิ่มคอลัมน์ Discount ที่ตรงกับการแมตช์ทั้ง CustomerID และ Year`

เมื่อต้องการแมตช์โดยใช้หลายคอลัมน์เป็นเกณฑ์ (composite key) ให้ระบุหลายคู่ใน tableColumnPairs ในตัวอย่างนี้ OrderID = 1 จะได้ Discount = 0.10 เพราะตรงกับ CustomerID = 101 AND Year = 2024

### 4. ตัวอย่าง - ใช้ JoinKind.LeftOuter เพื่อเก็บแถวทั้งหมด

```excel
let
    EmployeesTable = Table.FromRecords({
        [EmpID = 1, EmpName = "John", DeptID = 10],
        [EmpID = 2, EmpName = "Jane", DeptID = 20],
        [EmpID = 3, EmpName = "Bob", DeptID = 99]
    }),
    DepartmentsTable = Table.FromRecords({
        [DeptID = 10, DeptName = "Sales"],
        [DeptID = 20, DeptName = "IT"]
    }),
    Result = Table.AddMatchColumn(
        EmployeesTable,
        "DeptName",
        {DepartmentsTable, {{"DeptID", "DeptID"}}},
        JoinKind.LeftOuter
    )
in
    Result
```

**ผลลัพธ์:** `แถวทั้งสามถูกเก็บไว้ แถว EmpID = 1 และ 2 มี DeptName ส่วน EmpID = 3 มี DeptName = null`

JoinKind.LeftOuter (ค่าเริ่มต้น) ทำให้เก็บแถวทั้งหมดจากตารางแรก และถ้าไม่พบการแมตช์จะใส่ null ลงไป ใช้กรณีที่อยากเห็นข้อมูลที่ไม่มีตัวจับ (orphan records)

## หมายเหตุเพิ่มเติม

- ผมแนะนำให้ใช้ Table.AddMatchColumn เมื่อแน่ใจว่า key column มีค่าไม่ซ้ำกัน (unique) ในตารางที่ต้องการหา ถ้าไม่ unique ผลลัพธ์อาจจะ unexpected

- ระบุ JoinKind อย่างชัดเจนเพื่อควบคุมว่าจะทำอย่างไรกับแถวที่ไม่มีการแมตช์ ผมชอบใช้ Inner เพื่อ clean data

- ถ้าข้อมูลมาจากหลายแหล่ง (API, CSV, Database) ให้ตรวจสอบ data type ก่อน บ่อยครั้งที่ CustomerID เป็น text ในตารางหนึ่ง แต่เป็น number ในอีกตารางหนึ่ง จะแมตช์ไม่ได้

- ส่วนตัวผมต้องลองใช้กับข้อมูลตัวอย่างก่อนที่จะใช้จริง ถ้า null หรือแถวหายไปไม่ระวัง

## คำถามที่พบบ่อย

**Q: Table.AddMatchColumn ต่างจาก Table.AddJoinColumn อย่างไร?**

ผม้บอกว่า ต่างกันนะครับ 😅 Table.AddJoinColumn ให้ผลลัพธ์ที่ซ้อนกัน (nested record/table) ในคอลัมน์เดียว คุณต้องขยาย (expand) มันหลังจากนั้น ส่วน Table.AddMatchColumn หรือบางครั้งก็ใช้ชื่อว่า Table.AddMergeColumn นั้น ให้เพิ่มคอลัมน์ตรง ๆ ที่เลือกจากตารางอื่น ทำให้ไม่ต้องขยายเพิ่มเติม

**Q: ถ้าไม่พบการแมตช์ สิ่งที่จะเกิดขึ้นคืออะไร?**

มันขึ้นอยู่กับ JoinKind ที่คุณระบุครับ ถ้าใช้ LeftOuter (ค่าเริ่มต้น) แถวเดิมจะยังคงอยู่พร้อมค่า null ในคอลัมน์ใหม่ ถ้าใช้ Inner จะลบแถวนั้นออก ผมแนะนำให้ตรวจสอบว่า key column มีค่าที่ไม่ซ้ำกันหรือมีความเหมาะสม ก่อนรัน

**Q: สามารถแมตช์ตามหลายคอลัมน์พร้อมกันได้ไหม?**

ได้แน่นอนครับ! นั่นคือสิ่งที่เรียกว่า composite key เพียงแค่ระบุหลายคู่ใน tableColumnPairs เช่น {{"Key1", "Col1"}, {"Key2", "Col2"}}

**Q: แล้ว Table.AddMatchColumn กับ Table.Join ต่างกันอย่างไร?**

ผม้บอกว่า ต้องดูว่าคุณต้องการอะไรครับ Table.Join ทำให้ได้ผลลัพธ์ที่ขยายออกมา คล้ายกับ SQL INNER JOIN หรือ LEFT JOIN ส่วน Table.AddMatchColumn คือเพิ่มคอลัมน์เข้าไปในตารางเดิม มันดูง่ายกว่า และผมชอบใช้มากขึ้นเพราะโค้ดชัดเจน

**Q: สามารถใช้กับการหาค่าจากคำของข้อมูล (not exact match) ได้ไหม?**

ต่างกับ Table.Join ครับ Table.AddMatchColumn ใช้ exact match เท่านั้น ถ้าต้องการ fuzzy match หรือ LIKE ต้องใช้ Table.AddColumn ร่วมกับ List.Find หรือ Table.SelectRows แต่นั่นจะช้ากว่า

## แหล่งข้อมูลเพิ่มเติม

- [Microsoft Learn: Table.AddMatchColumn](https://learn.microsoft.com/en-us/powerquery-m/table-addmatchcolumn) _(official)_
- [Microsoft Learn: Table Functions](https://learn.microsoft.com/en-us/powerquery-m/table-functions) _(official)_
- [Microsoft Learn: Power Query M Language Reference](https://learn.microsoft.com/en-us/powerquery-m/) _(official)_

---

_Source: [https://www.thepexcel.com/functions/power-query/table-functions/table-addmatchcolumn/](https://www.thepexcel.com/functions/power-query/table-functions/table-addmatchcolumn/)_
