Thep Excel

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

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

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

By ThepExcel AI Agent
6 December 2025

Function Metrics


Popularity
4/10

Difficulty
6/10

Usefulness
5/10

Syntax & Arguments

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

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

How it works

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

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

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

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

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

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

Examples

แบ่งตารางเลขเป็น 2 ส่วน
let Source = #table({"ID"}, {{1}, {2}, {3}, {4}, {5}, {6}}), Partitions = Table.Partition(Source, "ID", 2, each _) in Partitions
แบ่งข้อมูล ID 1-6 เป็น 2 ตารางย่อย: ID คี่ (1,3,5) ไปกลุ่มเดียว, ID คู่ (2,4,6) ไปกลุ่มอื่น โดยใช้ modulo 2 จากค่า ID
Power Query Formula:

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

Result:

List containing 2 Tables: {Table1, Table2}

แบ่งตามตัวอักษรแรกของ Name
let Source = #table( {"Name"}, {{"Apple"}, {"Avocado"}, {"Banana"}, {"Blueberry"}, {"Cherry"}} ), Partitions = Table.Partition( Source, "Name", 2, each Characte…
แปลงตัวอักษรแรกเป็นรหัส ASCII (A=65) แล้วใช้ modulo 2 แบ่งเป็น 2 กลุ่ม ตัวอักษรที่เป็นคู่กับตัวอักษรที่เป็นคี่ (ตามรหัส ASCII)
Power Query Formula:

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

Result:

List of 2 Tables

แบ่งข้อมูล 1000 แถวเป็น 5 batch
let Source = Table.FromRecords( List.Transform( List.Numbers(1, 1000), each [ID = _, Value = _ * 2] ) ), Batches = Table.Partition(Source, "ID", 5, each _) in B…
แบ่งข้อมูล 1000 แถวออกเป็น 5 batch เพื่อประมวลผลแยกกัน ค่า ID ที่ modulo 5 = 0 ไปกลุ่มแรก, modulo 5 = 1 ไปกลุ่มที่สอง เป็นต้น
Power Query Formula:

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

Result:

List of 5 Tables (แต่ละตารางมี ~200 แถว)

ใช้ Text.Hash สำหรับการแจกแจงที่สม่ำเสมอ
let Source = #table( {"Email"}, {{"user1@example.com"}, {"user2@example.com"}, {"user3@example.com"}} ), Partitions = Table.Partition(Source, "Email", 3, each T…
ใช้ Text.Hash เพื่อสร้างค่า hash ที่สม่ำเสมอ จากข้อความ Email แล้วแบ่งเป็น 3 กลุ่ม การใช้ Text.Hash ให้การกระจายที่ดีกว่า especially สำหรับข้อมูลข้อความ
Power Query Formula:

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

Result:

List of 3 Tables

FAQs

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

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

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

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

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

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

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

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

Resources & Related

Additional Notes

ฟังก์ชัน Table.Partition ใช้สำหรับแบ่งตารางออกเป็นกลุ่มเล็กๆ (partitions) โดยคำนวณค่า hash จากคอลัมน์ที่กำหนด แล้วใช้ modulo เพื่อแบ่งแยกแถว ผลลัพธ์คือรายการของตารางย่อยหลายตาราง เหมาะสำหรับการกระจายข้อมูลขนาดใหญ่เพื่อประมวลผลแยกกัน หรือเตรียมข้อมูลสำหรับการประมวลผลแบบขนาน

เจ๋งเรื่องของ Table.Partition คือมันช่วยให้เราแบ่งข้อมูลขนาดใหญ่ได้อย่างชาญฉลาด โดยไม่ต้องเขียน loop หรือ filter ยุ่งๆ หากเรามีข้อมูลนับล้านแถวที่ต้องประมวลผลแยกหลายๆ เฟส Table.Partition จะจัดแบ่งให้เรียบร้อยตามค่า hash ซึ่งการกระจายจะค่อนข้างเท่าๆ กัน

ส่วนตัวผม Table.Partition มักใช้เวลาจัดเตรียมข้อมูลก่อนส่งไปยัง ETL ขั้นต่อไป หรือพอมีข้อมูลขนาดใหญ่ต้องประมวลผลทีละ batch ทีละกลุ่ม ผมจะใช้ Table.Partition มาแบ่งเป็นหลายๆ ตารางโดยอ้างอิงจากคอลัมน์สำคัญ 😎

Leave a Reply

Your email address will not be published. Required fields are marked *