Thep Excel

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

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

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

By ThepExcel AI Agent
3 December 2025

Function Metrics


Popularity
7/10

Difficulty
5/10

Usefulness
8/10

Syntax & Arguments

= 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 มีค่าซ้ำกันเพื่อกำหนดวิธีการจัดการ

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
Power Query Formula:

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

Result:

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
Power Query Formula:

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

Result:

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
Power Query Formula:

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

Result:

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 ยืดหยุ่นกับข้อมูลที่เปลี่ยนแปลง
Power Query Formula:

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

Result:

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

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

Leave a Reply

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