Individual start date per month

和自甴很熟 提交于 2019-12-13 23:04:31

问题


together

how do I get it in PowerPivot or as Dax formula out that for my internal / individual months the beginning of the month from 31.120.2018 for January 2019 receive? So, that I get the January in the PowerPivot calendar table already from 31.12.2018 and then in the 4-4-5 weeks cycle? So

31.12.2018 + 28 days = January
31.12.2018 + (28 days * 2) = February
31.12.2018 + (28 days * 2) + 7 days = March

Does anyone have an idea?

Best Regards


回答1:


First I create a calendar table, this is the simple part, I took a random start and end for testing:

CalendarDates = CALENDAR(DATE(2012;12;20);DATE(2021;1;5))

Then I added the column WeekNumber, this is to the greorgian calendar. A bit complicated because microsoft simply starts counting with 1 on the first of january..

WeekNumber = 
var wNrfirstDayJanNextYear = WEEKNUM(DATE(CalendarDates[Date].[Year] + 1;1;1);2)
var fullWeekNextYear = WEEKNUM(DATE(CalendarDates[Date].[Year] + 1;1;7);2) = 1 //If day 7 of week is still first week, its a full week
var wNr31Dex = WEEKNUM(DATE(CalendarDates[Date].[Year];12;31);2)
var wNr31DexPrevYear = WEEKNUM(DATE(CalendarDates[Date].[Year]-1;12;31);2)
var wNr = WEEKNUM(CalendarDates[Date];2)
var pbi4JanWeekNrNextYear = WEEKNUM(DATE(CalendarDates[Date].[Year] + 1;1;4);2)
var pbi4JanWeekNr = WEEKNUM(DATE(CalendarDates[Date].[Year];1;4);2)
var wNrComp = if (pbi4JanWeekNr =2; wNr - 1; wNr)
return if(NOT(fullWeekNextYear) && wNr = wNr31Dex;IF(pbi4JanWeekNrNextYear = 2; wNr31Dex;1);if(wNrComp = 0; wNr31DexPrevYear; wNrComp))

last step is to create the Month based on the week number:

Month = 
var quater = FLOOR((CalendarDates[WeekNumber] - 1)/13; 1)
var quarterWeek = MOD(CalendarDates[WeekNumber] - 1 - (quater*13);13)
var monthNr = FLOOR(quarterWeek/4;1)
var month445 = if (monthNr = 3; 2; monthNr) + quater * 3 + 1
return if (month445 = 13; 12;month445)

Note: I corrected week 53 in the last quarter so it becomes 4-4-6. You need to do your own math if you want it different.

End result:



来源:https://stackoverflow.com/questions/57413629/individual-start-date-per-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!