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
=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) |
แปลงข้อมูลยอดขายรายเดือนที่มีคอลัมน์ Jan, Feb, Mar… ให้กลายเป็นคอลัมน์ Month และ Sales เพื่อใช้ใน Data Model
แปลงข้อมูลที่ออกแบบมาให้อ่านง่าย (Wide Format) ให้อยู่ในรูปแบบที่เหมาะสำหรับ Database หรือ Analysis
แปลงข้อมูลให้อยู่ในรูปแบบ Long Format ก่อนนำไปสร้าง Pivot Table ใน Excel
let // สร้างตารางยอดขายรายปี (Wide Format) Source = Table.FromRecords({ [Region = "North", Y2022 = 100, Y2023 = 120], [Region = "South", Y2022 = 200, Y2023 = 25…=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 |
let // ตารางที่มีค่า null บางช่อง Source = Table.FromRecords({ [Product = "Apple", Jan = 10, Feb = null, Mar = 30], [Product = "Banana", Jan = 20, Feb = 25, Mar…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 |
let // ข้อมูลยอดขายรายไตรมาส (แบบที่เห็นบ่อยใน Excel) Source = Table.FromRecords({ [ID = 1, Product = "Laptop", Q1 = 500, Q2 = 600, Q3 = 550, Q4 = 700], [ID = 2…=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 |
let // ตารางต้นฉบับ Source = Table.FromRecords({ [Store = "A", Manager = "John", Jan = 100, Feb = 120, Mar = 110] }), // วิธีที่ 1: Table.Unpivot - ระบุคอลัมน์ท…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 |
Table.Unpivot ต้องระบุคอลัมน์ที่จะ unpivot โดยตรง ส่วน Table.UnpivotOtherColumns ต้องระบุคอลัมน์ที่จะ keep ไว้แทน ถ้าข้อมูลมีคอลัมน์เพิ่มมาใหม่บ่อยๆ (เช่น เดือนใหม่) แนะนำใช้ UnpivotOtherColumns ครับ เพราะไม่ต้องมาแก้ code ทุกครั้ง
Table.Unpivot จะข้ามค่า null โดยอัตโนมัติครับ ถ้าต้องการเก็บ null ไว้ด้วย ให้เปลี่ยนค่า null เป็นค่าอื่นก่อน (เช่น 0 หรือ “N/A”) ด้วย Table.ReplaceValue แล้วค่อย unpivot
ใช้ฟังก์ชัน Table.Pivot ครับ โดยระบุ Attribute Column เป็นคอลัมน์ที่จะกระจายเป็น header และ Value Column เป็นค่าที่จะใส่ในตาราง ถ้าต้องการ aggregate ค่า ให้ใส่ aggregation function เป็น argument สุดท้าย
ใช้ Table.ReorderColumns หลัง unpivot เพื่อจัดลำดับคอลัมน์ใหม่ได้ครับ เช่น Table.ReorderColumns(Unpivoted, {“ID”, “Name”, “Attribute”, “Value”})
ได้ครับ แต่ค่าใน Value Column จะถูกแปลงเป็น type any ทำให้ต้องมา cast type ใหม่ทีหลัง แนะนำให้ unpivot คอลัมน์ที่มี type เดียวกันจะดีกว่า
Table.Unpivot ใช้สำหรับแปลงข้อมูลจากรูปแบบ Wide (หลายคอลัมน์) ให้เป็นรูปแบบ Long (หลายแถว) โดยจะนำชื่อคอลัมน์ที่ระบุมาเก็บไว้ใน Attribute Column และนำค่าในคอลัมน์เหล่านั้นมาเก็บไว้ใน Value Column
.
ที่เจ๋งคือฟังก์ชันนี้จะ skip ค่า null ออกไปโดยอัตโนมัติ ทำให้ข้อมูลที่ได้สะอาดและพร้อมใช้งานทันที ไม่ต้องมา filter ค่าว่างทีหลังครับ
.
ส่วนตัวผมใช้ Table.Unpivot บ่อยมากตอนต้องเตรียมข้อมูลสำหรับ Power BI หรือทำ Pivot Table เพราะ Data Model ส่วนใหญ่ต้องการข้อมูลแบบ Long Format ถึงจะทำงานได้ดี 😎