---
title: Table.UnpivotOtherColumns – แปลงคอลัมน์ที่เหลือเป็นแถว (Unpivot แบบ Dynamic)
url: https://www.thepexcel.com/functions/power-query/table-functions/table-unpivotothercolumns/
type: function-explainer
program: Power Query
syntax: "Table.UnpivotOtherColumns(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.UnpivotOtherColumns – แปลงคอลัมน์ที่เหลือเป็นแถว (Unpivot แบบ Dynamic)

> แปลงคอลัมน์ที่เหลือทั้งหมดเป็นแถว (Dynamic Unpivot)

## คำอธิบาย

Table.UnpivotOtherColumns แปลงคอลัมน์ที่ไม่ได้ระบุทั้งหมดให้กลายเป็นแถว (Unpivot) โดยระบุเฉพาะคอลัมน์ที่ต้องการคงไว้ ข้อดีคือเป็น Dynamic ถ้ามีคอลัมน์ใหม่เพิ่มเข้ามาในอนาคตก็จะถูก Unpivot ให้อัตโนมัติ เหมาะสำหรับแปลงข้อมูลจาก Wide Format (หลายคอลัมน์) ให้เป็น Long Format (หลายแถว) สำหรับ PivotTable และ Database

## Syntax

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

**Variant**

```excel
Table.UnpivotOtherColumns(Source, {"ID"}, "Attribute", "Value")
```

รูปแบบพื้นฐาน - เก็บคอลัมน์ ID ไว้ แปลงคอลัมน์อื่นทั้งหมดเป็นแถว

**Variant**

```excel
Table.UnpivotOtherColumns(Source, {"Region", "Product"}, "Month", "Sales")
```

เก็บหลายคอลัมน์ไว้ - เก็บ Region และ Product แปลงคอลัมน์เดือนเป็นแถว

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table | Yes | table |  | ตารางข้อมูลที่ต้องการ Unpivot |
| pivotColumns | Yes | list |  | รายชื่อคอลัมน์ที่ต้องการ "เก็บไว้" ไม่ต้องการ Unpivot (คอลัมน์คงที่ เช่น ID, Region, Product) |
| attributeColumn | Yes | text |  | ชื่อคอลัมน์ใหม่สำหรับเก็บชื่อหัวคอลัมน์เดิมที่ถูก Unpivot (เช่น "Month", "Year", "Attribute") |
| valueColumn | Yes | text |  | ชื่อคอลัมน์ใหม่สำหรับเก็บค่าข้อมูลจากคอลัมน์ที่ถูก Unpivot (เช่น "Sales", "Amount", "Value") |

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

### แปลงข้อมูลยอดขายรายเดือน

ข้อมูลจาก Excel มักมีคอลัมน์ Jan, Feb, Mar... ใช้ฟังก์ชันนี้แปลงเป็นคอลัมน์ Month และ Sales เพื่อนำไป PivotTable หรือวิเคราะห์ใน Power BI

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

### รองรับข้อมูลที่เพิ่มคอลัมน์ใหม่

เมื่อมีคอลัมน์เดือนหรือปีใหม่เพิ่มเข้ามาในข้อมูลต้นทาง ฟังก์ชันนี้จะ Unpivot คอลัมน์ใหม่ให้อัตโนมัติโดยไม่ต้องแก้โค้ด

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

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

แปลงข้อมูลจาก Wide Format (Spreadsheet style) เป็น Long Format (Database style) เพื่อนำเข้า SQL Server หรือ Data Warehouse

_เหมาะกับ:_ etl-process

## ตัวอย่าง

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

```excel
let
    // สร้างตารางยอดขายที่มีคอลัมน์เป็นเดือน
    Source = Table.FromRecords({
        [Product = "iPhone", Jan = 100, Feb = 120, Mar = 150],
        [Product = "iPad", Jan = 50, Feb = 60, Mar = 70]
    }),
    // Unpivot โดยเก็บคอลัมน์ Product ไว้
    // คอลัมน์อื่นๆ (Jan, Feb, Mar) จะถูกแปลงเป็นแถว
    Unpivoted = Table.UnpivotOtherColumns(
        Source,
        {"Product"},
        "Month",
        "Sales"
    )
in
    Unpivoted
```

**ผลลัพธ์:** `Table 6 rows: (iPhone, Jan, 100), (iPhone, Feb, 120), (iPhone, Mar, 150), (iPad, Jan, 50), (iPad, Feb, 60), (iPad, Mar, 70)`

จากตารางที่มี 2 แถว 4 คอลัมน์ (Product, Jan, Feb, Mar) กลายเป็น 6 แถว 3 คอลัมน์ (Product, Month, Sales) สังเกตว่าคอลัมน์ Product ยังคงอยู่ ส่วนคอลัมน์เดือนกลายเป็นค่าในคอลัมน์ Month ครับ

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

```excel
let
    // ตารางยอดขายตาม Region และ Product
    Source = Table.FromRecords({
        [Region = "North", Product = "Apple", Q1 = 1000, Q2 = 1200, Q3 = 1100],
        [Region = "South", Product = "Orange", Q1 = 800, Q2 = 900, Q3 = 950]
    }),
    // เก็บทั้ง Region และ Product ไว้
    // Unpivot เฉพาะคอลัมน์ไตรมาส
    Unpivoted = Table.UnpivotOtherColumns(
        Source,
        {"Region", "Product"},
        "Quarter",
        "Amount"
    )
in
    Unpivoted
```

**ผลลัพธ์:** `Table 6 rows: (North, Apple, Q1, 1000), (North, Apple, Q2, 1200), (North, Apple, Q3, 1100), (South, Orange, Q1, 800), (South, Orange, Q2, 900), (South, Orange, Q3, 950)`

เก็บคอลัมน์ Region และ Product ไว้คงที่ ส่วนคอลัมน์ Q1, Q2, Q3 ถูก Unpivot กลายเป็นค่าในคอลัมน์ Quarter วิธีนี้ใช้บ่อยมากในการแปลงข้อมูลสำหรับ PivotTable ครับ 💡

### 3. ตัวอย่างที่ 3: Dynamic - รองรับคอลัมน์ใหม่อัตโนมัติ

```excel
let
    // สมมติข้อมูลปีหน้ามีเดือน Apr เพิ่มมา
    Source = Table.FromRecords({
        [ID = 1, Name = "John", Jan = 10, Feb = 20, Mar = 30, Apr = 40],
        [ID = 2, Name = "Jane", Jan = 15, Feb = 25, Mar = 35, Apr = 45]
    }),
    // ระบุแค่คอลัมน์ที่จะเก็บไว้ (ID, Name)
    // คอลัมน์อื่นๆ ทั้งหมดจะถูก Unpivot อัตโนมัติ
    Unpivoted = Table.UnpivotOtherColumns(
        Source,
        {"ID", "Name"},
        "Month",
        "Value"
    )
in
    Unpivoted
```

**ผลลัพธ์:** `Table 8 rows: (1, John, Jan, 10), (1, John, Feb, 20), (1, John, Mar, 30), (1, John, Apr, 40), (2, Jane, Jan, 15), (2, Jane, Feb, 25), (2, Jane, Mar, 35), (2, Jane, Apr, 45)`

ที่เจ๋งคือถ้าปีหน้ามีเดือน May, Jun เพิ่มมา โค้ดนี้ก็ยังใช้งานได้เลยไม่ต้องแก้ไข เพราะมัน Unpivot ทุกคอลัมน์ที่ไม่ใช่ ID และ Name นี่คือข้อดีหลักที่ทำให้ UnpivotOtherColumns ดีกว่า Table.Unpivot ธรรมดาครับ 😎

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

```excel
let
    Source = Table.FromRecords({
        [Product = "Laptop", Year2022 = 500, Year2023 = 600, Year2024 = 700]
    }),
    
    // วิธี 1: Table.Unpivot - ต้องระบุคอลัมน์ที่จะ Unpivot
    // ถ้าเพิ่ม Year2025 ต้องมาแก้โค้ดเพิ่ม
    Method1 = Table.Unpivot(
        Source,
        {"Year2022", "Year2023", "Year2024"},
        "Year",
        "Sales"
    ),
    
    // วิธี 2: Table.UnpivotOtherColumns - ระบุแค่คอลัมน์ที่จะเก็บ
    // ถ้าเพิ่ม Year2025 จะ Unpivot ให้อัตโนมัติ
    Method2 = Table.UnpivotOtherColumns(
        Source,
        {"Product"},
        "Year",
        "Sales"
    )
in
    Method2
```

**ผลลัพธ์:** `Table 3 rows: (Laptop, Year2022, 500), (Laptop, Year2023, 600), (Laptop, Year2024, 700)`

ผลลัพธ์เหมือนกัน แต่ Method2 (UnpivotOtherColumns) ยืดหยุ่นกว่า เพราะไม่ต้อง hardcode ชื่อคอลัมน์ปี ส่วนตัวผมแนะนำใช้ UnpivotOtherColumns เป็นหลักครับ ยกเว้นกรณีที่ต้องการ Unpivot เฉพาะบางคอลัมน์เท่านั้น

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

💡 **Tip จากประสบการณ์:**
.
ส่วนตัวผมมักใช้ Table.UnpivotOtherColumns เป็นหลักเสมอ ยกเว้นกรณีที่ต้องการ Unpivot เฉพาะบางคอลัมน์จริงๆ เพราะมันยืดหยุ่นกว่ามาก ไม่ต้องมาแก้โค้ดทุกครั้งที่มีคอลัมน์ใหม่เพิ่มเข้ามาครับ
.
ที่ต้องระวังคือ Data Type ของคอลัมน์ที่จะ Unpivot ควรเป็นประเภทเดียวกัน (เช่น Number หมด หรือ Text หมด) ไม่งั้นอาจเกิด Error ได้ 😎

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

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

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

**Q: ค่า null จะถูกจัดการอย่างไร?**

ค่า null จะถูกลบออกไปโดยอัตโนมัติระหว่าง Unpivot ถ้าต้องการเก็บ null ไว้ด้วย ต้องแปลง null เป็นค่าอื่นก่อน (เช่น 0 หรือ text ว่าง) แล้วค่อย Unpivot ครับ

**Q: ใช้ใน Power Query Editor ได้อย่างไร?**

เลือกคอลัมน์ที่ต้องการเก็บไว้ (กด Ctrl+Click) จากนั้นคลิกขวาแล้วเลือก "Unpivot Other Columns" ระบบจะสร้างโค้ด Table.UnpivotOtherColumns ให้อัตโนมัติครับ

**Q: Unpivot แล้วทำให้ข้อมูลหายไปทำไม?**

ปัญหานี้เจอบ่อยครับ 😅 สาเหตุหลักคือ cell ว่างหรือ null ถูกลบออกไป ให้เช็คข้อมูลต้นทางว่ามี null หรือเปล่า และถ้าต้องการเก็บ null ไว้ ให้ใช้ Table.ReplaceValue แปลง null เป็นค่าอื่นก่อน Unpivot ครับ

**Q: ควรใช้ตอนไหน และไม่ควรใช้ตอนไหน?**

ใช้เมื่อต้องการแปลง Wide Format เป็น Long Format และข้อมูลอาจมีคอลัมน์เพิ่มในอนาคต ไม่ควรใช้เมื่อต้องการ Unpivot เฉพาะบางคอลัมน์เท่านั้น (ใช้ Table.Unpivot แทน) หรือเมื่อข้อมูลต้นทางมี Data Type ไม่ตรงกันในแต่ละคอลัมน์ครับ

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

- [Table.Unpivot – แปลงคอลัมน์ให้เป็นแถว (Unpivot)](https://www.thepexcel.com/functions/power-query/table-functions/table-unpivot/)
- [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.FromRecords – สร้างตารางจากรายการ Record](https://www.thepexcel.com/functions/power-query/table-functions/table-fromrecords/)

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

- [Microsoft Learn: Table.UnpivotOtherColumns](https://learn.microsoft.com/en-us/powerquery-m/table-unpivotothercolumns) _(documentation)_
- [Microsoft Learn: Unpivot columns](https://learn.microsoft.com/en-us/power-query/unpivot-column) _(guide)_
- [PowerQuery.how: Table.UnpivotOtherColumns](https://powerquery.how/table-unpivotothercolumns/) _(guide)_

---

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