---
title: Table.NestedJoin – รวมตารางพร้อมสร้างคอลัมน์ตารางย่อย
url: https://www.thepexcel.com/functions/power-query/table-functions/table-nestedjoin/
type: function-explainer
program: Power Query
syntax: "Table.NestedJoin(table1 as table, key1 as any, table2 as any, key2 as any, newColumnName as text, optional joinKind as nullable number, optional keyEqualityComparers as nullable list) as table"
date: 2025-12-03
updated: 2025-12-20
scores:
  popularity: 8
  difficulty: 5
  usefulness: 9
---

# Table.NestedJoin – รวมตารางพร้อมสร้างคอลัมน์ตารางย่อย

> เชื่อมตารางสองตารางและสร้างคอลัมน์ตารางย่อย

## คำอธิบาย

Table.NestedJoin เชื่อมตารางสองตารางด้วยคีย์ที่กำหนด และสร้างคอลัมน์ใหม่ที่เก็บตารางย่อยของแถวที่จับคู่ได้ เหมาะกับงาน Merge ที่ต้องการเก็บรายละเอียดฝั่งขวาไว้เป็น nested table ก่อนจะ Expand หรือทำขั้นตอนต่อ

## Syntax

```excel
Table.NestedJoin(table1 as table, key1 as any, table2 as any, key2 as any, newColumnName as text, optional joinKind as nullable number, optional keyEqualityComparers as nullable list) as table
```

**Variant**

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

รูปแบบพื้นฐาน ใช้ joinKind ค่าเริ่มต้นเป็น JoinKind.LeftOuter

**Variant**

```excel
Table.NestedJoin(table1, key1, table2, key2, newColumnName, JoinKind.Inner)
```

กำหนด joinKind เพื่อเลือกชนิดการเชื่อม เช่น Inner, LeftOuter, FullOuter

**Variant**

```excel
Table.NestedJoin(table1, key1, table2, key2, newColumnName, joinKind, keyEqualityComparers)
```

กำหนดตัวเปรียบเทียบคีย์ เช่น case-insensitive โดยส่งเป็น list ของ comparer

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table1 | Yes | table |  | ตารางหลัก (ฝั่งซ้าย) ที่ต้องการคงแถวไว้ตามชนิดการ join |
| key1 | Yes | any |  | คอลัมน์คีย์ของ table1 ระบุเป็นชื่อคอลัมน์หรือ list ของชื่อคอลัมน์ |
| table2 | Yes | any |  | ตารางที่ต้องการเชื่อม (ฝั่งขวา) ซึ่งจะถูกจับคู่แล้วเก็บเป็นตารางย่อย |
| key2 | Yes | any |  | คอลัมน์คีย์ของ table2 ให้ตรงกับ key1 (จำนวนคอลัมน์ต้องเท่ากัน) |
| newColumnName | Yes | text |  | ชื่อคอลัมน์ใหม่ที่จะเก็บตารางย่อยของข้อมูลที่เชื่อมได้ |
| joinKind | No | nullable number | JoinKind.LeftOuter | ชนิดการเชื่อม เช่น JoinKind.LeftOuter, JoinKind.Inner, JoinKind.FullOuter ค่าเริ่มต้นคือ LeftOuter |
| keyEqualityComparers | No | nullable list | null | รายการ comparer สำหรับเทียบคีย์แต่ละคอลัมน์ ใช้เฉพาะกรณีพิเศษ เช่น case-insensitive |

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

### Merge ตารางอ้างอิงเพื่อเติมรายละเอียด

เชื่อมตารางธุรกรรมกับตารางอ้างอิง (เช่น สินค้า ลูกค้า) แล้วค่อย Expand เพื่อดึงข้อมูลเพิ่ม

_เหมาะกับ:_ lookup-merge

### เก็บข้อมูลฝั่งขวาเป็นตารางย่อย

เหมาะเมื่ออยากเก็บหลายรายการที่จับคู่กันไว้ในคอลัมน์เดียวก่อน แล้วค่อยเลือกวิธีจัดการภายหลัง

_เหมาะกับ:_ nested-table

### ควบคุมชนิดการเชื่อมอย่างชัดเจน

กำหนด joinKind เพื่อเลือกผลลัพธ์แบบ Inner/Left/Right/Full ให้ตรงกับโจทย์

_เหมาะกับ:_ join-kind-control

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: Left Outer Join (ค่าเริ่มต้น)

```excel
let
    Calls = Table.FromRecords({
        [CustomerToCall = 1],
        [CustomerToCall = 3]
    }),
    Customers = Table.FromRecords({
        [CustomerID = 1, Name = "Bob", Phone = "123-4567"],
        [CustomerID = 2, Name = "Jim", Phone = "987-6543"],
        [CustomerID = 3, Name = "Paul", Phone = "543-7890"]
    }),
    Joined = Table.NestedJoin(
        Calls,
        {"CustomerToCall"},
        Customers,
        {"CustomerID"},
        "CustomerDetails"
    )
in
    Joined
```

**ผลลัพธ์:** `Table ที่มี CustomerToCall และคอลัมน์ CustomerDetails เป็นตารางย่อยของข้อมูลที่จับคู่ได้`

ไม่ระบุ joinKind จะใช้ LeftOuter อัตโนมัติ ทำให้ทุกแถวของ Calls ถูกเก็บไว้ และ CustomerDetails มีข้อมูลเฉพาะแถวที่จับคู่ได้

### 2. ตัวอย่างที่ 2: Inner Join

```excel
let
    Orders = Table.FromRecords({
        [OrderID = 1, CustomerID = 10],
        [OrderID = 2, CustomerID = 20]
    }),
    Customers = Table.FromRecords({
        [CustomerID = 10, Name = "Ann"],
        [CustomerID = 30, Name = "Ken"]
    }),
    Joined = Table.NestedJoin(
        Orders,
        {"CustomerID"},
        Customers,
        {"CustomerID"},
        "Customer",
        JoinKind.Inner
    )
in
    Joined
```

**ผลลัพธ์:** `Table เหลือเฉพาะ OrderID=1 ที่มี CustomerID ตรงกัน`

Inner Join จะเก็บเฉพาะแถวที่จับคู่ได้ทั้งสองฝั่ง ทำให้ OrderID=2 ถูกตัดออก

### 3. ตัวอย่างที่ 3: Join ด้วยหลายคอลัมน์

```excel
let
    Sales = Table.FromRecords({
        [Region = "North", StoreID = 1, Amount = 100],
        [Region = "South", StoreID = 2, Amount = 200]
    }),
    StoreInfo = Table.FromRecords({
        [Region = "North", StoreID = 1, Manager = "A"],
        [Region = "North", StoreID = 2, Manager = "B"]
    }),
    Joined = Table.NestedJoin(
        Sales,
        {"Region", "StoreID"},
        StoreInfo,
        {"Region", "StoreID"},
        "StoreDetail",
        JoinKind.LeftOuter
    )
in
    Joined
```

**ผลลัพธ์:** `Table ที่ได้ StoreDetail เป็นตารางย่อยเฉพาะแถวที่ Region และ StoreID ตรงกัน`

การส่งคีย์เป็น list ช่วยให้ join หลายคอลัมน์ได้แม่นยำขึ้น ป้องกันการจับคู่ผิด

### 4. ตัวอย่างที่ 4: เทียบคีย์แบบไม่สนตัวพิมพ์ใหญ่เล็ก

```excel
let
    Left = Table.FromRecords({
        [Code = "ab-01"],
        [Code = "CD-02"]
    }),
    Right = Table.FromRecords({
        [Code = "AB-01", Value = 10],
        [Code = "cd-02", Value = 20]
    }),
    Joined = Table.NestedJoin(
        Left,
        {"Code"},
        Right,
        {"Code"},
        "Match",
        JoinKind.LeftOuter,
        {Comparer.OrdinalIgnoreCase}
    )
in
    Joined
```

**ผลลัพธ์:** `Table ที่จับคู่ Code ได้แม้ตัวพิมพ์ใหญ่เล็กไม่ตรงกัน`

กำหนด keyEqualityComparers เป็น list ของ Comparer เพื่อควบคุมการเทียบคีย์ เช่น case-insensitive ใช้เฉพาะกรณีพิเศษเท่านั้น

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

ผลลัพธ์ของ Table.NestedJoin คือคอลัมน์ตารางย่อย ต้อง Expand ถ้าต้องการคอลัมน์แบบแบน
joinKind กำหนดชนิดการเชื่อม เช่น Inner, LeftOuter, RightOuter, FullOuter
keyEqualityComparers ใช้กำหนดวิธีเทียบคีย์แบบพิเศษ เช่น case-insensitive

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

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

Table.NestedJoin คืนคอลัมน์เป็นตารางย่อย ส่วน Table.Join จะขยายคอลัมน์จากอีกตารางทันที เหมาะคนละสถานการณ์

**Q: ต้องขยายคอลัมน์ผลลัพธ์อย่างไร?**

ใช้ Table.ExpandTableColumn เพื่อเลือกคอลัมน์จากตารางย่อยมาแสดงในตารางหลัก

**Q: ถ้ามีหลายแถวที่จับคู่ได้จะเกิดอะไรขึ้น?**

ตารางย่อยในคอลัมน์ผลลัพธ์จะเก็บทุกแถวที่จับคู่ได้ ไม่ได้จำกัดแค่แถวแรก

**Q: ค่าเริ่มต้นของ joinKind คืออะไร?**

ถ้าไม่ระบุ joinKind จะใช้ JoinKind.LeftOuter ซึ่งเก็บทุกแถวจากตารางซ้ายไว้

## ฟังก์ชันที่เกี่ยวข้อง

- [Table.Join – รวมตารางแบบ SQL Join](https://www.thepexcel.com/functions/power-query/table-functions/table-join/)
- [Table.ExpandTableColumn – แตกข้อมูลจากคอลัมน์ตารางที่ซ้อนกัน](https://www.thepexcel.com/functions/power-query/table-functions/table-expandtablecolumn/)
- [Table.ExpandRecordColumn – ขยายคอลัมน์ที่เป็น Record ให้เป็นหลายคอลัมน์](https://www.thepexcel.com/?post_type=function-explainer&p=37949)
- [Table.ExpandListColumn – ขยาย List เป็นหลายแถว](https://www.thepexcel.com/functions/power-query/table-functions/table-expandlistcolumn/)
- [Table.Combine – รวมหลาย table เข้าด้วยกัน](https://www.thepexcel.com/functions/power-query/table-functions/table-combine/)
- [Table.SelectRows – กรองแถวตามเงื่อนไขใน Power Query](https://www.thepexcel.com/functions/power-query/table-functions/table-selectrows/)

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

- [Microsoft Learn: Table.NestedJoin](https://learn.microsoft.com/en-us/powerquery-m/table-nestedjoin) _(documentation)_
- [Microsoft Learn: JoinKind.Type](https://learn.microsoft.com/en-us/powerquery-m/joinkind-type) _(documentation)_
- [PowerQuery.how: Table.NestedJoin](https://powerquery.how/table-nestedjoin/) _(guide)_

---

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