---
title: Table.Unpivot – แปลงคอลัมน์ให้เป็นแถว (Unpivot)
url: https://www.thepexcel.com/functions/power-query/table-functions/table-unpivot/
type: function-explainer
program: Power Query
syntax: "Table.Unpivot(table as table, pivotColumns as list, attributeColumn as text, valueColumn as text) as table"
date: 2025-12-03
updated: 2025-12-20
scores:
  popularity: 8
  difficulty: 5
  usefulness: 9
---

# Table.Unpivot – แปลงคอลัมน์ให้เป็นแถว (Unpivot)

> แปลงคอลัมน์ที่ระบุให้กลายเป็นแถว (Wide to Long Format)

## คำอธิบาย

Table.Unpivot แปลงคอลัมน์ที่ระบุให้กลายเป็นแถว (Unpivot) โดยสร้าง 2 คอลัมน์ใหม่ คอลัมน์แรกเก็บชื่อคอลัมน์เดิม (Attribute) และคอลัมน์ที่สองเก็บค่า (Value) ฟังก์ชันนี้จะข้ามค่า null โดยอัตโนมัติ เหมาะสำหรับเตรียมข้อมูลแบบ Wide Format ให้กลายเป็น Long Format สำหรับใช้ใน Power BI หรือ Pivot Table

## Syntax

```excel
Table.Unpivot(table as table, pivotColumns as list, attributeColumn as text, valueColumn as text) as table
```

**Variant**

```excel
Table.Unpivot(table, {"Col1", "Col2"}, "Attribute", "Value")
```

รูปแบบมาตรฐาน ระบุ list ของคอลัมน์ที่ต้องการ unpivot

**Variant**

```excel
Table.Unpivot(table, Table.ColumnNames(table), "Attribute", "Value")
```

Unpivot ทุกคอลัมน์โดยใช้ Table.ColumnNames ดึงรายชื่อคอลัมน์

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table | Yes | table |  | ตารางต้นทางที่ต้องการแปลงข้อมูล |
| pivotColumns | Yes | list |  | List ของชื่อคอลัมน์ที่ต้องการ unpivot (แปลงเป็นแถว) เช่น {"Jan", "Feb", "Mar"} |
| attributeColumn | Yes | text |  | ชื่อคอลัมน์ใหม่ที่จะเก็บชื่อคอลัมน์เดิม (Attribute) |
| valueColumn | Yes | text |  | ชื่อคอลัมน์ใหม่ที่จะเก็บค่าข้อมูล (Value) |

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

### เตรียมข้อมูลสำหรับ Power BI Data Model

แปลงข้อมูลยอดขายรายเดือนที่มีคอลัมน์ Jan, Feb, Mar... ให้กลายเป็นคอลัมน์ Month และ Sales เพื่อใช้ใน Data Model

_เหมาะกับ:_ data-modeling

### Normalize ข้อมูลจาก Excel Spreadsheet

แปลงข้อมูลที่ออกแบบมาให้อ่านง่าย (Wide Format) ให้อยู่ในรูปแบบที่เหมาะสำหรับ Database หรือ Analysis

_เหมาะกับ:_ data-transformation

### เตรียมข้อมูลสำหรับ Pivot Table

แปลงข้อมูลให้อยู่ในรูปแบบ Long Format ก่อนนำไปสร้าง Pivot Table ใน Excel

_เหมาะกับ:_ reporting

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: Unpivot ข้อมูลยอดขายรายปี

```excel
let
    // สร้างตารางยอดขายรายปี (Wide Format)
    Source = Table.FromRecords({
        [Region = "North", Y2022 = 100, Y2023 = 120],
        [Region = "South", Y2022 = 200, Y2023 = 250],
        [Region = "East", Y2022 = 150, Y2023 = 180]
    }),
    // แปลงคอลัมน์ปีให้เป็นแถว
    Unpivoted = Table.Unpivot(
        Source,
        {"Y2022", "Y2023"},
        "Year",
        "Sales"
    )
in
    Unpivoted
```

**ผลลัพธ์:** `Table 6 แถว:
| Region | Year  | Sales |
|--------|-------|-------|
| North  | Y2022 | 100   |
| North  | Y2023 | 120   |
| South  | Y2022 | 200   |
| South  | Y2023 | 250   |
| East   | Y2022 | 150   |
| East   | Y2023 | 180   |`

ตารางเดิมมี 3 แถว 3 คอลัมน์ พอ unpivot แล้วจะได้ 6 แถว 3 คอลัมน์ โดย Region ยังคงอยู่ ส่วนคอลัมน์ Y2022 และ Y2023 ถูกแปลงเป็นแถวใน Year และค่าไปอยู่ใน Sales ครับ

### 2. ตัวอย่างที่ 2: Unpivot กับค่า null (ข้อมูลไม่ครบ)

```excel
let
    // ตารางที่มีค่า null บางช่อง
    Source = Table.FromRecords({
        [Product = "Apple", Jan = 10, Feb = null, Mar = 30],
        [Product = "Banana", Jan = 20, Feb = 25, Mar = null]
    }),
    // Unpivot จะข้าม null อัตโนมัติ
    Unpivoted = Table.Unpivot(
        Source,
        {"Jan", "Feb", "Mar"},
        "Month",
        "Quantity"
    )
in
    Unpivoted
```

**ผลลัพธ์:** `Table 4 แถว (ไม่รวม null):
| Product | Month | Quantity |
|---------|-------|----------|
| Apple   | Jan   | 10       |
| Apple   | Mar   | 30       |
| Banana  | Jan   | 20       |
| Banana  | Feb   | 25       |`

สังเกตว่า Apple-Feb และ Banana-Mar หายไปเพราะเป็น null ที่เจ๋งคือ Table.Unpivot จะ filter ค่า null ออกให้อัตโนมัติ ไม่ต้องมาทำเองทีหลัง 💡

### 3. ตัวอย่างที่ 3: Unpivot ข้อมูลรายไตรมาสสำหรับ Power BI

```excel
let
    // ข้อมูลยอดขายรายไตรมาส (แบบที่เห็นบ่อยใน Excel)
    Source = Table.FromRecords({
        [ID = 1, Product = "Laptop", Q1 = 500, Q2 = 600, Q3 = 550, Q4 = 700],
        [ID = 2, Product = "Mouse", Q1 = 100, Q2 = 120, Q3 = 110, Q4 = 150],
        [ID = 3, Product = "Keyboard", Q1 = 80, Q2 = 90, Q3 = 85, Q4 = 100]
    }),
    // แปลงให้เป็น Long Format สำหรับ Data Model
    Unpivoted = Table.Unpivot(
        Source,
        {"Q1", "Q2", "Q3", "Q4"},
        "Quarter",
        "Revenue"
    )
in
    Unpivoted
```

**ผลลัพธ์:** `Table 12 แถว:
| ID | Product  | Quarter | Revenue |
|----|----------|---------|--------|
| 1  | Laptop   | Q1      | 500    |
| 1  | Laptop   | Q2      | 600    |
| ...| ...      | ...     | ...    |
| 3  | Keyboard | Q4      | 100    |`

รูปแบบนี้เหมาะสำหรับ Power BI มากครับ เพราะสามารถสร้าง Date Table แยก แล้ว join กับคอลัมน์ Quarter ได้ ทำให้วิเคราะห์แนวโน้มรายไตรมาสได้ง่าย

### 4. ตัวอย่างที่ 4: เปรียบเทียบ Table.Unpivot vs Table.UnpivotOtherColumns

```excel
let
    // ตารางต้นฉบับ
    Source = Table.FromRecords({
        [Store = "A", Manager = "John", Jan = 100, Feb = 120, Mar = 110]
    }),
    
    // วิธีที่ 1: Table.Unpivot - ระบุคอลัมน์ที่จะ unpivot
    Method1 = Table.Unpivot(
        Source,
        {"Jan", "Feb", "Mar"},
        "Month",
        "Sales"
    ),
    
    // วิธีที่ 2: Table.UnpivotOtherColumns - ระบุคอลัมน์ที่จะเก็บไว้
    Method2 = Table.UnpivotOtherColumns(
        Source,
        {"Store", "Manager"},
        "Month",
        "Sales"
    )
    // ผลลัพธ์ทั้ง 2 วิธีเหมือนกัน!
in
    Method1
```

**ผลลัพธ์:** `Table 3 แถว:
| Store | Manager | Month | Sales |
|-------|---------|-------|-------|
| A     | John    | Jan   | 100   |
| A     | John    | Feb   | 120   |
| A     | John    | Mar   | 110   |`

ทั้ง 2 วิธีให้ผลลัพธ์เหมือนกัน แต่เลือกใช้ต่างกันตามสถานการณ์:
- ใช้ Table.Unpivot เมื่อรู้แน่ชัดว่าคอลัมน์ไหนจะ unpivot (เช่น Jan, Feb, Mar)
- ใช้ Table.UnpivotOtherColumns เมื่อคอลัมน์อาจเพิ่มขึ้นในอนาคต (เช่น มี Apr, May เพิ่มมา) 😎

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

**Tip ที่ผมใช้บ่อย:**
.
ถ้าข้อมูลต้นทางมีคอลัมน์เพิ่มขึ้นบ่อยๆ (เช่น เพิ่มเดือนใหม่ทุกเดือน) ให้ใช้ Table.UnpivotOtherColumns แทน Table.Unpivot ครับ เพราะไม่ต้องมาแก้ list ของคอลัมน์ทุกครั้ง
.
อีกอย่างที่ต้องระวังคือ Table.Unpivot จะข้าม null ออก ถ้าต้องการเก็บ null ไว้ (เช่น ต้องการรู้ว่าเดือนไหนไม่มีข้อมูล) ให้ใช้ Table.ReplaceValue แทนที่ null ด้วยค่าอื่นก่อน unpivot 💡

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

**Q: Table.Unpivot กับ Table.UnpivotOtherColumns ต่างกันอย่างไร?**

Table.Unpivot ต้องระบุคอลัมน์ที่จะ unpivot โดยตรง ส่วน Table.UnpivotOtherColumns ต้องระบุคอลัมน์ที่จะ keep ไว้แทน ถ้าข้อมูลมีคอลัมน์เพิ่มมาใหม่บ่อยๆ (เช่น เดือนใหม่) แนะนำใช้ UnpivotOtherColumns ครับ เพราะไม่ต้องมาแก้ code ทุกครั้ง

**Q: ทำไมบางแถวหายไปหลัง Unpivot?**

Table.Unpivot จะข้ามค่า null โดยอัตโนมัติครับ ถ้าต้องการเก็บ null ไว้ด้วย ให้เปลี่ยนค่า null เป็นค่าอื่นก่อน (เช่น 0 หรือ "N/A") ด้วย Table.ReplaceValue แล้วค่อย unpivot

**Q: จะ Unpivot กลับเป็น Pivot ได้อย่างไร?**

ใช้ฟังก์ชัน Table.Pivot ครับ โดยระบุ Attribute Column เป็นคอลัมน์ที่จะกระจายเป็น header และ Value Column เป็นค่าที่จะใส่ในตาราง ถ้าต้องการ aggregate ค่า ให้ใส่ aggregation function เป็น argument สุดท้าย

**Q: Unpivot แล้วลำดับคอลัมน์เปลี่ยน ทำอย่างไร?**

ใช้ Table.ReorderColumns หลัง unpivot เพื่อจัดลำดับคอลัมน์ใหม่ได้ครับ เช่น Table.ReorderColumns(Unpivoted, {"ID", "Name", "Attribute", "Value"})

**Q: Unpivot คอลัมน์ที่มี Data Type ต่างกันได้ไหม?**

ได้ครับ แต่ค่าใน Value Column จะถูกแปลงเป็น type any ทำให้ต้องมา cast type ใหม่ทีหลัง แนะนำให้ unpivot คอลัมน์ที่มี type เดียวกันจะดีกว่า

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

- [Table.UnpivotOtherColumns – แปลงคอลัมน์ที่เหลือเป็นแถว (Unpivot แบบ Dynamic)](https://www.thepexcel.com/functions/power-query/table-functions/table-unpivotothercolumns/)
- [Table.Pivot – หมุนข้อมูลจากแนวตั้งเป็นแนวนอน](https://www.thepexcel.com/functions/power-query/table-functions/table-pivot/)
- [Table.Transpose – สลับแกนตาราง (Transpose)](https://www.thepexcel.com/functions/power-query/table-functions/table-transpose/)
- [Table.FromRows – สร้างตารางจากรายการแถว](https://www.thepexcel.com/functions/power-query/table-functions/table-fromrows/)
- [Table.ColumnNames – ดึงชื่อคอลัมน์จากตาราง](https://www.thepexcel.com/functions/power-query/table-functions/table-columnnames/)

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

- [Microsoft Learn: Table.Unpivot](https://learn.microsoft.com/en-us/powerquery-m/table-unpivot) _(documentation)_
- [Microsoft Learn: Table.UnpivotOtherColumns](https://learn.microsoft.com/en-us/powerquery-m/table-unpivotothercolumns) _(documentation)_
- [Power Query M Reference](https://learn.microsoft.com/en-us/powerquery-m/) _(guide)_

---

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