Power BI | คำนวณ percent % ไม่ต้องเขียน Measures ด้วย Visual Calculations
Power BI | คำนวณ percent % ไม่ต้องเขียน Measures ด้วย Visual Calculations
Power BI ปกติ ถ้าจะคำนวณ Percentage ของตัวเลขที่สรุป
เพื่อแสดงผลใน กราฟ จะต้องเขียน Measures
ต้องใช้ Functions ตามนี้
CALCULATE
ALL
SUM
รวมถึงต้องเข้าใจใน Filter Context ของ Data Model ด้วย
Power BI ตั้งแต่ version Feb 2024 ออก วิธีการคำนวณใหม่มา
ชื่อ Visual Calculations
โดย ผู้ใช้งาน สร้างสูตรคำนวณ ไม่ต้องสร้าง Measures
วิธีเพิ่ม Visual Calculations
ตัวอย่างการใช้งาน
จากรูปมีการสรุป SalesAmount
ของแต่ละ Product Category ด้วย Table
โดยอยากจะต้องการ อีก Column เป็น Percent แสดงสัดส่วนยอดขาย
1.Home - > New Calculation
2.จะมีหน้าต่าง แสดงขึ้นมาด้านล่าง ให้ สร้าง Visual Calculations
ถ้าคลิกชื่อ Fx จะแสดงชื่อ Functions ที่ใข้ได้
ตัวอย่างที่จะลองทำคือ คำนวณ Percent
คลิกเลือก Percent of grand total
3.ในช่อง สูตร
จะขึ้น Template สูตรขึ้นมาให้ทันที
Percent of grand total = DIVIDE([Field], COLLAPSEALL([Field],Axis))
[ Field ] = ชื่อ Field ที่ต้องการจะรวม
Axis = แกนที่ต้องการจะรวม ยอดทั้งหมด
ความหมายของ Axis คือ ทิศทาง ที่ต้องการจะ รวม ตัวเลข ตามรูปด้านล่าง
รูปนี้ ดึงมาจาก Microsoft ตาม link นี้ครับ
https://powerbi.microsoft.com/en-us/blog/visual-calculations-preview/
ดังนั้น
[ Field ] = เลือกชื่อ Sales Amount
Axis = เลือก ROWS เพราะจะรวมยอดทั้งหมด ของ Product Category
Percent of grand total = DIVIDE([Sum of SalesAmount], COLLAPSEALL([Sum of SalesAmount], ROWS))
ผลลัพธ์ที่ได้จะได้ Field ชื่อ Percent of grand total
ตัวเลขที่ แสดง คือ percent
เช่น
Accessories -> 0.53 คือ 53% ของทั้งหมด
แต่ถ้าอยากจะให้แสดงผลลัพธ์ เป็น % เลย
ต้องใช้ Function ชื่อ Format ช่วยครับ
ตามนี้เลย
Format ( ตัวเลข , รูปแบบ format )
Format ( ตัวเลข , "#,00%")
Percent of grand total = FORMAT(DIVIDE([Sum of SalesAmount], COLLAPSEALL([Sum of SalesAmount], ROWS)),"#.00%")
ข้อควรระวังการใช้ Visual Calculations
Visual Calculations จะไ่ม่มีผล กับ Data Model
จะมีผล เฉพาะ กับ Visual ที่เขียนสูตรขึ้นมาเท่านั้น