ตัวอย่างที่ 1: คำนวณราคารวม
=Table.AddColumn(Source, "TotalPrice", each [Price] * [Quantity])
ตารางที่มีคอลัมน์ TotalPrice เพิ่มเข้ามา
เพิ่มคอลัมน์ใหม่จากการคำนวณสูตร
Table.AddColumn จะสร้างคอลัมน์ใหม่ต่อท้ายตารางเดิม โดยใช้ฟังก์ชันที่กำหนด (columnGenerator) คำนวณค่าสำหรับแต่ละแถว ซึ่งสามารถอ้างอิงค่าจากคอลัมน์อื่นๆ ในแถวเดียวกันได้ (โดยใช้ [ColumnName]) และสามารถระบุชนิดข้อมูล (Type) ของคอลัมน์ใหม่ได้ด้วย
=Table.AddColumn(table as table, newColumnName as text, columnGenerator as function, optional columnType as nullable type) as table
=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 |
สร้างคอลัมน์ Profit โดยนำ [Sales] – [Cost]
สร้างคอลัมน์ FullName โดยนำ [FirstName] & " " & [LastName]
ถ้า [Score] >= 50 ให้แสดง "Pass" ถ้าไม่ใช่ให้แสดง "Fail"
=Table.AddColumn(Source, "TotalPrice", each [Price] * [Quantity])
ตารางที่มีคอลัมน์ TotalPrice เพิ่มเข้ามา
=Table.AddColumn(Source, "Year", each Date.Year([Date]), Int64.Type)
คอลัมน์ Year เป็นตัวเลขจำนวนเต็ม
=Table.AddColumn(Source, "Status", each if [Age] >= 18 then "Adult" else "Child")
คอลัมน์ Status ระบุว่าเป็นเด็กหรือผู้ใหญ่
เหมือนกันครับ เวลาเรากดปุ่ม Add Custom Column โปรแกรมจะสร้างสูตร Table.AddColumn นี้ให้โดยอัตโนมัติ
ไม่จำเป็น แต่แนะนำให้ระบุเพื่อประสิทธิภาพ และลดขั้นตอน Changed Type ที่มักจะถูกสร้างตามมาทีหลัง
ฟังก์ชัน Table.AddColumn ใช้สำหรับเพิ่มคอลัมน์ใหม่เข้าไปในตาราง โดยค่าในคอลัมน์นั้นเกิดจากการคำนวณด้วยสูตร (Custom Column) ตามเงื่อนไขที่กำหนด