Table.IsEmpty ใช้สำหรับตรวจสอบว่าตารางไม่มีข้อมูล (ไม่มีแถว) ใช่หรือไม่ คืนค่า true หากตารางว่าง false หากมีข้อมูลอย่างน้อย 1 แถว
=Table.IsEmpty(table as table) as logical
=Table.IsEmpty(table as table) as logical
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| table | table | Yes | ตารางที่ต้องการตรวจสอบว่างหรือไม่ |
ตรวจสอบก่อนเริ่มประมวลผลเพื่อป้องกัน Error
ตรวจสอบผลลัพธ์การกรองข้อมูลว่าเหลือข้อมูลหรือไม่
ใช้ในการสร้างเงื่อนไขจัดการกรณีไม่มีข้อมูล (Empty State)
let Source = Table.FromRecords({[CustomerID=1, Name="Alice"]}), IsEmpty = Table.IsEmpty(Source) in IsEmptylet
Source = Table.FromRecords({[CustomerID=1, Name="Alice"]}),
IsEmpty = Table.IsEmpty(Source)
in
IsEmpty
false
let Source = Table.FromRecords({}), IsEmpty = Table.IsEmpty(Source) in IsEmptylet
Source = Table.FromRecords({}),
IsEmpty = Table.IsEmpty(Source)
in
IsEmpty
true
let Sales = Table.FromRecords({[Region="North", Amount=1000], [Region="South", Amount=2000]}), FilteredEast = Table.SelectRows(Sales, each [Region] = "East"), C…let
Sales = Table.FromRecords({[Region="North", Amount=1000], [Region="South", Amount=2000]}),
FilteredEast = Table.SelectRows(Sales, each [Region] = "East"),
CheckEmpty = Table.IsEmpty(FilteredEast)
in
CheckEmpty
true
let Data = Table.FromRecords({}), Result = if Table.IsEmpty(Data) then "No data found" else Table.RowCount(Data) in Resultlet
Data = Table.FromRecords({}),
Result = if Table.IsEmpty(Data) then "No data found" else Table.RowCount(Data)
in
Result
"No data found"
Table.IsEmpty คืนค่า true/false ส่วน Table.RowCount คืนค่าตัวเลขจำนวนแถว ใช้ Table.IsEmpty เมื่อต้องการตัดสินใจ if/else ใช้ Table.RowCount เมื่อต้องการรู้จำนวนที่แน่นอน
เข้มงวด – ตรวจเฉพาะจำนวนแถว ไม่สนใจว่าตารางมีคอลัมน์หรือเพียงว่างเปล่า แม้ตารางจะถูกสร้างจาก record ที่มี schema แต่ถ้าไม่มีแถว ก็ถือว่าว่าง
ไม่ได้ จะเกิด error หากอินพุตเป็น null ต้องป้องกัน null ก่อนใช้ Table.IsEmpty โดยใช้ try-catch หรือ if x is table
เร็วมาก ไม่ต้องดึงข้อมูลทั้งหมด เพียงแค่ตรวจสอบว่ามีแถวหนึ่งแถวขึ้นไป ใช้ได้ปลอดภัยแม้กับ large dataset
Table.IsEmpty(table) ใช้เพื่อตรวจสอบว่าตารางมีแถวข้อมูลหรือไม่ คืนค่า true สำหรับตารางว่าง (0 แถว) และ false เมื่อตารางมีข้อมูล
ที่เจ๋งของ Table.IsEmpty คือว่ามันประเมินเฉพาะจำนวนแถวเท่านั้น ไม่สนใจโครงสร้างของคอลัมน์ แม้ตารางจะมีคอลัมน์อยู่ แต่ถ้าไม่มีแถว ก็ถือว่าว่าง ใช้ได้ดีเมื่อต้องการตัดสินใจว่าจะทำขั้นตอนต่อไปหรือไม่
ส่วนตัวผม ชอบใช้ Table.IsEmpty ในการจัดการกับกรณี edge case คือ “เกิดอะไรขึ้นเมื่อข้อมูลอินพุตว่างเปล่า” ตัวอย่างเช่น หลังจากกรองข้อมูล ถ้า Table.IsEmpty คืนค่า true แสดงว่าไม่มีแถวที่ตรงเงื่อนไข เราสามารถจัดการกรณีนี้ได้โดยใช้ if condition