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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Dunner2020
Post Prodigy
Post Prodigy

Excluding weekends from the difference of dates.

Hi there,

 

I have a date table, one of the columns contains the name of week name. I want to calculate the number of days between the start date and end date and exclude the weekend. Below is the formula:

Difference in days = CALCULATE( COUNTROWS(Dates), FILTER(ALL(Dates), Dates[Date] >= start_date &&

Dates[Date] <= end_date &&  Dates[IsHoliday] = FALSE() &&  (Dates[Day Of Week Name] <> "Saturday" &&
Dates[Day Of Week Name] <> "Sunday")  )
 
However, it did not return the right number of days. Could anyone guide me where am I made the mistake?
 
 

 

8 REPLIES 8
pranit828
Community Champion
Community Champion

Hi @Dunner2020 

Can you please post some sample data for atleast 10 days?





PBI_SuperUser_Rank@1x.png


Hope it resolves your issue? 
Did I answer your question? Mark my post as a solution!

Appreciate your Kudos, Press the thumbs up button!!
Linkedin Profile

Hi @pranit828 ,

 

Here is the sample data:

 

DateYearQuarter Of YearMonth Of YearDay Of MonthDateIntMonth NameMonth In CalendarQuarter In CalendarDay In WeekDay Of Week NameWeek EndingWeek NumberMonth In YearQuarternYearShort YearRYIsHoliday
1/01/2019201911120190101JanuaryJan 2019Q1 20191Tuesday########1201901002019010019RY19False
2/01/2019201911220190102JanuaryJan 2019Q1 20192Wednesday########1201901002019010019RY19False
3/01/2019201911320190103JanuaryJan 2019Q1 20193Thursday########1201901002019010019RY19False
4/01/2019201911420190104JanuaryJan 2019Q1 20194Friday########1201901002019010019RY19False
5/01/2019201911520190105JanuaryJan 2019Q1 20195Saturday########1201901002019010019RY19False
6/01/2019201911620190106JanuaryJan 2019Q1 20196Sunday########1201901002019010019RY19False
7/01/2019201911720190107JanuaryJan 2019Q1 20190Monday########2201901002019010019RY19False
8/01/2019201911820190108JanuaryJan 2019Q1 20191Tuesday########2201901002019010019RY19False
9/01/2019201911920190109JanuaryJan 2019Q1 20192Wednesday########2201901002019010019RY19False
10/01/20192019111020190110JanuaryJan 2019Q1 20193Thursday########2201901002019010019RY19False
11/01/20192019111120190111JanuaryJan 2019Q1 20194Friday########2201901002019010019RY19False
12/01/20192019111220190112JanuaryJan 2019Q1 20195Saturday########2201901002019010019RY19False
13/01/20192019111320190113JanuaryJan 2019Q1 20196Sunday########2201901002019010019RY19False
14/01/20192019111420190114JanuaryJan 2019Q1 20190Monday########3201901002019010019RY19False
15/01/20192019111520190115JanuaryJan 2019Q1 20191Tuesday########3201901002019010019RY19True
16/01/20192019111620190116JanuaryJan 2019Q1 20192Wednesday########3201901002019010019RY19False
17/01/20192019111720190117JanuaryJan 2019Q1 20193Thursday########3201901002019010019RY19False
18/01/20192019111820190118JanuaryJan 2019Q1 20194Friday########3201901002019010019RY19False
19/01/20192019111920190119JanuaryJan 2019Q1 20195Saturday########3201901002019010019RY19False
20/01/20192019112020190120JanuaryJan 2019Q1 20196Sunday########3201901002019010019RY19False
21/01/20192019112120190121JanuaryJan 2019Q1 20190Monday########4201901002019010019RY19False
22/01/20192019112220190122JanuaryJan 2019Q1 20191Tuesday########4201901002019010019RY19False
23/01/20192019112320190123JanuaryJan 2019Q1 20192Wednesday########4201901002019010019RY19False
24/01/20192019112420190124JanuaryJan 2019Q1 20193Thursday########4201901002019010019RY19False
25/01/20192019112520190125JanuaryJan 2019Q1 20194Friday########4201901002019010019RY19False
26/01/20192019112620190126JanuaryJan 2019Q1 20195Saturday########4201901002019010019RY19False
27/01/20192019112720190127JanuaryJan 2019Q1 20196Sunday########4201901002019010019RY19False
28/01/20192019112820190128JanuaryJan 2019Q1 20190Monday########5201901002019010019RY19False
29/01/20192019112920190129JanuaryJan 2019Q1 20191Tuesday########5201901002019010019RY19False
30/01/20192019113020190130JanuaryJan 2019Q1 20192Wednesday########5201901002019010019RY19False
31/01/20192019113120190131JanuaryJan 2019Q1 20193Thursday########5201901002019010019RY19False

A mimic of Excel NETWORKDAYS func

Networkdays PBI = 
COUNTROWS (
    CALCULATETABLE(
        VALUES('Calendar'[Date]),
        DATESBETWEEN (
            Calendar[Date],
            MIN(Calendar[Date]),
            MAX(Calendar[Date])
        ),
        NOT WEEKDAY ( Calendar[Date] ) IN { 1, 7 },
        NOT 'Calendar'[IsHoliday]
    )
)

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

HI @Dunner2020 

It is missing Dates[IsHoliday] column.





PBI_SuperUser_Rank@1x.png


Hope it resolves your issue? 
Did I answer your question? Mark my post as a solution!

Appreciate your Kudos, Press the thumbs up button!!
Linkedin Profile

@pranit828 ,

 

That's strange. Because I can see IsHoliday column. IsHoliday contains either False or True Values.In the sample, all values of IsHoliday is false except one date i.e. 15/1/2019

HI @Dunner2020 

My bad, I didn't scroll to the right.

I got 22 as a result. My [Is Holiday] column is Text and used the below formula. You can change the [start date], [end date] and make sure they are in the correct format and compared with Dates[Date] column.

 

Difference in days = CALCULATE( COUNTROWS(Dates), FILTER(ALL(Dates)
                            , Dates[Date] >= DATE(2019,01,01) &&
                              Dates[Date] <= date(2019,01,31) &&  Dates[IsHoliday] = "FALSE" 
                              &&  (Dates[Day Of Week Name] <> "Saturday" &&
Dates[Day Of Week Name] <> "Sunday")  )  )

 

 

pranit828_0-1598824484736.png

 





PBI_SuperUser_Rank@1x.png


Hope it resolves your issue? 
Did I answer your question? Mark my post as a solution!

Appreciate your Kudos, Press the thumbs up button!!
Linkedin Profile

@pranit828 , When I ran your code, visualization did not work. I think the reason might be ISHoliday is a calculated column Which has either value 'False' or 'True'. In my code, I was checking holiday condition like date[ISHoliday]=False(), which you changed it to date[ISHoliday]="FALSE". 

@Dunner2020 - I like @pranit828 's solution, I do want to point out that Net Work Days was invented to solve this kind of thing. https://community.powerbi.com/t5/Quick-Measures-Gallery/Net-Work-Days/m-p/367362#M109


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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