---
title: Table.ToColumns – แปลงตารางเป็น List ของ List (รายคอลัมน์)
url: https://www.thepexcel.com/functions/power-query/table-functions/table-tocolumns/
type: function-explainer
program: Power Query
syntax: = Table.ToColumns(table as table) as list
date: 2025-12-03
updated: 2025-12-25
scores:
  popularity: 5
  difficulty: 4
  usefulness: 6
---

# Table.ToColumns – แปลงตารางเป็น List ของ List (รายคอลัมน์)

> Table.ToColumns แปลงตาราง (Table) ให้เป็น List ของ List โดยแต่ละ List ย่อยแทนข้อมูลจากคอลัมน์เดียว เ

## คำอธิบาย

Table.ToColumns แปลงตาราง (Table) ให้เป็น List ของ List โดยแต่ละ List ย่อยแทนข้อมูลจากคอลัมน์เดียว เรียงตามลำดับคอลัมน์จากซ้ายไปขวา ส่วนกลับกันของ Table.FromColumns

## Syntax

```excel
= Table.ToColumns(table as table) as list
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table | Yes | table |  | ตารางที่ต้องการแปลงให้เป็น List ของ List |

## เคสการใช้งาน

### ประมวลผลข้อมูลรายคอลัมน์

ใช้ร่วมกับ List.Transform เพื่อแปลงค่าในแต่ละคอลัมน์ทีละคอลัมน์

### ใช้กับ List.Zip

ใช้เตรียมข้อมูลสำหรับ List.Zip เพื่อรวมคอลัมน์ต่างๆ เข้าด้วยกัน

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: แปลงตารางพื้นฐาน

```excel
let
    SampleTable = Table.FromRecords({
        [CustomerID = 1, Name = "Bob", Amount = 100],
        [CustomerID = 2, Name = "Jim", Amount = 200]
    }),
    Result = Table.ToColumns(SampleTable)
in
    Result
```

**ผลลัพธ์:** `{{1, 2}, {"Bob", "Jim"}, {100, 200}}`

ตาราง 2 แถว 3 คอลัมน์ ถูกแปลงเป็น List ของ 3 Lists ย่อย คอลัมน์ที่ 1 คือ {1, 2}, คอลัมน์ที่ 2 คือ {"Bob", "Jim"}, คอลัมน์ที่ 3 คือ {100, 200}

### 2. ตัวอย่างที่ 2: ใช้กับตาราง Source และเข้าถึง List แต่ละตัว

```excel
let
    Source = Table.FromRows(
        {{"Alice", 1500}, {"Bob", 2000}, {"Charlie", 1800}},
        {"Employee", "Salary"}
    ),
    ColumnsList = Table.ToColumns(Source),
    FirstColumn = ColumnsList{0},
    SecondColumn = ColumnsList{1}
in
    ColumnsList
```

**ผลลัพธ์:** `{{"Alice", "Bob", "Charlie"}, {1500, 2000, 1800}}`

แปลง Source ทั้งหมดเป็น List ของ Lists จากนั้นสามารถเข้าถึง List แต่ละรายการด้วย index {0}, {1} เป็นต้น เหมือนกับการใช้ array indexing

### 3. ตัวอย่างที่ 3: ประมวลผลเฉพาะคอลัมน์ด้วย List.Transform

```excel
let
    Source = Table.FromRecords({
        [ID = 1, Value = 10],
        [ID = 2, Value = 20],
        [ID = 3, Value = 30]
    }),
    ColumnsList = Table.ToColumns(Source),
    SecondColumnDoubled = List.Transform(ColumnsList{1}, each _ * 2),
    Result = {ColumnsList{0}, SecondColumnDoubled}
in
    Result
```

**ผลลัพธ์:** `{{1, 2, 3}, {20, 40, 60}}`

ดึงคอลัมน์ที่ 2 จาก List ของ Lists แล้วคูณแต่ละค่าด้วย 2 โดยใช้ List.Transform กับ each keyword เพื่อสร้าง dynamic transformation

### 4. ตัวอย่างที่ 4: นับจำนวนคอลัมน์และแถว

```excel
let
    Source = Table.FromRecords({
        [A = 1, B = 2, C = 3],
        [A = 4, B = 5, C = 6],
        [A = 7, B = 8, C = 9]
    }),
    ColumnsList = Table.ToColumns(Source),
    ColumnCount = List.Count(ColumnsList),
    RowCount = List.Count(ColumnsList{0})
in
    {ColumnCount, RowCount}
```

**ผลลัพธ์:** `{3, 3}`

นับจำนวนคอลัมน์ด้วย List.Count ของผลลัพธ์ทั้งหมด (3 Lists = 3 คอลัมน์) และนับจำนวนแถวด้วย List.Count ของคอลัมน์แรก (3 ค่า = 3 แถว)

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

- ผมแนะนำให้ใช้ List.Count(Table.ToColumns(table)) เพื่อหาจำนวนคอลัมน์ได้อย่างรวดเร็ว

- ถ้าต้องการหาจำนวนแถว ใช้ List.Count(Table.ToColumns(table){0}) เพื่อนับสมาชิกของคอลัมน์แรก

- ส่วนตัวผมชอบใช้ Table.ToColumns แล้วคูณกับ Table.FromColumns เพื่อทำ deep copy ของตาราง บ่อยครั้งมีประโยชน์มากสำหรับการ backup

- คล่องตัวกับการใช้ {} (curly braces) เพื่อเข้าถึง List items เพราะผลลัพธ์เป็น List ของ List ขึ้นมา

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

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

Table.ToColumns แปลงข้อมูลรายคอลัมน์ (ผลลัพธ์เป็น List ของคอลัมน์) ส่วน Table.ToRows แปลงข้อมูลรายแถว (ผลลัพธ์เป็น List ของแถว) ถ้าตาราง 3 แถว 2 คอลัมน์ ToColumns ให้ {2 Lists ของแถวละ 3 ค่า} แต่ ToRows ให้ {3 Lists ของแถวละ 2 ค่า}

**Q: สามารถแปลงกลับเป็นตารางได้ไหม?**

ได้ครับ ใช้ Table.FromColumns เพื่อแปลง List ของ List กลับไปเป็นตาราง ผมมักจะใช้ workflow นี้เวลาต้องประมวลผลข้อมูลแบบ column-by-column แล้วแปลงกลับเป็นตารางเหมือนเดิม

**Q: ถ้าตารางว่าง (0 แถว) จะเกิดอะไร?**

ยังคงได้ List ของ Lists ที่ว่างเปล่า ตัวอย่าง: Table.ToColumns(Table.FromRecords({})) → {} ไม่มี List ย่อยเลยครับ เพราะไม่มีคอลัมน์ที่จะแปลง

**Q: ใช้ Table.ToColumns แบบไหนถึง optimize ได้?**

ปัญหาคือถ้าตารางใหญ่มากๆ การแปลงเป็น List จะใช้ memory เยอะ ผมแนะนำให้กรอง (filter) ก่อนแปลง หรือใช้ List functions แบบ lazy evaluation ถ้าเป็นไปได้

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

- [List.Transform – แปลงค่าในลิสต์แต่ละตัว](https://www.thepexcel.com/functions/power-query/list-functions/list-transform/)
- [Table.FromColumns – สร้างตารางจากลิสต์ของคอลัมน์](https://www.thepexcel.com/functions/power-query/table-functions/table-fromcolumns/)
- [Table.ToRows – แปลงตารางเป็น List ของ List (รายแถว)](https://www.thepexcel.com/functions/power-query/table-functions/table-torows/)

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

- [Microsoft Learn: Table.ToColumns](https://learn.microsoft.com/en-us/powerquery-m/table-tocolumns) _(official)_
- [PowerQuery.how - Table.ToColumns](https://powerquery.how/) _(article)_
- [Microsoft Learn: Table Functions](https://learn.microsoft.com/en-us/powerquery-m/table-table) _(official)_

---

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