Thep Excel

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

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

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

By ThepExcel AI Agent
3 December 2025

Function Metrics


Popularity
8/10

Difficulty
5/10

Usefulness
9/10

Syntax & Arguments

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

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

How it works

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

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

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

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

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

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

Examples

ตัวอย่างที่ 1: Unpivot ข้อมูลยอดขายรายปี
let // สร้างตารางยอดขายรายปี (Wide Format) Source = Table.FromRecords({ [Region = "North", Y2022 = 100, Y2023 = 120], [Region = "South", Y2022 = 200, Y2023 = 25…
ตารางเดิมมี 3 แถว 3 คอลัมน์ พอ unpivot แล้วจะได้ 6 แถว 3 คอลัมน์ โดย Region ยังคงอยู่ ส่วนคอลัมน์ Y2022 และ Y2023 ถูกแปลงเป็นแถวใน Year และค่าไปอยู่ใน Sales ครับ
Power Query Formula:

=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

Result:

Table 6 แถว:
| Region | Year | Sales |
|--------|-------|-------|
| North | Y2022 | 100 |
| North | Y2023 | 120 |
| South | Y2022 | 200 |
| South | Y2023 | 250 |
| East | Y2022 | 150 |
| East | Y2023 | 180 |

ตัวอย่างที่ 2: Unpivot กับค่า null (ข้อมูลไม่ครบ)
let // ตารางที่มีค่า null บางช่อง Source = Table.FromRecords({ [Product = "Apple", Jan = 10, Feb = null, Mar = 30], [Product = "Banana", Jan = 20, Feb = 25, Mar…
สังเกตว่า Apple-Feb และ Banana-Mar หายไปเพราะเป็น null ที่เจ๋งคือ Table.Unpivot จะ filter ค่า null ออกให้อัตโนมัติ ไม่ต้องมาทำเองทีหลัง 💡
Power Query Formula:

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

Result:

Table 4 แถว (ไม่รวม null):
| Product | Month | Quantity |
|---------|-------|----------|
| Apple | Jan | 10 |
| Apple | Mar | 30 |
| Banana | Jan | 20 |
| Banana | Feb | 25 |

ตัวอย่างที่ 3: Unpivot ข้อมูลรายไตรมาสสำหรับ Power BI
let // ข้อมูลยอดขายรายไตรมาส (แบบที่เห็นบ่อยใน Excel) Source = Table.FromRecords({ [ID = 1, Product = "Laptop", Q1 = 500, Q2 = 600, Q3 = 550, Q4 = 700], [ID = 2…
รูปแบบนี้เหมาะสำหรับ Power BI มากครับ เพราะสามารถสร้าง Date Table แยก แล้ว join กับคอลัมน์ Quarter ได้ ทำให้วิเคราะห์แนวโน้มรายไตรมาสได้ง่าย
Power Query Formula:

=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

Result:

Table 12 แถว:
| ID | Product | Quarter | Revenue |
|----|----------|---------|--------|
| 1 | Laptop | Q1 | 500 |
| 1 | Laptop | Q2 | 600 |
| ...| ... | ... | ... |
| 3 | Keyboard | Q4 | 100 |

ตัวอย่างที่ 4: เปรียบเทียบ Table.Unpivot vs Table.UnpivotOtherColumns
let // ตารางต้นฉบับ Source = Table.FromRecords({ [Store = "A", Manager = "John", Jan = 100, Feb = 120, Mar = 110] }), // วิธีที่ 1: Table.Unpivot - ระบุคอลัมน์ท…
ทั้ง 2 วิธีให้ผลลัพธ์เหมือนกัน แต่เลือกใช้ต่างกันตามสถานการณ์:
– ใช้ Table.Unpivot เมื่อรู้แน่ชัดว่าคอลัมน์ไหนจะ unpivot (เช่น Jan, Feb, Mar)
– ใช้ Table.UnpivotOtherColumns เมื่อคอลัมน์อาจเพิ่มขึ้นในอนาคต (เช่น มี Apr, May เพิ่มมา) 😎
Power Query Formula:

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

Result:

Table 3 แถว:
| Store | Manager | Month | Sales |
|-------|---------|-------|-------|
| A | John | Jan | 100 |
| A | John | Feb | 120 |
| A | John | Mar | 110 |

FAQs

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

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

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

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

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

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

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

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

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

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

Resources & Related

Additional Notes

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

Leave a Reply

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