Forum Discussion
Using Power Query or Dax to create a custom date table for DoD, WoW, MoM view
- Anonymous4 years ago
And to tie this off, it worked! Here is the code to build the table:
DoDWoWMoM_Table = var dailycal = ADDCOLUMNS(CALENDAR(TODAY()-8, TODAY()-1),"Working Date",if(WEEKDAY([Date]) in {1,7},blank(),[Date]),"Category","Day","Testvar",1) var workingdailycal = SUMMARIZE(filter(dailycal,isblank([Working Date])=false()),[Working Date],[Category],[Testvar]) var lastdayofSAweek = [SA_Data_Date] - WEEKDAY(SA_Data[SA_Data_Date],3)+4 var weeklycal = ADDCOLUMNS(CALENDAR(lastdayofSAweek-4-7*4, lastdayofSAweek),"Working Date",if(WEEKDAY([Date]) in {1,7},blank(),[Date]),"Category","Week","Testvar",1 ) var workingweeklycal = SUMMARIZE(filter(weeklycal,isblank([Working Date])=false()),[Working Date],[Category],[Testvar]) var lastdayofSAmonth = EOMONTH(SA_Data[SA_Data_Date],0) var firstdayofSAmonthminus3 = EOMONTH(SA_Data[SA_Data_Date],-3)+1 var monthlycal = ADDCOLUMNS(CALENDAR(firstdayofSAmonthminus3, lastdayofSAmonth),"Working Date",if(WEEKDAY([Date]) in {1,7},blank(),[Date]),"Category","Month","Testvar",lastdayofSAmonth) var workingmonthlycal = SUMMARIZE(filter(monthlycal,isblank([Working Date])=false()),[Working Date],[Category],[Testvar]) return union(workingdailycal,workingweeklycal,workingmonthlycal)Here is the code for naming the columns:
DoDWoWMoM_Name = SWITCH([Category], "Day", FORMAT([Working Date],"Short Date"), "Week", lookupvalue('dimension-date'[month_name_short],'dimension-date'[date],[Working Date])&" Wk "&1 + WEEKNUM ( [Working Date] )-WEEKNUM( STARTOFMONTH ( DoDWoWMoM_Table[Working Date] )), "Month", left(LOOKUPVALUE('dimension-date'[month_name],'dimension-date'[date],[Working Date]),3)&"-"&right(LOOKUPVALUE('dimension-date'[year],'dimension-date'[date],[Working Date]),2))And here is the code for creating the sort columns. Change value to decimal and it does the trick:
Sort_Value = SWITCH([Category], "Day", "0.1"&YEAR([Working Date])&DAY( [Working Date] ), "Week", "0.2"&YEAR([Working Date])&WEEKNUM ( [Working Date] ), "Month", "0.3"&YEAR([Working Date])&MONTH([Working Date]))There might be a thing or two to troubleshoot, but overall this looks to be doing exactly what I need it to do.
In case anyone is looking at the same issue, here is a first idea: Based on this post https://community.powerbi.com/t5/Desktop/How-to-add-rows-to-a-new-custom-table-based-on-values-in-other/m-p/80029, the right step might be to create three new calendar tables and crossjoin them. I'll see if I can piece that together from that post.
- Anonymous4 years agoNot applicable
And to tie this off, it worked! Here is the code to build the table:
DoDWoWMoM_Table = var dailycal = ADDCOLUMNS(CALENDAR(TODAY()-8, TODAY()-1),"Working Date",if(WEEKDAY([Date]) in {1,7},blank(),[Date]),"Category","Day","Testvar",1) var workingdailycal = SUMMARIZE(filter(dailycal,isblank([Working Date])=false()),[Working Date],[Category],[Testvar]) var lastdayofSAweek = [SA_Data_Date] - WEEKDAY(SA_Data[SA_Data_Date],3)+4 var weeklycal = ADDCOLUMNS(CALENDAR(lastdayofSAweek-4-7*4, lastdayofSAweek),"Working Date",if(WEEKDAY([Date]) in {1,7},blank(),[Date]),"Category","Week","Testvar",1 ) var workingweeklycal = SUMMARIZE(filter(weeklycal,isblank([Working Date])=false()),[Working Date],[Category],[Testvar]) var lastdayofSAmonth = EOMONTH(SA_Data[SA_Data_Date],0) var firstdayofSAmonthminus3 = EOMONTH(SA_Data[SA_Data_Date],-3)+1 var monthlycal = ADDCOLUMNS(CALENDAR(firstdayofSAmonthminus3, lastdayofSAmonth),"Working Date",if(WEEKDAY([Date]) in {1,7},blank(),[Date]),"Category","Month","Testvar",lastdayofSAmonth) var workingmonthlycal = SUMMARIZE(filter(monthlycal,isblank([Working Date])=false()),[Working Date],[Category],[Testvar]) return union(workingdailycal,workingweeklycal,workingmonthlycal)Here is the code for naming the columns:
DoDWoWMoM_Name = SWITCH([Category], "Day", FORMAT([Working Date],"Short Date"), "Week", lookupvalue('dimension-date'[month_name_short],'dimension-date'[date],[Working Date])&" Wk "&1 + WEEKNUM ( [Working Date] )-WEEKNUM( STARTOFMONTH ( DoDWoWMoM_Table[Working Date] )), "Month", left(LOOKUPVALUE('dimension-date'[month_name],'dimension-date'[date],[Working Date]),3)&"-"&right(LOOKUPVALUE('dimension-date'[year],'dimension-date'[date],[Working Date]),2))And here is the code for creating the sort columns. Change value to decimal and it does the trick:
Sort_Value = SWITCH([Category], "Day", "0.1"&YEAR([Working Date])&DAY( [Working Date] ), "Week", "0.2"&YEAR([Working Date])&WEEKNUM ( [Working Date] ), "Month", "0.3"&YEAR([Working Date])&MONTH([Working Date]))There might be a thing or two to troubleshoot, but overall this looks to be doing exactly what I need it to do.