Table.Pivot แปลงข้อมูล long format (แนวตั้ง) เป็น wide format (แนวนอน) โดยหมุนค่า attribute ให้เป็นคอลัมน์ใหม่
= Table.Pivot(table, pivotValues, attributeColumn, valueColumn, [aggregationFunction])
= Table.Pivot(table, pivotValues, attributeColumn, valueColumn, [aggregationFunction])
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| table | Table | Yes | ตารางข้อมูลต้นทาง – โดยปกติจะเป็นข้อมูลในรูป long format ที่มีคอลัมน์ attribute และ valueColumn | |
| pivotValues | List | Yes | รายการค่า (list) ที่สกัดมาจาก attributeColumn เพื่อหมุนให้เป็นคอลัมน์ใหม่ เช่น {“Jan”, “Feb”, “Mar”} | |
| attributeColumn | Text | Yes | ชื่อคอลัมน์ (text) ที่มีค่า attribute ที่ต้องการหมุน เช่น “Month” หรือ “Category” | |
| valueColumn | Text | Yes | ชื่อคอลัมน์ (text) ที่มีค่าตัวเลข/ข้อมูล ที่จะใส่ลงไปในคอลัมน์ใหม่ เช่น “Sales” หรือ “Amount” | |
| aggregationFunction | Function | Optional | null | ฟังก์ชันรวมข้อมูล (optional) เช่น List.Sum, List.Max, List.Min – ใช้เมื่อ attributeColumn มีค่าซ้ำกันเพื่อกำหนดวิธีการจัดการ |
let Source = Table.FromRecords({ [key = "x", attribute = "a", value = 1], [key = "x", attribute = "c", value = 3], [key = "y", attribute = "a", value = 2], [key…let
Source = Table.FromRecords({
[key = "x", attribute = "a", value = 1],
[key = "x", attribute = "c", value = 3],
[key = "y", attribute = "a", value = 2],
[key = "y", attribute = "b", value = 4]
}),
Pivot = Table.Pivot(
Source,
{"a", "b", "c"},
"attribute",
"value"
)
in
Pivot
key | a | b | c
----|---|------|---
x | 1 | null | 3
y | 2 | 4 | null
let Source = Table.FromRecords({ [Product = "iPhone", Month = "Jan", Sales = 500], [Product = "iPhone", Month = "Feb", Sales = 620], [Product = "iPad", Month =…let
Source = Table.FromRecords({
[Product = "iPhone", Month = "Jan", Sales = 500],
[Product = "iPhone", Month = "Feb", Sales = 620],
[Product = "iPad", Month = "Jan", Sales = 300],
[Product = "iPad", Month = "Feb", Sales = 350]
}),
PivotMonths = Table.Pivot(
Source,
{"Jan", "Feb"},
"Month",
"Sales"
)
in
PivotMonths
Product | Jan | Feb
--------|-----|-----
iPhone | 500 | 620
iPad | 300 | 350
let Source = Table.FromRecords({ [Region = "North", Category = "A", Value = 100], [Region = "North", Category = "A", Value = 150], [Region = "North", Category =…let
Source = Table.FromRecords({
[Region = "North", Category = "A", Value = 100],
[Region = "North", Category = "A", Value = 150],
[Region = "North", Category = "B", Value = 200],
[Region = "South", Category = "A", Value = 120],
[Region = "South", Category = "B", Value = 180]
}),
PivotWithSum = Table.Pivot(
Source,
{"A", "B"},
"Category",
"Value",
List.Sum
)
in
PivotWithSum
Region | A | B
-------|-----|-----
North | 250 | 200
South | 120 | 180
let Source = Table.FromRecords({ [Employee = "Alice", Metric = "Sales", Value = 50000], [Employee = "Alice", Metric = "Tasks", Value = 120], [Employee = "Bob",…let
Source = Table.FromRecords({
[Employee = "Alice", Metric = "Sales", Value = 50000],
[Employee = "Alice", Metric = "Tasks", Value = 120],
[Employee = "Bob", Metric = "Sales", Value = 45000],
[Employee = "Bob", Metric = "Tasks", Value = 95]
}),
UniqueMetrics = List.Distinct(Source[Metric]),
PivotDynamic = Table.Pivot(
Source,
UniqueMetrics,
"Metric",
"Value"
)
in
PivotDynamic
Employee | Sales | Tasks
---------|-------|-------
Alice | 50000 | 120
Bob | 45000 | 95
Table.Pivot แปลงจากแนวตั้ง (long) เป็นแนวนอน (wide) ส่วน Table.Unpivot ทำสิ่งตรงกันข้าม – แปลงจากแนวนอนเป็นแนวตั้ง ถ้า Pivot คือ “ม้วนเข้า” Unpivot ก็คือ “เปิดออก” ครับ
Row key คือคอลัมน์ที่ใช้ระบุแถวแต่ละแถว (ไม่นับ attributeColumn และ valueColumn) เช่นถ้ามี Product, Month, Sales แล้ว pivot Month เป็นคอลัมน์ Product จะเป็น row key ได้หลายคอลัมน์ด้วยครับ
ระบุเฉพาะค่าที่ต้องการหมุน ไม่จำเป็นต้องครบทั้งหมด ค่าที่ไม่ระบุจะหายไป ถ้าต้องให้ dynamic ใช้ List.Distinct(Source[attributeColumn]) แทน
เมื่อการผสมระหว่าง row key กับ attribute value นั้นไม่มีอยู่ในข้อมูลต้นทาง Table.Pivot จะใส่ null โดยอัตโนมัติ ถ้าต้องแทนด้วยค่าอื่น เช่น 0 ให้ใช้ Table.ReplaceValue หลังจาก Pivot
ขึ้นอยู่กับสถานการณ์ – List.Sum ใช้ได้กับข้อมูลตัวเลขที่ต้องรวม, List.Max สำหรับค่าสูงสุด, List.First สำหรับเอาค่าแรก ส่วนใหญ่ List.Sum ใช้บ่อยที่สุด
Table.Pivot(table, pivotValues, attributeColumn, valueColumn, [aggregationFunction])
ส่วนตัวผมคิดว่า Table.Pivot เป็นหนึ่งในฟังก์ชันที่เข้าใจยากที่สุดใน Power Query เพราะความสัมพันธ์ระหว่างพารามิเตอร์นั้นค่อนข้างนามธรรม แต่พอเข้าใจแล้ว มันจะเป็นเครื่องมือที่แรงมากครับ
ที่เจ๋งคือ Table.Pivot ทำให้เราแปลงข้อมูลจากรูป long format (เช่น ข้อมูลยอดขายที่บันทึกแบบ 1 แถวต่อ 1 เดือน) เป็น wide format (แต่ละเดือนเป็นหนึ่งคอลัมน์) ได้อย่างลื่นไหล โดยเพียงระบุว่า “attribute column ไหนที่จะหมุน” และ “value column ไหนที่จะใส่ลงไป” ก็เสร็จแล้ว
ส่วนตัวผม มักใช้ Table.Pivot คู่กับ List.Distinct เพื่อให้ pivotValues เป็น dynamic – ไม่ต้องฮาร์ดโค้ดค่า attribute ทั้งหมด ส่วนถ้า attribute value มีค่าซ้ำ ใช้ aggregationFunction (เช่น List.Sum หรือ List.Max) เพื่อบอก Power Query ว่าควรทำอย่างไร ซึ่งช่วยให้โค้ดยืดหยุ่นมากเลยครับ 😎