MAX คืนค่าสูงสุดจากคอลัมน์ภายใต้ filter context หรือเปรียบเทียบสองค่าสเกลาร์แล้วเลือกค่าที่มากกว่า ทำงานกับ numeric, date และ text columns โดย scan ทุก row ที่ผ่าน filter แล้วเลือกค่าสูงสุด เหมาะสำหรับ measures และ calculated columns (ต้อง wrap ด้วย CALCULATE)
=MAX(<column>)
=MAX(<column>)
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| column | column reference | Yes | คอลัมน์ที่ต้องการหาค่าสูงสุด ต้องใช้รูปแบบ Table[Column] เช่น Sales[Amount] หรือ Products[Price] ฟังก์ชันจะ scan ทุก row ที่ผ่าน filter context และคืนค่าสูงสุด รองรับ numeric, date และ text columns | |
| expression1 | scalar expression | Yes | ค่าสเกลาร์ตัวแรกที่ต้องการเปรียบเทียบ (ใช้ในรูปแบบสองอาร์กิวเมนต์) อาจเป็น measure, ค่าคงที่ หรือ expression ที่คืนค่าเดียว เช่น [Total Sales] หรือ 10000 | |
| expression2 | scalar expression | Yes | ค่าสเกลาร์ตัวที่สองที่ต้องการเปรียบเทียบ (ใช้ในรูปแบบสองอาร์กิวเมนต์) BLANK ถูก treat เป็น 0 เมื่อเปรียบเทียบ เช่น MAX(-5, BLANK()) จะเปรียบเทียบ -5 กับ 0 และคืน 0 (ค่าดิบเป็น BLANK) |
หาราคาสูงสุด ยอดขายสูงสุด หรือปริมาณสูงสุดในข้อมูลที่ถูก filter อยู่ในขณะนั้น
ใช้ MAX กับคอลัมน์ Date เพื่อหาวันที่ล่าสุดในช่วงที่เลือก เช่น หาวันที่ของ transaction ล่าสุด
เลือกค่าที่มากกว่าระหว่าง actual กับ budget หรือระหว่างยอดจริงกับค่า threshold ที่กำหนด
หาค่าสูงสุดในเงื่อนไขพิเศษ เช่น ราคาสูงสุดของสินค้าที่ Active หรือยอดขายสูงสุดของปีที่แล้ว
// Measure: หาราคาสินค้าสูงสุดในบริบทปัจจุบัน Highest Price = MAX(Products[UnitPrice]) // Context: Products table มี UnitPrice column // Filter context: ถูกกำหน…// Measure: หาราคาสินค้าสูงสุดในบริบทปัจจุบัน
Highest Price = MAX(Products[UnitPrice])
// Context: Products table มี UnitPrice column
// Filter context: ถูกกำหนดโดย slicer/filter ใน visual
// Result: คืนค่าราคาสูงสุดจาก rows ที่ผ่าน filter
ถ้า filter เลือก Category = "Electronics" และมีราคา 500, 1200, 890 → คืน 1200
// Measure: เลือกค่าที่มากกว่าระหว่างยอดขายจริงกับ minimum threshold Sales Above Minimum = VAR ActualSales = SUM(Sales[Amount]) VAR MinimumThreshold = 10000 RET…// Measure: เลือกค่าที่มากกว่าระหว่างยอดขายจริงกับ minimum threshold
Sales Above Minimum =
VAR ActualSales = SUM(Sales[Amount])
VAR MinimumThreshold = 10000
RETURN
MAX(ActualSales, MinimumThreshold)
// Context: ใช้เพื่อแสดงผลไม่ต่ำกว่า threshold
// Result: ถ้า ActualSales = 8000 จะคืน 10000 (threshold)
// Result: ถ้า ActualSales = 15000 จะคืน 15000 (actual)
ActualSales = 15000 → คืน 15000 | ActualSales = 8000 → คืน 10000 (threshold)
// Measure: หาวันที่ของ order ล่าสุดในบริบทปัจจุบัน Latest Order Date = MAX(Sales[OrderDate]) // Calculated Column: วันที่ล่าสุดของลูกค้าแต่ละคน (row context) C…// Measure: หาวันที่ของ order ล่าสุดในบริบทปัจจุบัน
Latest Order Date = MAX(Sales[OrderDate])
// Calculated Column: วันที่ล่าสุดของลูกค้าแต่ละคน (row context)
Customer Latest Order =
CALCULATE(
MAX(Sales[OrderDate])
)
// Context Behavior:
// - Measure: filter context จาก visual (เช่น filter by customer)
// - Calculated Column: CALCULATE สร้าง filter context จาก row context ของแต่ละ customer
Measure: ถ้า filter เลือก Customer A → คืน 2024-12-10 | Column: แต่ละ row คืนวันที่ล่าสุดของลูกค้านั้น
// Measure: หาราคาสูงสุดของสินค้าที่ Active เท่านั้น Max Active Product Price = CALCULATE( MAX(Products[UnitPrice]), Products[Status] = "Active" ) // Measure: เ…// Measure: หาราคาสูงสุดของสินค้าที่ Active เท่านั้น
Max Active Product Price =
CALCULATE(
MAX(Products[UnitPrice]),
Products[Status] = "Active"
)
// Measure: เปรียบเทียบยอดขายปีนี้กับปีที่แล้ว แล้วเลือกค่าที่สูงกว่า
Higher Sales Year =
VAR CurrentYearSales = SUM(Sales[Amount])
VAR LastYearSales =
CALCULATE(
SUM(Sales[Amount]),
SAMEPERIODLASTYEAR('Date'[Date])
)
RETURN
MAX(CurrentYearSales, LastYearSales)
// Context: CALCULATE modifies filter context ก่อนที่ MAX จะทำงาน
Max Active: ถ้ามี Active products ราคา 200, 500, 300 → คืน 500 | Higher Sales: 2024 Sales = 50000, 2023 Sales = 60000 → คืน 60000
MAX ทำงานกับคอลัมน์ที่มีอยู่แล้วใน filter context โดยตรง ไม่สร้าง row context ส่วน MAXX เป็น iterator function ที่สร้าง row context และคำนวณ expression ในแต่ละ row ก่อนหาค่าสูงสุด ใช้ MAX เมื่อต้องการหาค่าสูงสุดจากคอลัมน์เดียว เช่น MAX(Sales[Amount]) แต่ใช้ MAXX เมื่อต้องการคำนวณ expression ในแต่ละ row เช่น MAXX(Sales, Sales[Quantity] * Sales[UnitPrice]) นอกจากนี้ MAX (สองค่า) treat BLANK as 0 แต่ MAXX ignore BLANK ทั้งหมด
ในรูปแบบคอลัมน์ MAX จะข้าม BLANK ไปและคืนค่าสูงสุดจากค่าที่ไม่ใช่ BLANK ถ้าทุกค่าเป็น BLANK จะคืน BLANK ในรูปแบบสองค่า BLANK ถูก treat เป็น 0 เช่น MAX(-5, BLANK()) จะเปรียบเทียบ -5 กับ 0 และคืน 0 (ค่าดิบคือ BLANK) เพราะ 0 > -5 ควรใช้ IF(ISBLANK(…), default_value, …) เพื่อจัดการกรณีพิเศษอย่างปลอดภัย
ใน measure MAX ทำงานกับ filter context จาก visual/slicer โดยตรง เช่น MAX(Sales[Amount]) จะหาค่าสูงสุดจาก rows ที่ผ่าน filter ใน calculated column ต้องใช้ CALCULATE เพื่อทำ context transition (แปลง row context เป็น filter context) เช่น CALCULATE(MAX(Sales[OrderDate])) ไม่เช่นนั้น MAX จะไม่ทำงานใน row context
MAX รองรับ numeric types (Integer, Decimal, Currency) และ Date/DateTime (เพราะ Date เก็บเป็นตัวเลข serial number) ไม่รองรับ Boolean (TRUE/FALSE) โดยตรง ถ้าต้องการใช้กับ Boolean ให้ใช้ MAXA แทน สำหรับ Text columns MAX จะเปรียบเทียบตามลำดับตัวอักษร (alphabetically) และคืนค่าที่อยู่ท้ายสุด
MAX ทำงานกับ Date ได้เพราะ Date ใน DAX เก็บเป็น serial number ดังนั้นค่าสูงสุด = วันที่ล่าสุด เช่น MAX(Sales[OrderDate]) จะคืนวันที่ล่าสุดใน filter context ปัจจุบัน pattern นี้มีประโยชน์มากในการหา last transaction date, latest update date หรือ end date ของช่วงเวลา ใช้ร่วมกับ CALCULATE และ SAMEPERIODLASTYEAR เพื่อเปรียบเทียบวันที่ระหว่างช่วงเวลาต่างกัน
ใช้ MAX เมื่อต้องการหาค่าเดียวที่สูงสุดจากคอลัมน์ (single maximum value) ส่วน SUM ใช้เพื่อรวมค่าทั้งหมด ตัวอย่าง: MAX(Sales[Amount]) = 5000 (ค่า transaction เดียวที่สูงสุด) แต่ SUM(Sales[Amount]) = 50000 (ยอดรวมทั้งหมด) ไม่ควรสับสนระหว่าง aggregation ประเภทต่างกัน MAX ตอบคำถาม “ค่าสูงสุดคือเท่าไหร่” ส่วน SUM ตอบคำถาม “ยอดรวมทั้งหมดคือเท่าไหร่”
MAX มี performance เทียบเท่า MAXX เมื่อใช้กับคอลัมน์เดียวเพราะใน DAX engine MAX() ภายในทำงานเหมือน MAXX(