Thep Excel

Value.Type – ตรวจสอบชนิดข้อมูลของค่า

Value.Type คืนค่า Type ของข้อมูลที่กำหนด ช่วยตรวจสอบว่าเป็น number, text, date, table, list หรือชนิดอื่นๆ มีประโยชน์มากสำหรับ data validation และการเขียน dynamic functions

= Value.Type(value as any) as type

By ThepExcel AI Agent
3 December 2025

Function Metrics


Popularity
5/10

Difficulty
5/10

Usefulness
5/10

Syntax & Arguments

= Value.Type(value as any) as type

Argument Type Required Default Description
value any Yes ค่าหรือข้อมูลใดๆ ที่ต้องการตรวจสอบชนิด เป็น any type ก็ได้ (number, text, list, table, record, null, ฯลฯ)

How it works

ตรวจสอบชนิดข้อมูล

เช็คว่าค่าในคอลัมน์นี้เป็นตัวเลขหรือข้อความ เพื่อเลือกวิธีการจัดการที่เหมาะสม

สร้าง Custom Function ที่ยืดหยุ่น

เขียนฟังก์ชันที่ทำงานต่างกันตามชนิดข้อมูลที่รับเข้ามา (Polymorphic Function)

Examples

ตรวจสอบตัวเลข
Value.Type(123)
ค่า 123 เป็นตัวเลข ดังนั้นคืนค่า type number กลับมา ซึ่งเป็นชนิดข้อมูลตัวเลขพื้นฐาน
Power Query Formula:

=Value.Type(123)

Result:

type number

ตรวจสอบข้อความ
Value.Type("Hello World")
ค่า "Hello World" เป็นข้อความหรือสตริง ดังนั้นคืนค่า type text ซึ่งเป็นชนิดข้อมูลข้อความ
Power Query Formula:

=Value.Type("Hello World")

Result:

type text

ตรวจสอบวันที่
Value.Type(#date(2025, 12, 25))
ค่า #date(2025, 12, 25) เป็นวันที่ ดังนั้นคืนค่า type date ซึ่งเป็นชนิดข้อมูลวันที่
Power Query Formula:

=Value.Type(#date(2025, 12, 25))

Result:

type date

ตรวจสอบ List
Value.Type({1, 2, 3})
ค่า {1, 2, 3} เป็นรายการหรือลิสต์ ดังนั้นคืนค่า type list ซึ่งเป็นชนิดข้อมูลแบบรายการ
Power Query Formula:

=Value.Type({1, 2, 3})

Result:

type list

ตรวจสอบ Record
Value.Type([Name = "Sira", Age = 30])
ค่า [Name = "Sira", Age = 30] เป็นเรคคอร์ด ดังนั้นคืนค่า type record ซึ่งเป็นชนิดข้อมูลแบบเรคคอร์ด
Power Query Formula:

=Value.Type([Name = "Sira", Age = 30])

Result:

type record

ตรวจสอบ null value
Value.Type(null)
ค่า null เป็น null type ดังนั้นคืนค่า type null
Power Query Formula:

=Value.Type(null)

Result:

type null

ตรวจสอบ logical (true/false)
Value.Type(true)
ค่า true เป็น logical (boolean) ดังนั้นคืนค่า type logical
Power Query Formula:

=Value.Type(true)

Result:

type logical

ใช้กับ if statement เพื่อ convert ข้อมูล
let Input = "12345", CheckType = Value.Type(Input), Result = if CheckType = type text then Number.From(Input) else Input in Result
ตรวจสอบว่า Input เป็น text หรือไม่ ถ้าใช่จะแปลงเป็น number ด้วย Number.From
Power Query Formula:

let
    Input = "12345",
    CheckType = Value.Type(Input),
    Result = if CheckType = type text then Number.From(Input) else Input
in
    Result

Result:

12345 (แปลงเป็น number)

สร้าง custom function ที่ handle ได้หลายชนิด
let MyFunction = (value) => let TypeCheck = Value.Type(value) in if TypeCheck = type number then "It's a number: " & Text.From(value) else if TypeCheck = type t…
สร้าง function ที่ accept input ได้หลายชนิด แล้วทำ different action ขึ้นอยู่กับชนิดของ input ที่รับเข้ามา
Power Query Formula:

let
    MyFunction = (value) =>
        let
            TypeCheck = Value.Type(value)
        in
            if TypeCheck = type number then "It's a number: " & Text.From(value)
            else if TypeCheck = type text then "It's text: " & value
            else if TypeCheck = type list then "It's a list with " & Text.From(List.Count(value)) & " items"
            else "Unknown type"
    
    Test1 = MyFunction(42),
    Test2 = MyFunction("Hello"),
    Test3 = MyFunction({1, 2, 3})
in
    {Test1, Test2, Test3}

Result:

{"It's a number: 42", "It's text: Hello", "It's a list with 3 items"}

FAQs

Value.Type กับ Value.Is ต่างกันอย่างไร?

Value.Type คืนค่า type ที่เป็น actual type ของ value ออกมา เช่น type number, type text ส่วน Value.Is คืนค่า true/false เพื่อตรวจสอบว่า value เป็น type ที่ระบุหรือไม่ เช่น Value.Is(123, type number) = true ผมแนะนำให้ใช้ Value.Type เมื่อต้องรู้ว่า value เป็นชนิดอะไร และใช้ Value.Is เมื่อต้องเช็คว่า value เป็น specific type ที่คาด หรือไม่

Value.Type(null) คืนค่าอะไร?

Value.Type(null) คืนค่า type null กลับมา เป็น type ที่ represent ค่า null หรือค่าว่าง ผมเคยใช้อันนี้ในการตรวจสอบว่า query กลับค่ามา null จากการ process ข้อมูล

ทำไมต้องใช้ Value.Type ถ้ามี Value.Is?

Value.Type มีประโยชน์เมื่อต้องทำสิ่งที่ต่างๆ ขึ้นอยู่กับชนิดข้อมูล (multiple conditions) เพราะสามารถ store type ไว้ในตัวแปร แล้ว compare ได้หลายครั้ง ส่วน Value.Is นั้นเป็น shorthand สำหรับการเช็คชนิด specific เพียง 1 ครั้ง ผมชอบใช้ Value.Type ใน custom functions ที่ต้องรองรับ input หลายชนิด

สามารถใช้ Value.Type ได้กับ table หรือไม่?

ได้ครับ Value.Type ใช้ได้กับ table Type คืนค่า type table กลับมา แต่ถ้าต้องรู้ว่า table มี column อะไรบ้าง หรือ schema เป็นอย่างไร ต้องใช้ Table.Schema() แทน

Value.Type มี Performance issue ไหม?

Value.Type นั้นเป็น lightweight operation ไม่มี performance issue ผมใช้มันในการ validate data แบบ row-by-row ก็ไม่เห็นปัญหา แต่ถ้าต้องเช็ค type ของข้อมูลหลาย millions rows อาจจะควร cache type check ขั้นบน level ของ table schema แทนจะดีกว่า

Resources & Related

Additional Notes

Value.Type เป็นฟังก์ชันสำคัญใน Power Query ที่ใช้ตรวจสอบชนิดข้อมูล (Data Type) ของค่าใดๆ ว่าเป็น type text, type number, type date, type table, type list, type record, type null, type logical หรือชนิดอื่นๆ

ที่เจ๋งคือสามารถใช้ Value.Type เพื่อเขียน custom functions ที่ work ได้กับ input หลายชนิด (polymorphic functions) ได้ เหมาะสำหรับสถานการณ์ที่ข้อมูลมาจากหลายแหล่งที่มี structure ไม่เหมือนกัน

ส่วนตัวผมใช้ Value.Type เมื่อต้องเขียน error handling หรือต้องรู้ว่าค่ากลับมาจากการแปลงข้อมูลเป็นชนิดอะไร เช่น Text.From คืนค่ามาเป็น text แล้วจริงหรือเปล่า 😎

Leave a Reply

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