---
title: Table.Pivot – หมุนข้อมูลจากแนวตั้งเป็นแนวนอน
url: https://www.thepexcel.com/functions/power-query/table-functions/table-pivot/
type: function-explainer
program: Power Query
syntax: "= Table.Pivot(table, pivotValues, attributeColumn, valueColumn, [aggregationFunction])"
date: 2025-12-03
updated: 2025-12-20
scores:
  popularity: 7
  difficulty: 5
  usefulness: 8
---

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

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

## คำอธิบาย

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

## Syntax

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

## Arguments

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

## ตัวอย่าง

### 1. Pivot พื้นฐาน - แปลง Attribute เป็นคอลัมน์

```excel
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`

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

### 2. Pivot ยอดขายรายเดือน

```excel
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`

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

### 3. Pivot กับ Aggregation Function - จัดการค่าซ้ำ

```excel
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`

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

### 4. Pivot Dynamic - สกัด pivotValues จากข้อมูล

```excel
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`

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

## หมายเหตุเพิ่มเติม

- ใช้ List.Distinct() บน attributeColumn เพื่อให้ pivotValues เป็น dynamic = Table.Pivot(Source, List.Distinct(Source[Month]), "Month", "Sales") ไม่ต้องฮาร์ดโค้ด

- ก่อน Pivot ให้ลองใช้ Table.FirstN(Source, 5) เพื่อตรวจสอบรูป long format ว่าถูกต้องหรือไม่

- หลังจาก Pivot ถ้า pivot values type ยังเป็น "any" ให้ใช้ Table.TransformColumnTypes เพื่อแปลงเป็นชนิดข้อมูลที่ต้องการ

- ถ้าข้อมูล pivotValues มี special characters หรือ space ให้ใช้ Table.RenameColumns ทีหลังเพื่อให้ชื่อคอลัมน์ใช้งานได้สะดวก

- เรียงลำดับ pivotValues ตามที่ต้องการ เพราะ Table.Pivot จะรักษาลำดับที่ระบุไว้ - ก่อน Pivot ให้ sort attribute column

## คำถามที่พบบ่อย

**Q: ต่างจาก Table.Unpivot อย่างไร?**

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

**Q: Row key คืออะไร?**

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

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

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

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

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

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

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

## ฟังก์ชันที่เกี่ยวข้อง

- [Table.Unpivot – แปลงคอลัมน์ให้เป็นแถว (Unpivot)](https://www.thepexcel.com/functions/power-query/table-functions/table-unpivot/)
- [Table.Group – จัดกลุ่มและสรุปผลข้อมูล (Group By)](https://www.thepexcel.com/functions/power-query/table-functions/table-group/)
- [Table.Transpose – สลับแกนตาราง (Transpose)](https://www.thepexcel.com/functions/power-query/table-functions/table-transpose/)
- [Table.Sort – เรียงลำดับข้อมูลในตาราง](https://www.thepexcel.com/functions/power-query/table-functions/table-sort/)
- [List.Distinct – ลบค่าซ้ำออกจาก List](https://www.thepexcel.com/functions/power-query/list-functions/list-distinct/)

## แหล่งข้อมูลเพิ่มเติม

- [Table.Pivot - Microsoft Learn (Official)](https://learn.microsoft.com/en-us/powerquery-m/table-pivot) _(official)_
- [Power Query M Function Reference](https://learn.microsoft.com/en-us/powerquery-m/) _(official)_
- [Power Query - Table Functions](https://learn.microsoft.com/en-us/powerquery-m/table) _(official)_

---

_Source: [https://www.thepexcel.com/functions/power-query/table-functions/table-pivot/](https://www.thepexcel.com/functions/power-query/table-functions/table-pivot/)_
