ดึงแถวแรก (Record) จากตารางข้อมูล พร้อมตัวเลือกค่า default สำหรับกรณีตารางว่าง
=Table.First(table as table, optional default as any) as any
=Table.First(table as table, optional default as any) as any
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| table | Table | Yes | ตารางข้อมูลที่ต้องการดึงแถวแรก | |
| default | Any | Optional | null | ค่าที่จะคืนกลับถ้าตารางว่างเปล่า ถ้าไม่ระบุและตารางว่าง จะคืนค่า error จึงควรใส่ default เสมอในการใช้งานจริง |
let Sales = Table.FromRecords({ [OrderID = 1, Customer = "Alice", Amount = 5000], [OrderID = 2, Customer = "Bob", Amount = 3200], [OrderID = 3, Customer = "Char…let
Sales = Table.FromRecords({
[OrderID = 1, Customer = "Alice", Amount = 5000],
[OrderID = 2, Customer = "Bob", Amount = 3200],
[OrderID = 3, Customer = "Charlie", Amount = 4500]
}),
FirstOrder = Table.First(Sales)
in
FirstOrder
[OrderID = 1, Customer = "Alice", Amount = 5000]
let EmptyTable = Table.FromRecords({}), DefaultRecord = [OrderID = 0, Customer = "No Data", Amount = 0], Result = Table.First(EmptyTable, DefaultRecord) in Resu…let
EmptyTable = Table.FromRecords({}),
DefaultRecord = [OrderID = 0, Customer = "No Data", Amount = 0],
Result = Table.First(EmptyTable, DefaultRecord)
in
Result
[OrderID = 0, Customer = "No Data", Amount = 0]
let Employees = Table.FromRecords({ [Name = "David", Position = "Manager", Salary = 80000], [Name = "Emma", Position = "Analyst", Salary = 50000], [Name = "Fran…let
Employees = Table.FromRecords({
[Name = "David", Position = "Manager", Salary = 80000],
[Name = "Emma", Position = "Analyst", Salary = 50000],
[Name = "Frank", Position = "Developer", Salary = 65000]
}),
FirstRow = Table.First(Employees),
FirstName = FirstRow[Name]
in
FirstName
"David"
let Products = Table.FromRecords({ [SKU = "A001", Category = "Fruits", Price = 30], [SKU = "B002", Category = "Vegetables", Price = 20], [SKU = "A002", Category…let
Products = Table.FromRecords({
[SKU = "A001", Category = "Fruits", Price = 30],
[SKU = "B002", Category = "Vegetables", Price = 20],
[SKU = "A002", Category = "Fruits", Price = 45]
}),
FilteredFruits = Table.SelectRows(Products, each [Category] = "Fruits"),
FirstFruit = Table.First(FilteredFruits, [SKU = "EMPTY", Price = 0])
in
FirstFruit
[SKU = "A001", Category = "Fruits", Price = 30]
Table.First คืนค่า Record (แถว 1 รายการ) ส่วน Table.FirstN คืนค่า Table ที่มีหลายแถว เช่น Table.FirstN(tbl, 5) คืนตารางแถวแรก 5 แถว ถ้าต้องการแถวเดียว ใช้ Table.First ประหยัดกว่า
จะเกิด error “Expression.Error: We cannot find a row to return” ดังนั้นหากไม่แน่ใจว่าตารางจะมีข้อมูล ควรใส่ default เสมอ
ไม่ได้ Table.First ใช้ได้เฉพาะ Table type เท่านั้น ถ้าต้องการดึง Element แรกจาก List ให้ใช้ List.First แทน
ทำหน้าที่เดียวกับ Table.First แต่ดึงแถวสุดท้าย (Last row) ของตารางแทน
ก็ระบุค่า Number ใน default ตรงๆ เช่น Table.First(tbl, 0) หรือ Table.First(tbl, null) ขึ้นอยู่กับว่าต้องการคืนค่าอะไร
Table.First ใช้สำหรับดึงแถวแรกของตารางออกมาเป็น Record ซึ่งต่างจาก Table.FirstN ที่คืนค่าเป็นตารางขนาดเล็ก
ฟังก์ชันนี้มีประโยชน์มากเวลาต้องการข้อมูลแถวแรกเท่านั้น เช่น ดึงชื่อผู้บริหารแถวแรกจากรายชื่อพนักงาน หรือดึง Header แถวแรกของข้อมูลต่างๆ ส่วนตัวผมใช้มันกับ Table.Last บ่อยมาก สำหรับดึงข้อมูลช่วงหรือหลักแรก-หลักสุดท้ายออกมาประมวลผล
จุดที่ต้องระวัง: ถ้าตารางว่าง Table.First จะ error ถ้าไม่มี default value ดังนั้นตอนเขียนโค้ดอย่าลืมใส่ default ไว้เสมอ