---
title: Table.Partition – แบ่งตารางเป็นส่วนๆ
url: https://www.thepexcel.com/functions/power-query/table-functions/table-partition/
type: function-explainer
program: Power Query
syntax: "Table.Partition(table as table, column as text, groups as number, hash as function) as list"
date: 2025-12-06
updated: 2025-12-24
scores:
  popularity: 4
  difficulty: 6
  usefulness: 5
---

# Table.Partition – แบ่งตารางเป็นส่วนๆ

> แบ่งตารางออกเป็นรายการของตารางย่อย (List of Tables) โดยใช้ hash function กำหนดการกระจายข้อมูล

## คำอธิบาย

แบ่งตารางออกเป็นรายการของตารางย่อย (List of Tables) โดยใช้ hash function กำหนดการกระจายข้อมูล

## Syntax

```excel
Table.Partition(table as table, column as text, groups as number, hash as function) as list
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table | Yes | table |  | ตารางที่ต้องการแบ่ง |
| column | Yes | text |  | ชื่อคอลัมน์ที่ใช้เป็นตัวกำหนด partition (ค่าของคอลัมน์นี้จะผ่านฟังก์ชัน hash) |
| groups | Yes | number |  | จำนวนกลุ่ม (partition) ที่ต้องการ - ต้องเป็นจำนวนเต็มบวก |
| hash | Yes | function |  | ฟังก์ชันสำหรับคำนวณค่า hash ปกติใช้ each _ (แทนค่าในคอลัมน์) หรือ each Text.Hash(_) สำหรับแฮชข้อความ |

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

### แบ่งงานประมวลผล (Parallel Processing)

แบ่งงานประมวลผล (Parallel Processing)

### กระจายข้อมูลลงถัง (Bucketing)

กระจายข้อมูลลงถัง (Bucketing)

### เตรียมข้อมูลสำหรับการ Load เข้า Partitioned Table ใน Database

เตรียมข้อมูลสำหรับการ Load เข้า Partitioned Table ใน Database

## ตัวอย่าง

### 1. แบ่งตารางเลขเป็น 2 ส่วน

```excel
let
    Source = #table({"ID"}, {{1}, {2}, {3}, {4}, {5}, {6}}),
    Partitions = Table.Partition(Source, "ID", 2, each _)
in
    Partitions
```

**ผลลัพธ์:** `List containing 2 Tables: {Table1, Table2}`

แบ่งข้อมูล ID 1-6 เป็น 2 ตารางย่อย: ID คี่ (1,3,5) ไปกลุ่มเดียว, ID คู่ (2,4,6) ไปกลุ่มอื่น โดยใช้ modulo 2 จากค่า ID

### 2. แบ่งตามตัวอักษรแรกของ Name

```excel
let
    Source = #table(
        {"Name"}, 
        {{"Apple"}, {"Avocado"}, {"Banana"}, {"Blueberry"}, {"Cherry"}}
    ),
    Partitions = Table.Partition(
        Source, 
        "Name", 
        2, 
        each Character.ToNumber(Text.Start(_, 1)) mod 65
    )
in
    Partitions
```

**ผลลัพธ์:** `List of 2 Tables`

แปลงตัวอักษรแรกเป็นรหัส ASCII (A=65) แล้วใช้ modulo 2 แบ่งเป็น 2 กลุ่ม ตัวอักษรที่เป็นคู่กับตัวอักษรที่เป็นคี่ (ตามรหัส ASCII)

### 3. แบ่งข้อมูล 1000 แถวเป็น 5 batch

```excel
let
    Source = Table.FromRecords(
        List.Transform(
            List.Numbers(1, 1000),
            each [ID = _, Value = _ * 2]
        )
    ),
    Batches = Table.Partition(Source, "ID", 5, each _)
in
    Batches
```

**ผลลัพธ์:** `List of 5 Tables (แต่ละตารางมี ~200 แถว)`

แบ่งข้อมูล 1000 แถวออกเป็น 5 batch เพื่อประมวลผลแยกกัน ค่า ID ที่ modulo 5 = 0 ไปกลุ่มแรก, modulo 5 = 1 ไปกลุ่มที่สอง เป็นต้น

### 4. ใช้ Text.Hash สำหรับการแจกแจงที่สม่ำเสมอ

```excel
let
    Source = #table(
        {"Email"}, 
        {{"user1@example.com"}, {"user2@example.com"}, {"user3@example.com"}}
    ),
    Partitions = Table.Partition(Source, "Email", 3, each Text.Hash(_))
in
    Partitions
```

**ผลลัพธ์:** `List of 3 Tables`

ใช้ Text.Hash เพื่อสร้างค่า hash ที่สม่ำเสมอ จากข้อความ Email แล้วแบ่งเป็น 3 กลุ่ม การใช้ Text.Hash ให้การกระจายที่ดีกว่า especially สำหรับข้อมูลข้อความ

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

- ใช้ List.Transform กับผลลัพธ์เพื่อประมวลผลแต่ละ partition แยกกัน

- หากต้องการ access แต่ละ partition ให้ใช้ {0}, {1}, {2} เพื่ออ้างถึง index ของ List

- สำหรับข้อมูลขนาดใหญ่มาก ลองใช้ Text.Hash แทน each _ เพื่อการกระจายที่ดีกว่า

- ผลลัพธ์เป็น List ของ Table ไม่ใช่ Table เดี่ยว ต้องจัดการกับรูปแบบที่ต่างกัน

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

**Q: ผลการแบ่งจะเท่ากันทุกตารางหรือไม่?**

ไม่จำเป็น ผลการแบ่งขึ้นอยู่กับค่า hash ของข้อมูล ถ้าข้อมูลกระจายตัวดี ผลจะค่อนข้างเท่าๆ กัน แต่ถ้าข้อมูลเบ่งเบนไปทางค่าใดค่าหนึ่ง บางตารางอาจจะมีแถวมากกว่า

**Q: แตกต่างจาก Table.Split ยังไง?**

Table.Split แบ่งตามจำนวนแถว (เช่น ทุก 100 แถว) ส่วน Table.Partition แบ่งตามค่า hash ของคอลัมน์ที่กำหนด ใช้ได้กับข้อมูลที่ต้องกระจายแบบสม่ำเสมอ

**Q: hash function ที่ควรใช้คืออะไร?**

สำหรับตัวเลข: each _ | สำหรับข้อความ: each Text.Hash(_) | สำหรับวันที่: each [Date.DayOfYear(_) * 1000] | ระบุ function ที่คืนค่าตัวเลขได้

**Q: ถ้า groups = 1 จะได้อะไร?**

จะได้รายการที่มี 1 ตารางเท่านั้น (ตารางต้นฉบับเหมือนเดิม) ไม่เหมาะที่จะ partition

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

- [Table.ReplacePartitionKey – แทนที่คีย์แบ่งพาร์ทิชันของตาราง](https://www.thepexcel.com/functions/power-query/table-functions/table-replacepartitionkey/)

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

- [Microsoft Docs - Table.Partition](https://learn.microsoft.com/en-us/powerquery-m/table-partition) _(official)_
- [PowerQuery.how](https://powerquery.how/table-partition/) _(article)_

---

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