cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
water-guy-5
Helper III
Helper III

Creating a networkdays type of measure

I already have a Dimension Dates table and one of the columns looks like this
    Day       DayofWeek
Sunday           1
Monday          2
Tuesday          3
Wednesday    4
Thursday        5
Friday             6
Saturday         7

I have a measure that return if a day is a weekday like this:

IsWeekday = IF(OR(DimDates(DayofWeek) = 1, DimDates(DayofWeek) = 7), "Yes", "No")

I am now trying to use DATEDIFF to calculate the time between two columns. 'ScheduledDate' and 'CompletedDate'.

Thanks!

1 ACCEPTED SOLUTION
jeroendekk
Resolver IV
Resolver IV

Hi @water-guy-5 
You could do something like this basicly counting the rows in the DimDate table with the filters applied. 

This function includes both the first and last day. But your business requirement might be different. But that can easily be changed in de variables.


Working days = 
VAR startdate = MAX(Table[ScheduledDate])
VAR enddate = MAX(Table[CompletedDate])
RETURN

CALCULATE(
    COUNTROWS('DimDate'), 
    DATESBETWEEN('DimDate'[Date], startdate, enddate),
    'DimDate'[DayOfweek] > 1 && 'DimDate'[DayOfweek] < 7)

Best regards
jeroen,


If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!

View solution in original post

3 REPLIES 3
v-jingzhang
Community Support
Community Support

Hi @water-guy-5 

 

If you want a calculated column, here is an example. See Counting working days in DAX - SQLBI

Sales[DeliveryWorkingDays] =
CALCULATE(
    COUNTROWS ( 'Date'),
    DATESBETWEEN ( 'Date'[Date],  Sales[Order Date], Sales[Delivery Date] – 1 ),
    'Date'[IsWorkingDay] = TRUE,
    ALL ( Sales )
)

 

Take notice that when using DATESBETWEEN(<Dates>, <StartDate>, <EndDate>), if StartDate is larger than EndDate, the result is an empty table. So you may need to add an IF condition to decide which date ('ScheduledDate' and 'CompletedDate') is larger before calculating net working days. 

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

rsbin
Super User
Super User

@water-guy-5,

You may want to double check your IsWeekday measure.  It looks to me like you have your YES and No reversed.

If Weekday = 1 or 7, then it is a Weekend, not Weekday.  Just wanna make sure you get this part correct.

 

Regards,

jeroendekk
Resolver IV
Resolver IV

Hi @water-guy-5 
You could do something like this basicly counting the rows in the DimDate table with the filters applied. 

This function includes both the first and last day. But your business requirement might be different. But that can easily be changed in de variables.


Working days = 
VAR startdate = MAX(Table[ScheduledDate])
VAR enddate = MAX(Table[CompletedDate])
RETURN

CALCULATE(
    COUNTROWS('DimDate'), 
    DATESBETWEEN('DimDate'[Date], startdate, enddate),
    'DimDate'[DayOfweek] > 1 && 'DimDate'[DayOfweek] < 7)

Best regards
jeroen,


If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!

Helpful resources

Announcements
Carousel_PBI_Wave1

2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

Power BI Summit Carousel 2

Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

BizApps LATAM 2023

Business Application LATAM Summit 2023

Join the biggest FREE Business Applications Event in LATAM this February.

Power Platform Bootcamp

Global Power Platform Bootcamp

In this bootcamp we will deep-dive into Microsoft’s Power Platform stack with hands-on sessions and labs, delivered to you by experts and community leaders.