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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
elshadlive
Frequent Visitor

DAX - How to know which date was 5 working days before a specific date?

PBI_WD.png

I need the formula to figure out the date of 5 working days (not calendar days) before a specific date. Let's assume my entry date is 19.05.2022
If I subtract 5 calendar days from 19.05.2022 then I get 14.05.2022, but it should be 12.05.2022 because there are also non-working days in that range.

In the Sales table, all dates are working dates. My first thought was to take distinct dates in that range and from the result list of dates count back 5 rows (dates). But I couldn't figure out how to do that.
I also created a Calendar table that has a one-to-many relationship with the Sales table, if that will help
Any ideas?

1 ACCEPTED SOLUTION

@elshadlive,

 

Try this measure:

 

5 Working Days Before = 
VAR vCurDate =
    MAX ( Sales[WorkingDays] )
VAR vBaseTable =
    CALCULATETABLE (
        VALUES ( Sales[WorkingDays] ),
        ALLSELECTED (),
        DimDate[Weekday Flag] = 1,
        Sales[WorkingDays] < vCurDate
    )
VAR vRankTable =
    ADDCOLUMNS (
        vBaseTable,
        "@Rank", RANKX ( vBaseTable, Sales[WorkingDays],, DESC )
    )
VAR vResult =
    CALCULATE ( VALUES ( Sales[WorkingDays] ), FILTER ( vRankTable, [@Rank] = 5 ) )
RETURN
    vResult

 

Sample data:

 

DataInsights_0-1652810695435.png

 

Result:

 

DataInsights_1-1652810717760.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

4 REPLIES 4
v-luwang-msft
Community Support
Community Support

Hi @elshadlive ,

Has your problem been solved, if so, please consider Accept a correct reply as the solution or share your own solution to help others find it.

Best Regards
Lucien

DataInsights
Super User
Super User

@elshadlive,

 

Try this measure. The concept is to count five days back, and subtract the number of weekend days in that range.

 

5 Working Days Before =
VAR vCurDate =
    MAX ( DimDate[Date] )
VAR vPastDate = vCurDate - 5
VAR vWeekendCount =
    CALCULATE (
        COUNTROWS ( DimDate ),
        DimDate[Date] >= vPastDate,
        DimDate[Date] <= vCurDate,
        DimDate[Weekday Flag] = 0
    )
VAR vResult = vPastDate - vWeekendCount
RETURN
    vResult

 

DataInsights_0-1652801120524.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




@DataInsights 
Thank you for your reply. What if there were no transactions (working days) for the last two weeks or months? It is not only about weekends. Maybe the company did not sell anything for a month. In that case, we will not have any record on the Sales table if count 5 calendar days back.
I need the 5th previous transaction day.

For example: In the following transaction dates column we have transaction records with the following dates...
10.03.2022
10.03.2022
15.03.2022
16.03.2022
16.03.2022
07.04.2022
07.04.2022
07.04.2022
28.04.2022
19.05.2022
If we start counting back 5 DISTINCT transaction days starting from 19.05.2022, then we have to get 10.03.2022.

I did not test your solution yet, but looking at this part I had a thought that it will not work if we'll not have any transactions for the previous 5 calendar days.

VAR vPastDate = vCurDate - 5

@elshadlive,

 

Try this measure:

 

5 Working Days Before = 
VAR vCurDate =
    MAX ( Sales[WorkingDays] )
VAR vBaseTable =
    CALCULATETABLE (
        VALUES ( Sales[WorkingDays] ),
        ALLSELECTED (),
        DimDate[Weekday Flag] = 1,
        Sales[WorkingDays] < vCurDate
    )
VAR vRankTable =
    ADDCOLUMNS (
        vBaseTable,
        "@Rank", RANKX ( vBaseTable, Sales[WorkingDays],, DESC )
    )
VAR vResult =
    CALCULATE ( VALUES ( Sales[WorkingDays] ), FILTER ( vRankTable, [@Rank] = 5 ) )
RETURN
    vResult

 

Sample data:

 

DataInsights_0-1652810695435.png

 

Result:

 

DataInsights_1-1652810717760.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.