Thep Excel

Table.FirstN – ดึง N แถวแรกจากตาราง

Table.FirstN คืนแถวแรกๆ จากตาราง โดยสามารถระบุจำนวนแถวที่ต้องการ หรือระบุเงื่อนไข (condition function) เพื่อดึงแถวที่ตรงเงื่อนไขจนกว่าจะพบแถวที่ไม่ตรงแล้วหยุด ถ้าส่งเป็นตัวเลข เช่น 5 จะได้ 5 แถวแรก ถ้าส่งเป็นฟังก์ชัน เช่น each [Amount] > 0 จะได้แถวที่ Amount > 0 จนกว่าจะเจอแถวที่ Amount ≤ 0

=Table.FirstN(table as table, countOrCondition as any) as table

By ThepExcel AI Agent
17 December 2025

Function Metrics


Popularity
8/10

Difficulty
2/10

Usefulness
8/10

Syntax & Arguments

=Table.FirstN(table as table, countOrCondition as any) as table

Argument Type Required Default Description
table table Yes ตารางข้อมูลต้นฉบับที่ต้องการดึงแถวแรก
countOrCondition any Yes ตัวเลข (จำนวนแถวที่ต้องการ) หรือฟังก์ชันเงื่อนไข (each [Column] > value) สำหรับกรองแถว

How it works

Sampling ข้อมูลสำหรับ Preview

ดึงแค่ 100 หรือ 1000 แถวแรกมาดูโครงสร้างข้อมูลและทดสอบ transformation ก่อนประมวลผลข้อมูลทั้งหมด ช่วยประหยัดเวลาในการพัฒนา query

ดึง Top N รายการ

หลังจาก sort แล้ว ใช้ Table.FirstN เพื่อดึง Top 10 หรือ Top 100 รายการที่มีค่าสูงสุด เช่น สินค้าขายดี 10 อันดับแรก

ดึงแถวจนกว่าเงื่อนไขจะเปลี่ยน

ดึงแถวที่ตรงเงื่อนไขจนกว่าจะพบแถวที่ไม่ตรง เช่น ดึงรายการที่ยอดขายเป็นบวกจนกว่าจะเจอรายการที่ขาดทุน

Examples

ตัวอย่างที่ 1: ดึง N แถวแรกตามจำนวน
let Source = Table.FromRecords({ [CustomerID = 1, Name = "Bob", Phone = "123-4567"], [CustomerID = 2, Name = "Jim", Phone = "987-6543"], [CustomerID = 3, Name =…
ดึง 2 แถวแรกจากตาราง ได้ Bob และ Jim ใช้สำหรับ preview ข้อมูลหรือทดสอบ query ก่อนรันกับข้อมูลทั้งหมด
Power Query Formula:

let
    Source = Table.FromRecords({
        [CustomerID = 1, Name = "Bob", Phone = "123-4567"],
        [CustomerID = 2, Name = "Jim", Phone = "987-6543"],
        [CustomerID = 3, Name = "Paul", Phone = "543-7890"],
        [CustomerID = 4, Name = "Ringo", Phone = "232-1550"]
    }),
    First2Rows = Table.FirstN(Source, 2)
in
    First2Rows

Result:

Table: {[CustomerID=1, Name="Bob", ...], [CustomerID=2, Name="Jim", ...]}

ตัวอย่างที่ 2: ดึงแถวตามเงื่อนไข (Positive Values)
let Source = Table.FromRecords({ [a = 1, b = 2], [a = 3, b = 4], [a = -5, b = -6], [a = 7, b = 8] }), PositiveRows = Table.FirstN(Source, each [a] > 0) in Posit…
ดึงแถวที่ a > 0 จนกว่าจะพบแถวที่ a ≤ 0 ได้แถว 1-2 แล้วหยุดที่แถว 3 (a=-5) แม้แถว 4 จะมี a > 0 แต่ก็ไม่ถูกดึงออกมา
Power Query Formula:

let
    Source = Table.FromRecords({
        [a = 1, b = 2],
        [a = 3, b = 4],
        [a = -5, b = -6],
        [a = 7, b = 8]
    }),
    PositiveRows = Table.FirstN(Source, each [a] > 0)
in
    PositiveRows

Result:

Table: {[a=1, b=2], [a=3, b=4]} (หยุดที่แถว 3 เพราะ a=-5)

ตัวอย่างที่ 3: ใช้หลัง Sort เพื่อดึง Top N
let Source = Table.FromRecords({ [Product = "A", Sales = 1000], [Product = "B", Sales = 5000], [Product = "C", Sales = 3000], [Product = "D", Sales = 2000] }),…
Sort ตาราง descending ตาม Sales แล้วดึง 3 แถวแรก ได้สินค้าที่ขายดี 3 อันดับแรก pattern นี้ใช้บ่อยสำหรับ Top N analysis
Power Query Formula:

let
    Source = Table.FromRecords({
        [Product = "A", Sales = 1000],
        [Product = "B", Sales = 5000],
        [Product = "C", Sales = 3000],
        [Product = "D", Sales = 2000]
    }),
    Sorted = Table.Sort(Source, {{"Sales", Order.Descending}}),
    Top3 = Table.FirstN(Sorted, 3)
in
    Top3

Result:

Table: {[Product="B", Sales=5000], [Product="C", Sales=3000], [Product="D", Sales=2000]}

ตัวอย่างที่ 4: Sampling สำหรับทดสอบ Query
let Source = Excel.Workbook(File.Contents("C:\\Data\\LargeFile.xlsx")), Sheet1 = Source{[Item="Sheet1"]}[Data], Sample = Table.FirstN(Sheet1, 1000), Transformed…
ดึงแค่ 1000 แถวแรกจากไฟล์ใหญ่มาทดสอบ transformation ก่อน ช่วยประหยัดเวลาในการพัฒนาและ debug query โดยไม่ต้องรอประมวลผลข้อมูลทั้งหมด
Power Query Formula:

let
    Source = Excel.Workbook(File.Contents("C:\\Data\\LargeFile.xlsx")),
    Sheet1 = Source{[Item="Sheet1"]}[Data],
    Sample = Table.FirstN(Sheet1, 1000),
    Transformed = Table.AddColumn(Sample, "Total", each [Price] * [Qty])
in
    Transformed

Result:

Table ของ 1000 แถวแรกพร้อม Total column

FAQs

ต่างจาก Table.Range อย่างไร?

Table.FirstN(table, 10) เทียบเท่า Table.Range(table, 0, 10) โดย Table.FirstN ใช้งานง่ายกว่าเมื่อต้องการแถวแรกๆ แต่ Table.Range ยืดหยุ่นกว่าเพราะระบุจุดเริ่มต้นได้ (เช่น เริ่มแถวที่ 100)

ถ้าระบุ count มากกว่าจำนวนแถวจะเกิดอะไรขึ้น?

จะคืนแถวทั้งหมดที่มีในตาราง ไม่เกิด error เช่น ตารางมี 5 แถว แต่ระบุ Table.FirstN(table, 100) จะได้ 5 แถวทั้งหมด

เงื่อนไขใน condition parameter ทำงานอย่างไร?

Power Query จะเช็คแถวทีละแถวตามลำดับ ถ้าตรงเงื่อนไขก็เก็บไว้และเช็คแถวต่อไป พอเจอแถวที่ไม่ตรงเงื่อนไขจะหยุดทันที (ไม่เช็คแถวถัดไปอีก) ต่างจาก Table.SelectRows ที่เช็คทุกแถว

ควรใช้ Table.FirstN หรือ Table.SelectRows?

ใช้ Table.FirstN เมื่อต้องการแถวแรกๆ หรือแถวที่ตรงเงื่อนไขจนกว่าจะเจอแถวที่ไม่ตรง ใช้ Table.SelectRows เมื่อต้องการกรองทุกแถวที่ตรงเงื่อนไข ไม่ว่าจะอยู่ตำแหน่งไหนในตาราง

สามารถใช้ร่วมกับ Table.LastN ได้ไหม?

ได้ เช่น Table.FirstN(Table.LastN(source, 100), 10) จะได้ 10 แถวแรกจาก 100 แถวสุดท้าย แต่ควรพิจารณาว่าจำเป็นจริงหรือไม่ เพราะอาจใช้ Table.Range ได้ตรงกว่า

Resources & Related

Additional Notes

ฟังก์ชัน Table.FirstN ใช้สำหรับดึงแถวแรกๆ จากตาราง สามารถระบุจำนวนแถวที่ต้องการ หรือระบุเงื่อนไขเพื่อดึงแถวที่ตรงเงื่อนไขจนกว่าจะพบแถวที่ไม่ตรง เหมาะสำหรับการ sampling ข้อมูลหรือดึงข้อมูลชุดแรกมาทดสอบ

Leave a Reply

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