Thep Excel

Table.Pivotหมุนข้อมูลจากแนวตั้งเป็นแนวนอน

Table.Pivot แปลงข้อมูล long format (แนวตั้ง) เป็น wide format (แนวนอน) โดยหมุนค่า attribute ให้เป็นคอลัมน์ใหม่

fx
= Table.Pivot(table, pivotValues, attributeColumn, valueColumn, [aggregationFunction])

ความนิยม7/10
ความยาก5/10
ประโยชน์8/10

อัปเดต 3 December 2025

Syntax & Arguments

อาร์กิวเมนต์ ชนิด ค่าเริ่มต้น คำอธิบาย
table Table ตารางข้อมูลต้นทาง – โดยปกติจะเป็นข้อมูลในรูป long format ที่มีคอลัมน์ attribute และ valueColumn
pivotValues List รายการค่า (list) ที่สกัดมาจาก attributeColumn เพื่อหมุนให้เป็นคอลัมน์ใหม่ เช่น {“Jan”, “Feb”, “Mar”}
attributeColumn Text ชื่อคอลัมน์ (text) ที่มีค่า attribute ที่ต้องการหมุน เช่น “Month” หรือ “Category”
valueColumn Text ชื่อคอลัมน์ (text) ที่มีค่าตัวเลข/ข้อมูล ที่จะใส่ลงไปในคอลัมน์ใหม่ เช่น “Sales” หรือ “Amount”
[aggregationFunction]ไม่บังคับ Function null ฟังก์ชันรวมข้อมูล (optional) เช่น List.Sum, List.Max, List.Min – ใช้เมื่อ attributeColumn มีค่าซ้ำกันเพื่อกำหนดวิธีการจัดการ

[ ] = อาร์กิวเมนต์ที่ไม่บังคับ

Additional Notes

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 ว่าควรทำอย่างไร ซึ่งช่วยให้โค้ดยืดหยุ่นมากเลยครับ 😎

Examples

Pivot พื้นฐาน – แปลง Attribute เป็นคอลัมน์let Source = Table.FromRecords({ [key = "x", attribute = "a", value = 1], [key = "x", attribute = "c", value = 3], [key = "y", attribute = "a", value = 2], [key…

แปลง attribute column (a, b, c) เป็นคอลัมน์ใหม่ คอลัมน์ key ยังคงเป็น row key ค่าที่ไม่มีการจับคู่จะเป็น null

fx
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
Pivot ยอดขายรายเดือนlet Source = Table.FromRecords({ [Product = "iPhone", Month = "Jan", Sales = 500], [Product = "iPhone", Month = "Feb", Sales = 620], [Product = "iPad", Month =…

แปลงข้อมูลยอดขายจากรูป long format (เดือนเป็นแถว) เป็น wide format (เดือนเป็นคอลัมน์) Product เป็น row key

fx
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
Pivot กับ Aggregation Function – จัดการค่าซ้ำlet Source = Table.FromRecords({ [Region = "North", Category = "A", Value = 100], [Region = "North", Category = "A", Value = 150], [Region = "North", Category =…

ใช้ List.Sum เพื่อรวมค่า A ของ North (100+150=250) เมื่อ category ซ้ำกัน Region เป็น row key

fx
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
Pivot Dynamic – สกัด pivotValues จากข้อมูลlet Source = Table.FromRecords({ [Employee = "Alice", Metric = "Sales", Value = 50000], [Employee = "Alice", Metric = "Tasks", Value = 120], [Employee = "Bob",…

ใช้ List.Distinct สกัดค่า unique จาก Metric column โดยอัตโนมัติ – ไม่ต้องฮาร์ดโค้ด pivotValues ทำให้ query ยืดหยุ่นกับข้อมูลที่เปลี่ยนแปลง

fx
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

FAQs

ต่างจาก Table.Unpivot อย่างไร?+

Table.Pivot แปลงจากแนวตั้ง (long) เป็นแนวนอน (wide) ส่วน Table.Unpivot ทำสิ่งตรงกันข้าม – แปลงจากแนวนอนเป็นแนวตั้ง ถ้า Pivot คือ "ม้วนเข้า" Unpivot ก็คือ "เปิดออก" ครับ

Row key คืออะไร?+

Row key คือคอลัมน์ที่ใช้ระบุแถวแต่ละแถว (ไม่นับ attributeColumn และ valueColumn) เช่นถ้ามี Product, Month, Sales แล้ว pivot Month เป็นคอลัมน์ Product จะเป็น row key ได้หลายคอลัมน์ด้วยครับ

ต้องระบุ pivotValues ทั้งหมด หรือระบุได้บางส่วน?+

ระบุเฉพาะค่าที่ต้องการหมุน ไม่จำเป็นต้องครบทั้งหมด ค่าที่ไม่ระบุจะหายไป ถ้าต้องให้ dynamic ใช้ List.Distinct(Source[attributeColumn]) แทน

Null value จากการแปลง Pivot ทำไมถึงมี?+

เมื่อการผสมระหว่าง row key กับ attribute value นั้นไม่มีอยู่ในข้อมูลต้นทาง Table.Pivot จะใส่ null โดยอัตโนมัติ ถ้าต้องแทนด้วยค่าอื่น เช่น 0 ให้ใช้ Table.ReplaceValue หลังจาก Pivot

aggregationFunction ควรใช้ตัวไหนบ่อย?+

ขึ้นอยู่กับสถานการณ์ – List.Sum ใช้ได้กับข้อมูลตัวเลขที่ต้องรวม, List.Max สำหรับค่าสูงสุด, List.First สำหรับเอาค่าแรก ส่วนใหญ่ List.Sum ใช้บ่อยที่สุด

Resources & Related

ฟังก์ชันที่ผู้เขียนโยงไว้กับ Table.Pivot จัดกลุ่มตามหมวด · ชี้ที่ชื่อใดชื่อหนึ่ง แล้วฟังก์ชันที่ใช้คู่กันจะมีจุดทอง

ฟังก์ชันนี้Table.PivotTable functions
Table functions (5)
Table.Group

Table.Group จัดกลุ่มข้อมูลตามคอลัมน์ที่กำหนดและสรุปผลในแต่ละกลุ่มด้วย aggregation functions เช่น List.Sum, List.Count, List.Average คล้ายกับ GROUP BY ใน SQL แต่ทรงพลังกว่าเพราะสามารถจัดกลุ่มหลายคอลัมน์พร้อมกันและสร้างคอลัมน์สรุปผลหลายคอลัมน์ในคำสั่งเดียว รองรับ GroupKind.Local เพื่อเพิ่มประสิทธิภาพเมื่อข้อมูลเรียงลำดับแล้ว

Table.Sort

เรียงลำดับข้อมูลในตารางตามคอลัมน์ที่ระบุ รองรับการเรียงหลายระดับและกำหนดทิศทางการเรียง

Table.Transpose

Table.Transpose จะกลับด้านตาราง คล้ายกับการ Copy > Paste Special > Transpose ใน Excel โดยข้อมูลในแถวที่ 1 จะกลายเป็นคอลัมน์ที่ 1 แถวที่ 2 เป็นคอลัมน์ที่ 2 ไปเรื่อยๆ

Table.Unpivot

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

Table.UnpivotOtherColumns

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

Comments

อีเมลของคุณจะไม่ถูกเผยแพร่