---
title: Table.ColumnNames – ดึงชื่อคอลัมน์จากตาราง
url: https://www.thepexcel.com/functions/power-query/table-functions/table-columnnames/
type: function-explainer
program: Power Query
syntax: = Table.ColumnNames(table as table) as list
date: 2025-12-03
updated: 2025-12-24
scores:
  popularity: 7
  difficulty: 2
  usefulness: 7
---

# Table.ColumnNames – ดึงชื่อคอลัมน์จากตาราง

> Table.ColumnNames ดึงชื่อคอลัมน์ทั้งหมดจากตาราง และส่งกลับมาเป็นลิสต์ข้อความ ซึ่งจำเป็นสำหรับการตรวจ

## คำอธิบาย

Table.ColumnNames ดึงชื่อคอลัมน์ทั้งหมดจากตาราง และส่งกลับมาเป็นลิสต์ข้อความ ซึ่งจำเป็นสำหรับการตรวจสอบ schema แบบ dynamic

## Syntax

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

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table | Yes | table |  | ตารางที่ต้องการดึงชื่อคอลัมน์ |

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

### ตรวจสอบ Header ของตาราง

ใช้เพื่อดูว่าชื่อคอลัมน์ในตารางถูกต้องตามที่คาดไว้หรือไม่ หรือมีคอลัมน์ที่ไม่รู้จักปะปนมา

### สร้าง Dynamic Column List

ใช้เป็น Input สำหรับฟังก์ชันที่ต้องการ List ของชื่อคอลัมน์ เช่น Table.SelectColumns

## ตัวอย่าง

### 1. ตัวอย่างพื้นฐาน - ดึงชื่อคอลัมน์จากตารางง่ายๆ

```excel
let
    SalesTable = Table.FromRecords({
        [OrderID = 1, CustomerName = "Alice", Amount = 500],
        [OrderID = 2, CustomerName = "Bob", Amount = 1200]
    }),
    ColumnNames = Table.ColumnNames(SalesTable)
in
    ColumnNames
```

**ผลลัพธ์:** `{"OrderID", "CustomerName", "Amount"}`

ดึงชื่อคอลัมน์ทั้งหมดจาก SalesTable และส่งกลับเป็นลิสต์ข้อความ 3 รายการตามลำดับจากซ้ายไปขวา

### 2. ตัวอย่างการนับจำนวนคอลัมน์

```excel
let
    SalesTable = Table.FromRecords({
        [OrderID = 1, CustomerName = "Alice", Amount = 500],
        [OrderID = 2, CustomerName = "Bob", Amount = 1200]
    }),
    ColumnCount = List.Count(Table.ColumnNames(SalesTable))
in
    ColumnCount
```

**ผลลัพธ์:** `3`

นับจำนวนคอลัมน์โดยใช้ Table.ColumnNames ร่วมกับ List.Count เพื่อทำความเข้าใจขนาด schema

### 3. ตัวอย่างการตรวจสอบว่าคอลัมน์มีอยู่หรือไม่

```excel
let
    SalesTable = Table.FromRecords({
        [OrderID = 1, CustomerName = "Alice", Amount = 500],
        [OrderID = 2, CustomerName = "Bob", Amount = 1200]
    }),
    ColumnNames = Table.ColumnNames(SalesTable),
    HasAmountColumn = List.Contains(ColumnNames, "Amount")
in
    HasAmountColumn
```

**ผลลัพธ์:** `true`

ตรวจสอบว่าคอลัมน์ "Amount" มีอยู่ในตารางหรือไม่ โดยใช้ List.Contains ซึ่งมีประโยชน์สำหรับ error handling

### 4. ตัวอย่างการกรองคอลัมน์ที่ขึ้นต้นด้วยตัวอักษรเฉพาะ

```excel
let
    SourceTable = Table.FromRecords({
        [ID = 1, Name = "Alice", Email = "alice@test.com", Phone = "123-4567"],
        [ID = 2, Name = "Bob", Email = "bob@test.com", Phone = "987-6543"]
    }),
    AllColumns = Table.ColumnNames(SourceTable),
    FilteredColumns = List.Select(AllColumns, each Text.StartsWith(_, "E"))
in
    FilteredColumns
```

**ผลลัพธ์:** `{"Email"}`

กรองชื่อคอลัมน์ที่ขึ้นต้นด้วยตัวอักษร "E" เพื่อใช้สำหรับการเลือกคอลัมน์แบบ dynamic

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

- ใช้ Table.ColumnNames ร่วมกับ List.Contains() เพื่อตรวจสอบว่าคอลัมน์มีอยู่หรือไม่ ก่อนที่จะเข้าถึง

- ใช้ List.Count(Table.ColumnNames()) เพื่อนับจำนวนคอลัมน์ มีประโยชน์สำหรับ validation

- Table.ColumnNames มีประโยชน์สำหรับ dynamic transformations ที่ทำงานกับตาราง schema ต่างๆ ไม่ต้องกำหนด schema ล่วงหน้า

- หากต้องการชื่อคอลัมน์เฉพาะชนิด ใช้ Table.ColumnsOfType() แทน

- สามารถใช้ List.Select() ร่วมกับ Table.ColumnNames() เพื่อกรองชื่อคอลัมน์ตามเงื่อนไข

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

**Q: Table.ColumnNames ทำงานกับตาราง empty ได้หรือไม่?**

ได้ครับ มันจะคืนค่าลิสต์ของชื่อคอลัมน์เหมือนเดิม แม้ว่าตารางจะไม่มีแถวข้อมูล เพราะชื่อคอลัมน์ยังคงเป็นส่วนหนึ่งของ schema และไม่ขึ้นกับจำนวนแถว

**Q: ผลลัพธ์ที่ได้เป็นลิสต์เท่านั้น ฉันจะแปลงเป็นตารางได้ไหม?**

ได้ใช้ #table() เพื่อสร้างตารางจากลิสต์ชื่อคอลัมน์ หรือใช้ List.Buffer() สำหรับการจัดการลิสต์ของคอลัมน์ที่มีขนาดใหญ่

**Q: ลำดับของชื่อคอลัมน์ที่ส่งกลับคืออะไร?**

เป็นลำดับที่ปรากฏในตารางจากซ้ายไปขวา ตามลำดับที่กำหนดไว้ในตาราง หากคุณใช้ Table.ReorderColumns() มาก่อน ลำดับก็จะเปลี่ยนไปด้วย

**Q: ต่างกับ Table.ColumnsOfType() อย่างไร?**

Table.ColumnNames() ดึงชื่อคอลัมน์ทั้งหมด ส่วน Table.ColumnsOfType() ดึงเฉพาะชื่อคอลัมน์ที่มีชนิดข้อมูลเฉพาะเท่านั้น เช่น type number หรือ type text

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

- [Microsoft Learn: Table.ColumnNames](https://learn.microsoft.com/en-us/powerquery-m/table-columnnames) _(official)_
- [Power Query M Language Reference](https://learn.microsoft.com/en-us/powerquery-m/) _(official)_

---

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