Thep Excel

Text.Length – นับจำนวนตัวอักษรในข้อความ

Text.Length ใช้นับจำนวนตัวอักษรทั้งหมดในสตริงข้อความ รวมถึงช่องว่างและอักขระพิเศษ ผลลัพธ์คืนค่าเป็นตัวเลข มีประโยชน์ในการตรวจสอบคุณภาพข้อมูล การกรองข้อมูลตามความยาว หรือการคำนวณ Logic ต่างๆ

=Text.Length(text as nullable text) as nullable number

By ThepExcel AI Agent
3 December 2025

Function Metrics


Popularity
7/10

Difficulty
2/10

Usefulness
7/10

Syntax & Arguments

=Text.Length(text as nullable text) as nullable number

Argument Type Required Default Description
text text Yes ข้อความที่ต้องการนับจำนวนตัวอักษร อาจเป็น null ได้ ถ้าเป็น null จะคืนค่า null

How it works

ตรวจสอบความยาวรหัส

เช็คว่ารหัสสินค้า หรือรหัสบัตรประชาชนมีจำนวนตัวอักษรตรงตามที่กำหนดหรือไม่

ทำความสะอาดข้อมูล

ใช้ร่วมกับ Text.Trim เพื่อหาความยาวที่แท้จริงของเนื้อหา (ไม่รวมช่องว่างหน้าหลัง)

วิเคราะห์ข้อความ

นับจำนวนตัวอักษรของข้อความในรีวิว เพื่อดูว่ารีวิวสั้นหรือยาว

Examples

ตัวอย่างพื้นฐาน: นับตัวอักษรธรรมชาติ
Text.Length("apple")
คำว่า "apple" มี 5 ตัวอักษร
Power Query Formula:

= Text.Length("apple")

Result:

5

นับรวมช่องว่าง
Text.Length("hello world")
"hello world" มี 11 ตัวอักษร (h-e-l-l-o-space-w-o-r-l-d)
Power Query Formula:

= Text.Length("hello world")

Result:

11

ใช้กับคอลัมน์ในตาราง
let Source = Table.FromRows({"apple", "banana", "kiwi"}), AddLength = Table.AddColumn(Source, "Length", each Text.Length([Value])) in AddLength
สร้างคอลัมน์ใหม่ที่แสดงความยาวข้อความสำหรับแต่ละแถว
Power Query Formula:

let
    Source = Table.FromRows({"apple", "banana", "kiwi"}),
    AddLength = Table.AddColumn(Source, "Length", each Text.Length([Value]))
in
    AddLength

Result:

3 แถว พร้อมคอลัมน์ Length แสดง 5, 6, 4

กรองข้อมูลตามความยาว
let Customers = Table.FromRows({"John", "AB", "Christopher"}), LongNames = Table.SelectRows(Customers, each Text.Length([Name]) > 5) in LongNames
กรองเหลือแค่ชื่อที่มีความยาวมากกว่า 5 ตัวอักษร
Power Query Formula:

let
    Customers = Table.FromRows({"John", "AB", "Christopher"}),
    LongNames = Table.SelectRows(Customers, each Text.Length([Name]) > 5)
in
    LongNames

Result:

2 แถว: "John" (4, ไม่รวม), "Christopher" (11, รวม)

ตรวจสอบการจัดรูปแบบ (เช่น รหัสผ่าน)
let Password = "MyPass123", IsValid = Text.Length(Password) >= 8 in IsValid
ตรวจสอบว่ารหัสผ่านมีความยาวตั้งแต่ 8 ตัวอักษรขึ้นไป
Power Query Formula:

let
    Password = "MyPass123",
    IsValid = Text.Length(Password) >= 8
in
    IsValid

Result:

true

ใช้กับ Text.Trim เพื่อเอาช่องว่างออก
Text.Length(Text.Trim(" hello "))
ถ้าไม่ trim ก็จะนับ 13 (รวมช่องว่าง 4 ตัว) แต่ trim ก่อนจะเหลือแค่ 5
Power Query Formula:

= Text.Length(Text.Trim("  hello  "))

Result:

5

FAQs

Text.Length กับ Excel LEN() ต่างกันอย่างไร?

ไม่ต่างครับ ทั้งสองทำหน้าที่เดียวกัน – นับตัวอักษร แต่ Text.Length ใช้ใน Power Query (M language) ส่วน LEN() ใช้ใน Excel Worksheet Function

ถ้าข้อความเป็น null จะเกิดอะไร?

จะคืนค่า null ตัวเอง ถ้าต้องการให้คืนค่า 0 แทน ให้ใช้ Text.Length(…) ?? 0

นับอักขระพิเศษและ Unicode ด้วยไหม?

นับครับ Text.Length นับทุกอักขระ ไม่ว่าจะเป็น ASCII, Unicode, emoji, สัญลักษณ์พิเศษ ก็นับหมด

การนับตัวอักษรไทยแตกต่างจาก English ไหม?

ไม่ Text.Length นับตัวอักษรไทยเหมือนตัวอักษรอื่นๆ นับตัวอักษร 1 ตัวต่อ 1 ตัวอักษร

ใช้กับ List.Transform ได้ไหม?

ได้ครับ ใช้ List.Transform({“apple”, “banana”}, each Text.Length(_)) ได้ผลลัพธ์เป็น {5, 6}

Resources & Related

Additional Notes

Text.Length ใน Power Query ใช้สำหรับนับจำนวนตัวอักษรทั้งหมดที่มีอยู่ในข้อความ รวมถึงช่องว่างและสัญลักษณ์พิเศษด้วย ส่วนตัวผมชอบใช้ฟังก์ชันนี้คู่กับ Table.SelectRows เพื่อกรองข้อมูลที่มีความยาวไม่ตรงตามเกณฑ์ เช่นกรองรหัสผ่านที่สั้นเกินไป หรือตรวจสอบว่าข้อมูล ID มีรูปแบบที่ถูกต้องหรือไม่

ที่เจ๋งคือ Text.Length นับ null เป็นอักษร 0 ไม่ใช่ null ดังนั้นตรวจสอบ null ก่อนการนับสำคัญมากเลย ถ้าไม่เช็ก null อาจทำให้ query ของเราขัดข้องได้

ส่วนตัวผม มักใช้ Text.Length ไปกับ Text.Trim เพื่อให้ได้ความยาวที่แท้จริง เพราะบางครั้งข้อมูลจากแหล่งอื่นมีช่องว่างหน้า-หลังติดมา ซึ่งทำให้การนับไม่แม่นยำ 😎

Leave a Reply

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