Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Using Power Query or Dax to create a custom date table for DoD, WoW, MoM view

Hi all,   I am converting a report from Excel to Power BI, and one of the requested formats is to have a table that shows the last five days, the last five weeks, and the three most recent months i...
  • Anonymous's avatar
    Anonymous
    4 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.