Table.ReplacePartitionKey แทนที่ partition key ของตาราง ซึ่งเป็น metadata สำหรับการแบ่งพาร์ทิชันและการจัดการข้อมูลภายใน เอาไว้ควบคุมวิธีการแบ่งพาร์ทิชันของข้อมูลในตารางนั้น
=Table.ReplacePartitionKey(table as table, partitionKey as nullable list) as table
=Table.ReplacePartitionKey(table as table, partitionKey as nullable list) as table
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| table | table | Yes | ตารางที่ต้องการแทนที่ partition key | |
| partitionKey | nullable list | Yes | รายการ (list) ของชื่อคอลัมน์ที่จะใช้เป็นคีย์ใหม่ หรือ null เพื่อเอาคีย์ออก |
ใช้เมื่อจำเป็นต้องแก้ไข partition key ของตารางในบริบทขั้นสูง/ภายใน เช่น การจัดการตารางที่ถูกแบ่งพาร์ทิชัน
let SourceTable = Table.FromRecords({ [CustomerID=1, Region="TH", Amount=1000], [CustomerID=2, Region="TH", Amount=2000] }), UpdatedTable = Table.ReplacePartiti…let
SourceTable = Table.FromRecords({
[CustomerID=1, Region="TH", Amount=1000],
[CustomerID=2, Region="TH", Amount=2000]
}),
UpdatedTable = Table.ReplacePartitionKey(SourceTable, {"CustomerID"})
in
Table.PartitionKey(UpdatedTable)
{"CustomerID"}
let SourceTable = Table.FromRecords({ [CustomerID=1, Region="TH", Year=2024], [CustomerID=2, Region="US", Year=2024], [CustomerID=1, Region="TH", Year=2025] }),…let
SourceTable = Table.FromRecords({
[CustomerID=1, Region="TH", Year=2024],
[CustomerID=2, Region="US", Year=2024],
[CustomerID=1, Region="TH", Year=2025]
}),
UpdatedTable = Table.ReplacePartitionKey(SourceTable, {"Region", "Year"})
in
Table.PartitionKey(UpdatedTable)
{"Region", "Year"}
let SourceTable = Table.FromRecords({ [ID=1, Name="John"], [ID=2, Name="Jane"] }), ClearedTable = Table.ReplacePartitionKey(SourceTable, null) in Table.Partitio…let
SourceTable = Table.FromRecords({
[ID=1, Name="John"],
[ID=2, Name="Jane"]
}),
ClearedTable = Table.ReplacePartitionKey(SourceTable, null)
in
Table.PartitionKey(ClearedTable)
null
let SourceData = Table.FromRecords({ [InvoiceID=1001, LastModified="2024-01-15", Amount=500], [InvoiceID=1002, LastModified="2024-01-20", Amount=750] }), Optimi…let
SourceData = Table.FromRecords({
[InvoiceID=1001, LastModified="2024-01-15", Amount=500],
[InvoiceID=1002, LastModified="2024-01-20", Amount=750]
}),
OptimizedTable = Table.ReplacePartitionKey(SourceData, {"LastModified"})
in
OptimizedTable
ตารางที่มี partition key เป็น LastModified
ผม มักสงสัยเรื่องนี้ตอนแรกเหมือนกัน 😅 partition key คือ metadata ของ Power Query ใช้บอก Power Query ว่า ‘แบ่งพาร์ทิชันตามคอลัมน์นี้’ เพื่อให้ refresh และการจัดการข้อมูลเร็วขึ้น ส่วนคีย์หลัก (Primary Key) คือข้อบังคับของฐานข้อมูล บอก ‘แต่ละแถวต้องมีค่าไม่ซ้ำในคอลัมน์นี้’ สองอย่างต่างกันโลก
ส่วนตัวผมพบประโยชน์ตอนทำ Power BI ที่มี incremental refresh หรือจำนวนข้อมูลมากมาย การตั้ง partition key ให้ถูกช่วย optimize refresh time ได้เยอะ นอกจากนั้น ถ้าข้อมูลมาจากหลายแหล่งและต้องรวมกันแล้วเซ็ต partition key ก็จะช่วยให้ Power BI จัดการข้อมูลได้ดีขึ้น
ถ้าส่ง null แล้วมันจะเอา partition key ออก (รีเซ็ต) คือบอก Power Query ว่า ‘ตารางนี้ไม่มี partition key’ แต่ขึ้นอยู่กับแหล่งข้อมูลว่าจะรองรับ null เหมือนกันไหม บางครั้งระบบอาจไม่ยอมรับ null
Table.ReplacePartitionKey ใช้แทนที่ partition key ของตาราง ซึ่ง partition key คือ metadata ที่บอก Power Query ว่าตารางนี้มีการแบ่งพาร์ทิชันแบบไหน โดยที่ partition key มักจะเป็นรายการของชื่อคอลัมน์ที่ใช้เป็นตัวแบ่งข้อมูล
ที่เจ๋งคือ partition key ไม่ใช่แค่ metadata ง่ายๆ มันบอก Power Query ว่า แยกข้อมูลตามคีย์นี้เพื่อให้สามารถจัดการข้อมูลได้มีประสิทธิภาพมากขึ้น ถ้าคุณทำงานกับ partitioned data sources หรือ incremental refresh ใน Power BI เรื่องนี้สำคัญมากครับ
ส่วนตัวผมเจอเรื่องนี้เมื่อทำการซิงค์ข้อมูลขนาดใหญ่ใน Power BI – การตั้ง partition key ให้ถูก ต่อหลังจากนั้น refresh ก็เร็วขึ้นตั้งเยอะเลยครับ 😎