---
title: Table.ReplacePartitionKey – แทนที่คีย์แบ่งพาร์ทิชันของตาราง
url: https://www.thepexcel.com/functions/power-query/table-functions/table-replacepartitionkey/
type: function-explainer
program: Power Query
syntax: "Table.ReplacePartitionKey(table as table, partitionKey as nullable list) as table"
date: 2025-12-15
updated: 2025-12-25
scores:
  popularity: 5
  difficulty: 4
  usefulness: 6
---

# Table.ReplacePartitionKey – แทนที่คีย์แบ่งพาร์ทิชันของตาราง

> Table.ReplacePartitionKey แทนที่ partition key ของตาราง ซึ่งเป็น metadata สำหรับการแบ่งพาร์ทิชันและก

## คำอธิบาย

Table.ReplacePartitionKey แทนที่ partition key ของตาราง ซึ่งเป็น metadata สำหรับการแบ่งพาร์ทิชันและการจัดการข้อมูลภายใน เอาไว้ควบคุมวิธีการแบ่งพาร์ทิชันของข้อมูลในตารางนั้น

## Syntax

```excel
Table.ReplacePartitionKey(table as table, partitionKey as nullable list) as table
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table | Yes | table |  | ตารางที่ต้องการแทนที่ partition key |
| partitionKey | Yes | nullable list |  | รายการ (list) ของชื่อคอลัมน์ที่จะใช้เป็นคีย์ใหม่ หรือ null เพื่อเอาคีย์ออก |

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

### ปรับ metadata ของตารางสำหรับการแบ่งพาร์ทิชัน

ใช้เมื่อจำเป็นต้องแก้ไข partition key ของตารางในบริบทขั้นสูง/ภายใน เช่น การจัดการตารางที่ถูกแบ่งพาร์ทิชัน

_เหมาะกับ:_ advanced-metadata

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: ตั้งคีย์เป็นคอลัมน์เดียว

```excel
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"}`

สร้างตารางตัวอย่างที่มี CustomerID, Region, Amount จากนั้นใช้ Table.ReplacePartitionKey เพื่อตั้ง partition key ให้เป็น CustomerID เท่านั้น ตอนท้ายตรวจสอบด้วย Table.PartitionKey

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

```excel
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"}`

ถ้าต้องการให้ partition key ประกอบด้วยหลายคอลัมน์ ก็ส่ง list ของชื่อคอลัมน์ เช่น {"Region", "Year"} ซึ่งมีประโยชน์เมื่อต้องแบ่งพาร์ทิชันตามหลายเกณฑ์พร้อมกัน

### 3. ตัวอย่างที่ 3: เอาคีย์ออกด้วย null

```excel
let
    SourceTable = Table.FromRecords({
        [ID=1, Name="John"],
        [ID=2, Name="Jane"]
    }),
    ClearedTable = Table.ReplacePartitionKey(SourceTable, null)
in
    Table.PartitionKey(ClearedTable)
```

**ผลลัพธ์:** `null`

partitionKey มีค่า nullable ดังนั้นสามารถส่ง null เพื่อลบ/รีเซ็ต partition key ของตาราง ผลลัพธ์จะเป็น null แสดงว่าตารางนี้ไม่มี partition key

### 4. ตัวอย่างที่ 4: Partition key ในบริบท Incremental Refresh

```excel
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`

ในสถานการณ์จริงเวลาใช้ Incremental Refresh ใน Power BI เรามักตั้ง partition key ให้เป็นคอลัมน์วันที่ (เช่น LastModified) เพื่อให้ Power Query แบ่งข้อมูลตามช่วงเวลา ทำให้ refresh เร็วขึ้น

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

- ผมแนะนำให้ใช้ Table.PartitionKey() ตรวจสอบคีย์ปัจจุบันก่อน เพื่อดูว่าตารางมี partition key อะไรอยู่ก่อนแล้วจึงเปลี่ยน

- ถ้าทำงานกับ incremental refresh ใน Power BI ให้ตั้ง partition key ให้เป็นคอลัมน์ช่วงเวลา (date/datetime) เช่น LastModified หรือ SyncDate เพื่อให้ Power BI refresh เร็วขึ้น

- ส่วนตัวผมแนะนำให้ดูครอบครัวของ Table.Partition* functions ด้วย (เช่น Table.Partition, Table.PartitionKey) เพื่อเข้าใจให้ลึก

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

**Q: Partition key ต่างจากคีย์หลักของฐานข้อมูลอย่างไรครับ?**

ผม มักสงสัยเรื่องนี้ตอนแรกเหมือนกัน 😅 partition key คือ metadata ของ Power Query ใช้บอก Power Query ว่า 'แบ่งพาร์ทิชันตามคอลัมน์นี้' เพื่อให้ refresh และการจัดการข้อมูลเร็วขึ้น ส่วนคีย์หลัก (Primary Key) คือข้อบังคับของฐานข้อมูล บอก 'แต่ละแถวต้องมีค่าไม่ซ้ำในคอลัมน์นี้' สองอย่างต่างกันโลก

**Q: ใช้ Table.ReplacePartitionKey ตอนไหนครับ?**

ส่วนตัวผมพบประโยชน์ตอนทำ Power BI ที่มี incremental refresh หรือจำนวนข้อมูลมากมาย การตั้ง partition key ให้ถูกช่วย optimize refresh time ได้เยอะ นอกจากนั้น ถ้าข้อมูลมาจากหลายแหล่งและต้องรวมกันแล้วเซ็ต partition key ก็จะช่วยให้ Power BI จัดการข้อมูลได้ดีขึ้น

**Q: ค่า null หมายความว่าอะไรครับ?**

ถ้าส่ง null แล้วมันจะเอา partition key ออก (รีเซ็ต) คือบอก Power Query ว่า 'ตารางนี้ไม่มี partition key' แต่ขึ้นอยู่กับแหล่งข้อมูลว่าจะรองรับ null เหมือนกันไหม บางครั้งระบบอาจไม่ยอมรับ null

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

- [Table.PartitionKey – จัดการตาราง](https://www.thepexcel.com/?post_type=function-explainer&p=37996)
- [Table.Partition – แบ่งตารางเป็นส่วนๆ](https://www.thepexcel.com/functions/power-query/table-functions/table-partition/)
- [Table.FromRecords – สร้างตารางจากรายการ Record](https://www.thepexcel.com/functions/power-query/table-functions/table-fromrecords/)

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

- [Microsoft Learn: Table.ReplacePartitionKey](https://learn.microsoft.com/en-us/powerquery-m/table-replacepartitionkey) _(official)_
- [Microsoft Learn: Table.PartitionKey](https://learn.microsoft.com/en-us/powerquery-m/table-partitionkey) _(official)_

---

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