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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Calculating backwards X days excluding weekends and holidays in Power BI Dax (Calculated Column)

Need help with calculating backwards X days excluding weekends and holidays in Power BI Dax (Calculated Column.)  I can get to a solution when I need to count forward X days excluding weekends and holidays.  It's the counting backwards where I run into problems.  Especially if the time periods cross over both weekends AND holiday(s).  I have a date table that includes the following columns:

  • Date
  • HolidayFlag (True/False)
  • WorkdayFlag (True/False)
  • WeekendFlag (True/False)
  • IsWorkday (1=True,0=False)

 

I also have a table that includes Due Date.

 

If I needed to calculate 2 working days before the Due Date would return:

 

Due Date          2 Work Days Before

11/26/2019      11/22/2019

11/27/2019      11/25/2019

11/28/2019      11/26/2019

11/29/2019      11/26/2019

11/30/2019      11/26/2019

12/1/2019        11/26/2019

12/2/2019        11/26/2019

12/3/2019        11/27/2019

12/4/2019        12/2/2019   

 

Date Table

Date TableDate Table

 

Thanks

2 REPLIES 2
MartynRamsden
Solution Sage
Solution Sage

Hi @Anonymous 

 

This solution relies on their being a relationship between your fact table and your Date table.

 

Try creating a WorkingDayIndex calculated column in your Date table:

WorkingDayIndex = 
VAR RowDate = 'Date'[Date]
VAR RowWD = 'Date'[IsWorkingDay]
VAR Index =
CALCULATE ( 
    COUNTROWS ( 'Date' ),
    'Date'[Date] <= RowDate,
    'Date'[IsWorkingDay] = 1
) + 1
VAR Result = 
IF ( 
    RowWD = 0,
    Index + 1,
    Index
)
RETURN Result

 

Then, in your fact table, create a calculated column as follows:

Date -2 Working Days = 
VAR SelDateWDIndex = RELATED ( 'Date'[WorkingDayIndex] )
VAR Result = 
CALCULATE ( 
    MAX ( 'Date'[Date] ),
    FILTER (
        ALL ( 'Date' ),
        'Date'[WorkingDayIndex]  = SelDateWDIndex -2
    )
)
RETURN Result

 

If you need this as a measure rather than a calcualted column, you can replace the RELATED function with SELECTEDVALUE.

 

Best regards,

Martyn

 

If I answered your question, please help others by accepting it as a solution.

 

Anonymous
Not applicable

Hi Martyn,

 

Thanks for your suggestion.  I haven't tried it yet.  However, I stumbled across a solution that has worked for me using RANKX and LOOKUPVALUE.  It probably does the same thing as your suggestion 😀

 

First, in my DateTable I need to determine the "Rank" for "Workdays" (Excluding Weekends and Holidays):

 

WorkDayRank =

IF('DateTable’ [WorkdayFlag]=TRUE,RANKX(FILTER('DateTable','DateTable'[WorkdayFlag]=TRUE),'DateTable'[DateValue],,ASC))

 

2019-12-31_13-53-38.jpg

 

Now I can use the resulting "Rank" to calculate my Due Date Minus 2 working days without making any changes to my "Date Table” using LOOKUPVALUE in my Fact Table. By making N Variable, I can quickly change the number of days to count backwards.

 

Due Date Minus N WORKING Days =

Var N = 2

Return

CALCULATE(MAX('DateTable'[DateValue]),FILTER('DateTable','DateTable'[WorkDayRank]=LOOKUPVALUE('DateTable'[WorkDayRank],[DateValue], 'Fact Table'[DueDate])-N))

 

2019-12-31_14-16-23.jpg

 

Hopefully, this will be useful to others and I'll also get a chance to experiment with your solution!  (But you know how it goes, once you find a solution that works . . .)

 

Regards,

 

Thomas

 

 

 

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.