ในบทความนี้ผมจะมาแนะนำวิธีสร้าง Function Query ขึ้นใน Power Query ซึ่งไม่ใช่แบบปกติ แต่จะเป็นแบบที่ยังยอมให้เราแก้ไข Step ต่างๆ ใน Function นั้นได้โดยง่าย
ในบทความนี้ผมก็เลยจะมาแชร์วิธีสร้าง Function อีกวิธีนึงซึ่งยังสามารถทำให้เราแก้ไขได้ง่ายๆ โดยใช้ User Interface ได้อยู่ โดยที่ผมเห็นวิธีนี้ครั้งแรกมาจากคุณโบแห่ง Excel Wizard ที่ไปตอบในคลิปของ อ. Mike แห่ง Excel is fun
ในซีรีส์ Power BI บทก่อนหน้านี้เราได้เรียนรู้จักสูตร DAX พื้นฐานกันไปแล้ว คราวนี้เรามารู้จักฟังก์ชันกลุ่ม Table Function เพิ่มเติม นั่นก็คือ FILTER, DISTINCT, VALUES, ALL ซึ่งจะช่วยให้เราสามารถเขียนสูตร DAX ที่ซับซ้อนได้มากขึ้นในอนาคตนั่นเอง
แต่เพื่อไม่ให้มันเยอะเกินไป เดี๋ยวบทความนี้ผมจะพูดถึง FILTER แค่ตัวเดียวก่อน ส่วน ALL, DISTINCT, VALUES และผองเพื่อน จะพูดในตอนถัดไปนะครับ
แต่ถ้ากรณีจะอ้างอิงค่าใน Field เดียวกันหลายๆ item อย่าง Litware กับ Proseware เราไม่ต้องใช้ OR ก็ได้ แต่หันมาใช้การอ้างอิงเงื่อนไขแบบ field IN {item1, item2} แทนจะง่ายกว่าการเขียนว่า field = item1 ||field = item2 เช่น
myTable = FILTER(dProduct,dProduct[UnitPrice]>1000 || dProduct[BrandName] IN {"Litware" , "Proseware"} )
ผลลัพธ์ออกมาเป็น 4 หมื่นกว่าๆ เพราะยังไม่ได้ปรับ Number Format ดังนั้นให้ลากครอบแล้วปรับ Number Format ให้เป็น Date ซะ (เลือกพื้นที่เผื่อเอาลงมาเยอะๆก็ได้)
7/11/2021 version 3 – แก้ข้อมูลชื่อตำบลภาษาอังกฤษของอุบลราชธานีเลื่อน / แก้ชื่อ eng สมุทรปราการ กับ พังงา ตาม ISO 3166-2:TH แก้ชื่อ โพนพิสัย, กัลยาณิวัฒนา / แก้ district id เวียงเก่าเป็น 4029 / แก้ post code เฝ้าไร่
จากที่ผมได้เคยอธิบายไปว่า Date Table มีความสำคัญต่อการทำ Data Model เป็นอย่างยิ่ง โดยเฉพาะการใช้ฟังก์ชัน Date Intelligence ยิ่งจำเป็นต้องใช้ Date Table เลยล่ะ
แปลว่า Data Model ของทุกคนควรมี Date Table ซึ่งกลายเป็นเรื่องที่ทุกๆ คนต้องทำเหมือนๆ กันหมด
ดังนั้นจะดีกว่ามั้ย ถ้าเราสามารถมีสูตรสำเร็จรูปอะไรซักอย่าง ที่สามารถ Copy Paste ไปใช้สร้าง Date Table ได้เลย? และนั่นคือที่มาของบทความนี้ครับ
ทีนี้การสร้าง Date Table นั้นทำได้ 2 วิธี คือใช้ DAX กับใช้ M Code ของ Power Query เรามาดูแต่ละวิธีกันครับ
สร้าง Date Table ด้วย DAX
แค่กด New Table ใน Power BI แล้วใส่ code นี้ลงไป
dDateTable =
VAR MyCalendar=CALENDARAUTO()
RETURN
ADDCOLUMNS (MyCalendar,
"Year",YEAR([Date]),
"Quarter","Q" & QUARTER([Date]),
"Month",MONTH([Date]),
"Day",DAY([Date]),
"MonthName",FORMAT([Date],"mmmm"),
"YearMonth",FORMAT([Date],"yyyy-mm"),
"YearMonthName",FORMAT([Date],"yyyy-mmm"),
"WeekDayNum",WEEKDAY([Date],2),
"WeekDayName",FORMAT([Date],"dddd"),
"DateText",FORMAT([Date],"yyyy-mm-dd"),
"Start of Month",EOMONTH([Date],-1)+1,
"End of Month",EOMONTH([Date],0)
)
ปกติเวลาเราสั่งให้ Power Query รวมทุกไฟล์ใน Folder เดียวกัน แม้มันจะเอาข้อมูลรวมกันได้ แต่ก็อาจจะมีปัญหาเรื่องชื่อคอลัมน์ได้ในอนาคต
สาเหตุเพราะ มันมีการระบุชื่อคอลัมน์ที่แตกออกมาอย่างชัดเจนในสูตร M Code ทำให้ หากในอนาคตมีคอลัมน์เพิ่มขึ้นหรือลดลง ก็จะมีปัญหาทันที (อย่างเช่นที่ผมบอกไว้ในหนังสือ Excel Power Up ว่า เมื่อไหร่ก็ตามที่มีการระบุชื่อคอลัมน์ในสูตร M Code นั้น จะอันตรายต่อการ Refresh ทันที)
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.