DISTINCTCOUNT นับจำนวนค่าที่แตกต่างกันทั้งหมดในคอลัมน์ รวมถึง BLANK values ถ้าไม่มีแถวข้อมูล จะคืนค่า BLANK
=DISTINCTCOUNT(<column>)
=DISTINCTCOUNT(<column>)
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| column | Column | Yes | คอลัมน์ที่ต้องการนับค่าเอกลักษณ์ (สามารถใช้ได้กับ data type ใดก็ได้ – number, text, date, etc.) |
นับจาก CustomerID ในตาราง Sales เพื่อดูว่ามีลูกค้าไม่ซ้ำกี่คน
ดูความหลากหลายของสินค้าที่ขายออกไป
Total Customers = DISTINCTCOUNT(Sales[CustomerID])Total Customers = DISTINCTCOUNT(Sales[CustomerID])
15
Unique Categories = DISTINCTCOUNT(Products[SubCategory])Unique Categories = DISTINCTCOUNT(Products[SubCategory])
3
Electronics Customers = VAR ElectronicsOrders = FILTER(Sales, Sales[Category] = "Electronics") RETURN DISTINCTCOUNT(ElectronicsOrders[CustomerID])Electronics Customers =
VAR ElectronicsOrders = FILTER(Sales, Sales[Category] = "Electronics")
RETURN
DISTINCTCOUNT(ElectronicsOrders[CustomerID])
3
With Blanks = DISTINCTCOUNT(Orders[Status]) → 4 (Completed, Pending, BLANK, Cancelled) Without Blanks = DISTINCTCOUNTNOBLANK(Orders[Status]) → 3 (Completed, Pen…With Blanks = DISTINCTCOUNT(Orders[Status]) → 4 (Completed, Pending, BLANK, Cancelled)
Without Blanks = DISTINCTCOUNTNOBLANK(Orders[Status]) → 3 (Completed, Pending, Cancelled)
ด้วย BLANK = 4, ไม่มี BLANK = 3
DISTINCTCOUNT นับ BLANK ว่าเป็นค่า 1 ค่า เพราะ ใน DAX ค่าว่างถือเป็นค่าเอกลักษณ์หนึ่ง ถ้าต้องการไม่นับ BLANK ให้ใช้ DISTINCTCOUNTNOBLANK แทน
COUNT นับจำนวนแถว (non-blank) แต่ DISTINCTCOUNT นับจำนวนค่าที่แตกต่าง เช่น Customer C001 มี 5 order COUNT = 5, DISTINCTCOUNT = 1
ใช่ DISTINCTCOUNT(table[column]) เทียบเท่ากับ COUNTROWS(DISTINCT(table[column])) แต่ DISTINCTCOUNT ง่ายและเร็วกว่า
ได้ แต่ระวังเรื่อง Totals ที่ double-count ได้ เช่น ถ้า Customer C001 ปรากฏในหลาย Product Category แล้วหาผลรวม total อาจไม่เท่ากับของจริง
DISTINCTCOUNT เป็นฟังก์ชันสำหรับนับค่าเอกลักษณ์ (unique values) ในคอลัมน์ โดยนับทั้ง BLANK values ด้วย
ที่เจ๋งคือ DISTINCTCOUNT นับเฉพาะว่ามีกี่ค่าที่แตกต่างกัน ไม่ใช่นับจำนวนแถว เช่น ถ้า Products[SubCategory] มี “Electronics” 5 แถว, “Furniture” 3 แถว, BLANK 2 แถว ผลลัพธ์จะเป็น 3 (สามค่าเอกลักษณ์) ไม่ใช่ 10
ส่วนตัวผมคิดว่า DISTINCTCOUNT มีประโยชน์มากในการวิเคราะห์ Cardinality เช่น “มีกี่ลูกค้าที่ซื้อสินค้า” ซึ่ง COUNT หรือ COUNTA ทำไม่ได้ 😎
ข้อเตือน: ถ้า BLANK หลายแถวในข้อมูล DISTINCTCOUNT จะนับ BLANK ว่าเป็น 1 ค่า ถ้าต้องการไม่นับ BLANK ให้ใช้ DISTINCTCOUNTNOBLANK แทน