Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Anonymous
Not applicable

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 in the columns and the KPIs in the rows.

The resulting view is something like this:

Metric8/24/20228/25/20228/26/20228/29/20228/30/2022Jul Wk 5Aug Wk 1Aug Wk 2Aug Wk 3Aug Wk 4Jun-22Jul-22Aug-22
KPI 13.863.425.114.54 - 4.804.40 - 4.713.574.003.814.094.043.933.923.97
KPI 249.4%49.0%58.5%52.8%56.9%48.5%50.1%48.2%52.4%55.1%56.1%54.7%53.5%

 

I can solve this by importing the following table that I created in excel, linking the second column to my date table, filtering in both directions, sorting the "Daily_Report_Name" column by the sort order column, and then creating a matrix with the first column of this table in the columns and the KPI measures as values:

Daily_Report_NameDate_RangeSort Order
8/24/20228/24/20221
8/25/20228/25/20222
8/26/20228/26/20223
8/29/20228/29/20224
8/30/20228/30/20225
Jul Wk 57/25/20226
Jul Wk 57/26/20226
Jul Wk 57/27/20226
Jul Wk 57/28/20226
Jul Wk 57/29/20226
Aug Wk 18/1/20227
Aug Wk 18/2/20227
Aug Wk 18/3/20227
Aug Wk 18/4/20227
Aug Wk 18/5/20227
Aug Wk 28/8/20228
Aug Wk 28/9/20228
Aug Wk 28/10/20228
Aug Wk 28/11/20228
Aug Wk 28/12/20228
Aug Wk 38/15/20229
Aug Wk 38/16/20229
Aug Wk 38/17/20229
Aug Wk 38/18/20229
Aug Wk 38/19/20229
Aug Wk 48/22/202210
Aug Wk 48/23/202210
Aug Wk 48/24/202210
Aug Wk 48/25/202210
Aug Wk 48/26/202210
Jun-226/1/202211
Jun-226/2/202211
Jun-226/3/202211
Jun-226/4/202211
Jun-226/6/202211
Jun-226/7/202211
Jun-226/8/202211
Jun-226/9/202211
Jun-226/10/202211
Jun-226/13/202211
Jun-226/14/202211
Jun-226/15/202211
Jun-226/16/202211
Jun-226/17/202211
Jun-226/20/202211
Jun-226/21/202211
Jun-226/22/202211
Jun-226/23/202211
Jun-226/24/202211
Jun-226/27/202211
Jun-226/28/202211
Jun-226/29/202211
Jun-226/30/202211
Jul-227/1/202212
Jul-227/4/202212
Jul-227/5/202212
Jul-227/6/202212
Jul-227/7/202212
Jul-227/8/202212
Jul-227/11/202212
Jul-227/12/202212
Jul-227/13/202212
Jul-227/14/202212
Jul-227/15/202212
Jul-227/18/202212
Jul-227/19/202212
Jul-227/20/202212
Jul-227/21/202212
Jul-227/22/202212
Jul-227/25/202212
Jul-227/26/202212
Jul-227/27/202212
Jul-227/28/202212
Jul-227/29/202212
Aug-228/1/202213
Aug-228/2/202213
Aug-228/3/202213
Aug-228/4/202213
Aug-228/5/202213
Aug-228/8/202213
Aug-228/9/202213
Aug-228/10/202213
Aug-228/11/202213
Aug-228/12/202213
Aug-228/15/202213
Aug-228/16/202213
Aug-228/17/202213
Aug-228/18/202213
Aug-228/19/202213
Aug-228/22/202213
Aug-228/23/202213
Aug-228/24/202213
Aug-228/25/202213
Aug-228/26/202213
Aug-228/29/202213
Aug-228/30/202213
Aug-228/31/202213

 

What I am now trying to solve for is building this table dynamically through DAX or Power Query. There are three important quirks to how it needs to be built:

1. The five most recent days are always the five previous workdays (Monday-Friday)

2. The most recent weeks and months are the most recent weeks/months for which there is data in another table. I have a measure [SA_Data_Date] that outputs the most recent date for which there is data, or I could reference the [ds] column of the table, where max(SA_Data[ds]) would also give me the date.

The reason for this is that I always have data for the previous days from a less accurate data source, but the weekly and monthly KPIs as well as days for which there is data in the slower but more accurate source are calculated from that second source. So, for example, today (Wednesday of Aug wk 5) I have data from the less accurate source for Monday and Tuesday, but I'm not displaying Aug Wk 5 yet in the weekly view because I have no data from the accurate but delayed source that I'm using to calculate weekly and monthly KPIs.

3. The week count is including partial weeks. For example, Sep Wk 1 is going to be 9/1 through 9/2, and so forth.


What I am struggling with is how to write table creation formulas that dynamically create the right number of rows, name the weeks and months, and create the correct sort order column. Any advice or tricks on how to accomplish this would be appreciated.

Thanks so much!

1 ACCEPTED SOLUTION
Anonymous
Not 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.

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

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-ot..., 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.

Anonymous
Not 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.

Helpful resources

Announcements
October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors