---
title: Table.AddJoinColumn – เพิ่มคอลัมน์ Join จากตารางอื่น
url: https://www.thepexcel.com/functions/power-query/table-functions/table-addjoincolumn/
type: function-explainer
program: Power Query
syntax: "= Table.AddJoinColumn(table1, key1, table2, key2, newColumnName)"
date: 2025-12-06
updated: 2025-12-25
scores:
  popularity: 5
  difficulty: 4
  usefulness: 6
---

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

> Table.AddJoinColumn เชื่อมต่อข้อมูลจากสองตารางตามคีย์ที่ตรงกัน โดยเก็บผลลัพธ์ในคอลัมน์ใหม่เป็นตารางแ

## คำอธิบาย

Table.AddJoinColumn เชื่อมต่อข้อมูลจากสองตารางตามคีย์ที่ตรงกัน โดยเก็บผลลัพธ์ในคอลัมน์ใหม่เป็นตารางแบบ nested แทนการขยายแนวนอน

## Syntax

```excel
= Table.AddJoinColumn(table1, key1, table2, key2, newColumnName)
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table1 | Yes | table |  | ตารางหลัก (primary table) ที่ต้องการเพิ่มคอลัมน์ join |
| key1 | Yes | text or list |  | ชื่อคอลัมน์ (หรือ list หลายคอลัมน์) ในตารางหลักที่ใช้เป็น key สำหรับ join |
| table2 | Yes | function |  | ฟังก์ชันที่ return ตารางที่ต้องการ join กับ (ต้องเขียนเป็น () => TableName) |
| key2 | Yes | text or list |  | ชื่อคอลัมน์ (หรือ list หลายคอลัมน์) ในตารางที่ join ที่ต้องการเทียบกับ key1 |
| newColumnName | Yes | text |  | ชื่อของคอลัมน์ใหม่ที่จะเก็บผลลัพธ์ join (เป็นตารางแบบ nested) |

## เคสการใช้งาน

### Custom Join implementation

Custom Join implementation

## ตัวอย่าง

### 1. Join ตารางขายกับตารางราคา

```excel
let
    Sales = Table.FromRecords({
        [SaleID = 1, Item = "Shirt"],
        [SaleID = 2, Item = "Hat"],
        [SaleID = 3, Item = "Shoes"]
    }),
    PriceData = Table.FromRecords({
        [SaleID = 1, Price = 200, Stock = 50],
        [SaleID = 2, Price = 150, Stock = 80],
        [SaleID = 1, Price = 180, Currency = "USD"]
    }),
    Result = Table.AddJoinColumn(Sales, "SaleID", () => PriceData, "SaleID", "PriceInfo")
in
    Result
```

**ผลลัพธ์:** `ตารางขาย 3 แถว + คอลัมน์ PriceInfo ใหม่ที่มีตารางแบบ nested`

ทำ left join Sales กับ PriceData ตาม SaleID โดยใส่ผลลัพธ์ลงในคอลัมน์ PriceInfo แบบ nested table ซึ่งแต่ละเซลล์จะมีตารางเล็กๆ ที่มีข้อมูล price ทั้งหมด match กับ row นั้นๆ

### 2. Join กับข้อมูลจาก Query อื่นใน Excel

```excel
let
    Orders = Source,
    CustomerInfo = () => Excel.CurrentWorkbook(){[Name="Customers"]}[Content],
    Result = Table.AddJoinColumn(Orders, "CustomerID", CustomerInfo, "CustomerID", "Customer")
in
    Result
```

**ผลลัพธ์:** `ตาราง Orders ที่มีคอลัมน์ Customer ใหม่เป็นตารางแบบ nested`

Join ตาราง Orders (query ปัจจุบัน) กับ Customers query ที่อยู่ใน Excel workbook เดียวกัน โดยเก็บข้อมูล Customer detail ไว้ในคอลัมน์ nested

### 3. Join หลายคอลัมน์ด้วย Composite Key

```excel
let
    Table1 = Table.FromRecords({
        [Year = 2024, Month = 1, Sales = 1000],
        [Year = 2024, Month = 2, Sales = 1500],
        [Year = 2025, Month = 1, Sales = 2000]
    }),
    Table2 = Table.FromRecords({
        [Year = 2024, Month = 1, Target = 1200],
        [Year = 2024, Month = 2, Target = 1400],
        [Year = 2025, Month = 1, Target = 2200]
    }),
    Result = Table.AddJoinColumn(Table1, {"Year", "Month"}, () => Table2, {"Year", "Month"}, "TargetInfo")
in
    Result
```

**ผลลัพธ์:** `ตาราง Table1 + คอลัมน์ TargetInfo ที่มีตาราง Target details`

Join ตั้งแต่ 2 คอลัมน์พร้อมกัน (Year และ Month) ต้องมีค่าทั้ง Year และ Month ตรงกันกับตารางที่ join ใช้ list {"Col1", "Col2", ...} เพื่อระบุ composite key

### 4. Join แล้ว Expand บางคอลัมน์เท่านั้น

```excel
let
    Orders = Table.FromRecords({
        [OrderID = 1, OrderDate = #date(2024,1,1)],
        [OrderID = 2, OrderDate = #date(2024,1,2)]
    }),
    OrderDetails = Table.FromRecords({
        [OrderID = 1, Product = "Chair", Qty = 2],
        [OrderID = 1, Product = "Desk", Qty = 1],
        [OrderID = 2, Product = "Lamp", Qty = 3]
    }),
    Joined = Table.AddJoinColumn(Orders, "OrderID", () => OrderDetails, "OrderID", "Details"),
    Expanded = Table.ExpandTableColumn(Joined, "Details", {"Product", "Qty"}, {"Product", "Qty"})
in
    Expanded
```

**ผลลัพธ์:** `ตาราง Orders ที่มีคอลัมน์ Product และ Qty ขยายออกมา โดยแถวเพิ่มเติมสำหรับแต่ละ Product`

แสดงวิธี combine Table.AddJoinColumn กับ Table.ExpandTableColumn เพื่อให้ได้ผลลัพธ์ flatten แต่เฉพาะคอลัมน์ที่ต้องการเท่านั้น อันนี้เป็นวิธีใจความที่ดีมากครับ

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

- ผมแนะนำให้ใช้ Table.AddJoinColumn ตอนแรก แล้วค่อย Table.ExpandTableColumn เพื่อ flatten คอลัมน์ที่ต้องการเท่านั้น วิธีนี้ให้ความยืดหยุ่นในการแก้ไขหลังๆ ดีกว่า

- สำหรับ performance ให้ใช้ primary key (unique) ใน key1 เพื่อให้การ join เร็วขึ้น ตัวอย่างเช่น ถ้า key1 เป็นคอลัมน์ที่ซ้ำกัน performance จะแย่ลง

- ถ้า join ต้องสร้างทีละหลายคอลัมน์ สามารถเรียก Table.AddJoinColumn ซ้ำได้หลายครั้ง เพิ่มคอลัมน์ nested ไปเรื่อยๆ

- ทดสอบด้วย Table.FromRecords หรือตัวอย่างข้อมูลเล็กๆ ก่อน เพื่อให้แน่ใจว่า key ตรงกัน และ nested table มีข้อมูลที่คาดหวัง ส่วนตัวผมเคยพลาดเพราะ key มี space หรือ case ต่างกัน

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

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

Table.Join จะ flatten ผลลัพธ์ออกมาเป็นแถวแยก (multiplication ของแถว) แต่ Table.AddJoinColumn จะเก็บผลลัพธ์ join เป็นตารางแบบ nested ในคอลัมน์เดียว ซึ่งให้ความยืดหยุ่นมากว่า แล้วค่อย expand บางคอลัมน์หรือทั้งหมดก็ได้

**Q: ทำไม table2 ต้องเป็น function แบบ () => ?**

เพราะ Power Query จะประมวลผล function นี้อย่างมีประสิทธิภาพ โดยรอจนกว่าจะประเมินค่า key ใน table1 เสร็จสิ้นก่อน หลังจากนั้นค่อยเรียกใช้ table2 ช่วยหลีกเลี่ยงการโหลดตารางทั้งหมดล่วงหน้า และช่วย performance ได้เยอะ ผมแนะนำให้เสมอเขียน () => TableName

**Q: ถ้าไม่มีค่า key ที่ match ใน table2 จะเป็นยังไง?**

จะได้ตารางว่าง (empty table) ในคอลัมน์ nested ของแถวนั้น เพราะเป็น LeftOuter join มีความหมายว่าแถวจาก table1 ยังอยู่แม้ว่าไม่มี match ใน table2 หากต้องการ inner join (เก็บเฉพาะที่มี match) ต้องใช้ Table.Join แทน

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

ได้ครับ ใช้ list {"Column1", "Column2", "Column3", ...} แทนชื่อคอลัมน์เดี่ยว เวลานี้ Power Query จะต้องมีค่าทั้งหมดตรงกัน (composite key)

**Q: เวลา join กับตารางที่มี 1 หลาย (1 to many) จะเป็นยังไง?**

ผลก็คือคอลัมน์ nested จะมีหลายแถว สำหรับแต่ละแถวจาก table1 ตัวอย่างเช่น order 1 อัน มี product 3 ชนิด คอลัมน์ nested จะเป็นตาราง 3 แถว ผมแนะนำให้ expand มาดูชัดๆ ว่าข้อมูลตรงกับที่คิดไหม

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

- [Microsoft Learn: Table.AddJoinColumn](https://learn.microsoft.com/en-us/powerquery-m/table-addjoincolumn) _(official)_
- [Microsoft Learn: Table.Join (สำหรับเปรียบเทียบ)](https://learn.microsoft.com/en-us/powerquery-m/table-join) _(official)_
- [Microsoft Learn: Table.ExpandTableColumn](https://learn.microsoft.com/en-us/powerquery-m/table-expandtablecolumn) _(official)_

---

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