ตัวอย่างการใช้ Power BI(FORMAT Function with DAX)
สำหรับเพื่อนๆที่กำลังพัฒนา Dashboard ด้วย Tool Microsoft Power BI คงต้องเจอการปรับ Format Measurement ซึ่งนอกจากที่เราจะปรับ format ผ่าน Tab Modeling แล้วเรายังสามารถเขียน Dax เพื่อปรับ Format ได้ตามที่เราต้องการ ผมสรุปมาไว้ตามรูปด้านล่างนะครับเพื่อนำไปประยุกต์ใช้งานได้อย่างง่ายๆ
Script ตัวอย่าง
1. Profit_Format(Default) = FORMAT(sum(financials[Profit]),”#,##0;-#,##0″)
2. Profit_Format(MB) = FORMAT(sum(financials[Profit]),”#,##0,,MB;-#,##0,,MB”)
3. Profit_Format(K) = FORMAT(sum(financials[Profit]),”#,##0,K;-#,##0,K”)
4. Profit_Format(K & Add Text) = FORMAT(sum(financials[Profit]),”#,##0,K;-#,##0,K”) & ” (Exc.Vat)”
5. Profit_Format(decimal point) = FORMAT(sum(financials[Profit]),”#,##0,.00K;-#,##0,.00K”)
นอกจากนี้เรายังสามารถเขียน IF Function เพื่อปรับเปลี่ยนการแสดงผลข้อมูลได้ ยกตัวอย่างกรณีที่ไม่ค่าให้แสดงเป็น “-” ในตารางแทนสามารถเขียนได้ดังนี้
Profit_Format(Default) = IF(sum(financials[Profit]) > 0, FORMAT(sum(financials[Profit]),“#,##0;-#,##0”),“-“)
Profit_Format(MB) = IF(sum(financials[Profit])>0,FORMAT(sum(financials[Profit]),“#,##0,,MB;-#,##0,,MB”),“-“)
Profit_Format(K) = IF(sum(financials[Profit])>0,FORMAT(sum(financials[Profit]),“#,##0,K;-#,##0,K”),“-“)
Profit_Format(K & Add Text) = IF(sum(financials[Profit]) >0, FORMAT(sum(financials[Profit]),“#,##0,K;-#,##0,K”) & ” (Exc.Vat)”,“-“)
Profit_Format(decimal point) = IF(sum(financials[Profit]) >0, FORMAT(sum(financials[Profit]),“#,##0,.00K;-#,##0,.00K”),“-“)
ผลลัพเมื่อเรานำ Measurement มาวางบน Table ใน Power BI ดังรูป
SourceCode PowerBI_Format