Thep Excel

Table.AddColumn – เพิ่มคอลัมน์ใหม่ด้วยสูตร

เพิ่มคอลัมน์ใหม่จากการคำนวณสูตร

Table.AddColumn จะสร้างคอลัมน์ใหม่ต่อท้ายตารางเดิม โดยใช้ฟังก์ชันที่กำหนด (columnGenerator) คำนวณค่าสำหรับแต่ละแถว ซึ่งสามารถอ้างอิงค่าจากคอลัมน์อื่นๆ ในแถวเดียวกันได้ (โดยใช้ [ColumnName]) และสามารถระบุชนิดข้อมูล (Type) ของคอลัมน์ใหม่ได้ด้วย

=Table.AddColumn(table as table, newColumnName as text, columnGenerator as function, optional columnType as nullable type) as table

By ThepExcel AI Agent
3 December 2025

Syntax & Arguments

=Table.AddColumn(table as table, newColumnName as text, columnGenerator as function, optional columnType as nullable type) as table

Argument Type Required Default Description
table table Yes ตารางที่ต้องการเพิ่มคอลัมน์
newColumnName text Yes ชื่อของคอลัมน์ใหม่ที่จะสร้าง
columnGenerator function Yes สูตรการคำนวณค่าในคอลัมน์ใหม่ (มักใช้ ‘each …’)
columnType type Optional null ชนิดข้อมูลของคอลัมน์ใหม่ เช่น type text, type number, type date

How it works

คำนวณกำไร

สร้างคอลัมน์ Profit โดยนำ [Sales] – [Cost]

รวมชื่อ-นามสกุล

สร้างคอลัมน์ FullName โดยนำ [FirstName] & " " & [LastName]

สร้างคอลัมน์เงื่อนไข (Conditional Column)

ถ้า [Score] >= 50 ให้แสดง "Pass" ถ้าไม่ใช่ให้แสดง "Fail"

Examples

ตัวอย่างที่ 1: คำนวณราคารวม

สร้างคอลัมน์ใหม่ชื่อ TotalPrice โดยนำราคาคูณจำนวนสินค้าในแต่ละแถว
Power Query Formula:

=Table.AddColumn(Source, "TotalPrice", each [Price] * [Quantity])

Result:

ตารางที่มีคอลัมน์ TotalPrice เพิ่มเข้ามา

ตัวอย่างที่ 2: ระบุชนิดข้อมูล

ดึงปีจากวันที่ และระบุว่าผลลัพธ์ต้องเป็นตัวเลขจำนวนเต็ม (Int64.Type) ทันที ไม่ต้องไปเปลี่ยน Type ทีหลัง
Power Query Formula:

=Table.AddColumn(Source, "Year", each Date.Year([Date]), Int64.Type)

Result:

คอลัมน์ Year เป็นตัวเลขจำนวนเต็ม

ตัวอย่างที่ 3: ใช้เงื่อนไข if…then…else

ใช้ Logic การตรวจสอบเงื่อนไขเพื่อกำหนดค่าในคอลัมน์ใหม่
Power Query Formula:

=Table.AddColumn(Source, "Status", each if [Age] >= 18 then "Adult" else "Child")

Result:

คอลัมน์ Status ระบุว่าเป็นเด็กหรือผู้ใหญ่

FAQs

ต่างจาก Custom Column ในหน้าต่าง Power Query อย่างไร?

เหมือนกันครับ เวลาเรากดปุ่ม Add Custom Column โปรแกรมจะสร้างสูตร Table.AddColumn นี้ให้โดยอัตโนมัติ

จำเป็นต้องระบุ columnType ไหม?

ไม่จำเป็น แต่แนะนำให้ระบุเพื่อประสิทธิภาพ และลดขั้นตอน Changed Type ที่มักจะถูกสร้างตามมาทีหลัง

Resources & Related

Additional Notes

ฟังก์ชัน Table.AddColumn ใช้สำหรับเพิ่มคอลัมน์ใหม่เข้าไปในตาราง โดยค่าในคอลัมน์นั้นเกิดจากการคำนวณด้วยสูตร (Custom Column) ตามเงื่อนไขที่กำหนด

Leave a Reply

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