Table.UnpivotOtherColumns แปลงคอลัมน์ที่ไม่ได้ระบุทั้งหมดให้กลายเป็นแถว (Unpivot) โดยระบุเฉพาะคอลัมน์ที่ต้องการคงไว้ ข้อดีคือเป็น Dynamic ถ้ามีคอลัมน์ใหม่เพิ่มเข้ามาในอนาคตก็จะถูก Unpivot ให้อัตโนมัติ เหมาะสำหรับแปลงข้อมูลจาก Wide Format (หลายคอลัมน์) ให้เป็น Long Format (หลายแถว) สำหรับ PivotTable และ Database
=Table.UnpivotOtherColumns(table as table, pivotColumns as list, attributeColumn as text, valueColumn as text) as table
=Table.UnpivotOtherColumns(table as table, pivotColumns as list, attributeColumn as text, valueColumn as text) as table
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| table | table | Yes | ตารางข้อมูลที่ต้องการ Unpivot | |
| pivotColumns | list | Yes | รายชื่อคอลัมน์ที่ต้องการ “เก็บไว้” ไม่ต้องการ Unpivot (คอลัมน์คงที่ เช่น ID, Region, Product) | |
| attributeColumn | text | Yes | ชื่อคอลัมน์ใหม่สำหรับเก็บชื่อหัวคอลัมน์เดิมที่ถูก Unpivot (เช่น “Month”, “Year”, “Attribute”) | |
| valueColumn | text | Yes | ชื่อคอลัมน์ใหม่สำหรับเก็บค่าข้อมูลจากคอลัมน์ที่ถูก Unpivot (เช่น “Sales”, “Amount”, “Value”) |
ข้อมูลจาก Excel มักมีคอลัมน์ Jan, Feb, Mar… ใช้ฟังก์ชันนี้แปลงเป็นคอลัมน์ Month และ Sales เพื่อนำไป PivotTable หรือวิเคราะห์ใน Power BI
เมื่อมีคอลัมน์เดือนหรือปีใหม่เพิ่มเข้ามาในข้อมูลต้นทาง ฟังก์ชันนี้จะ Unpivot คอลัมน์ใหม่ให้อัตโนมัติโดยไม่ต้องแก้โค้ด
แปลงข้อมูลจาก Wide Format (Spreadsheet style) เป็น Long Format (Database style) เพื่อนำเข้า SQL Server หรือ Data Warehouse
let // สร้างตารางยอดขายที่มีคอลัมน์เป็นเดือน Source = Table.FromRecords({ [Product = "iPhone", Jan = 100, Feb = 120, Mar = 150], [Product = "iPad", Jan = 50, Fe…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)
let // ตารางยอดขายตาม Region และ Product Source = Table.FromRecords({ [Region = "North", Product = "Apple", Q1 = 1000, Q2 = 1200, Q3 = 1100], [Region = "South",…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)
let // สมมติข้อมูลปีหน้ามีเดือน Apr เพิ่มมา Source = Table.FromRecords({ [ID = 1, Name = "John", Jan = 10, Feb = 20, Mar = 30, Apr = 40], [ID = 2, Name = "Jane"…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)
let Source = Table.FromRecords({ [Product = "Laptop", Year2022 = 500, Year2023 = 600, Year2024 = 700] }), // วิธี 1: Table.Unpivot - ต้องระบุคอลัมน์ที่จะ Unpivo…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)
Table.Unpivot ต้องระบุชื่อคอลัมน์ที่จะ Unpivot โดยตรง (hardcode) ส่วน Table.UnpivotOtherColumns ระบุแค่คอลัมน์ที่จะ “เก็บไว้” แล้วคอลัมน์อื่นๆ ทั้งหมดจะถูก Unpivot อัตโนมัติ ข้อดีคือถ้ามีคอลัมน์ใหม่เพิ่มเข้ามา (เช่น เดือนใหม่) จะถูก Unpivot ให้โดยไม่ต้องแก้โค้ดครับ
ค่า null จะถูกลบออกไปโดยอัตโนมัติระหว่าง Unpivot ถ้าต้องการเก็บ null ไว้ด้วย ต้องแปลง null เป็นค่าอื่นก่อน (เช่น 0 หรือ text ว่าง) แล้วค่อย Unpivot ครับ
เลือกคอลัมน์ที่ต้องการเก็บไว้ (กด Ctrl+Click) จากนั้นคลิกขวาแล้วเลือก “Unpivot Other Columns” ระบบจะสร้างโค้ด Table.UnpivotOtherColumns ให้อัตโนมัติครับ
ปัญหานี้เจอบ่อยครับ 😅 สาเหตุหลักคือ cell ว่างหรือ null ถูกลบออกไป ให้เช็คข้อมูลต้นทางว่ามี null หรือเปล่า และถ้าต้องการเก็บ null ไว้ ให้ใช้ Table.ReplaceValue แปลง null เป็นค่าอื่นก่อน Unpivot ครับ
ใช้เมื่อต้องการแปลง Wide Format เป็น Long Format และข้อมูลอาจมีคอลัมน์เพิ่มในอนาคต ไม่ควรใช้เมื่อต้องการ Unpivot เฉพาะบางคอลัมน์เท่านั้น (ใช้ Table.Unpivot แทน) หรือเมื่อข้อมูลต้นทางมี Data Type ไม่ตรงกันในแต่ละคอลัมน์ครับ
Table.UnpivotOtherColumns ใช้แปลงคอลัมน์ทั้งหมดที่ไม่ได้ระบุให้กลายเป็นแถว (Unpivot) โดยเราระบุแค่คอลัมน์ที่ต้องการ “เก็บไว้” ส่วนคอลัมน์อื่นๆ ที่เหลือจะถูก Unpivot ให้อัตโนมัติ
.
ที่เจ๋งคือฟังก์ชันนี้เป็น Dynamic หมายความว่าถ้ามีคอลัมน์ใหม่เพิ่มเข้ามาในอนาคต เช่น เพิ่มเดือน April หรือปี 2025 เข้ามา คอลัมน์เหล่านั้นจะถูก Unpivot ให้อัตโนมัติโดยไม่ต้องแก้โค้ดเลยครับ
.
ส่วนตัวผมใช้ฟังก์ชันนี้บ่อยมากตอนทำ Data Transformation เพราะข้อมูลจาก Excel มักจะมาในรูปแบบ Wide Format (คอลัมน์เยอะ) แต่ Database และ PivotTable ต้องการ Long Format (แถวเยอะ) ฟังก์ชันนี้ช่วยแปลงได้ทันทีครับ 😎