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
ktt777
Helper V
Helper V

calculate number of days

hi 

how can i write a dax formula to calculate the number of working days ( only count the date, not time) in each month as below.

Number of working days in jan should be 19days ( 15+2+3 =20 but minus 1 as there is overlap in day 15/1/2022).

Number of working days in feb should be 15 days

 

Vehicle From dateTo dateNo of days
A1/1/202215/1/202215 days
A15/1/202216/1/20222 days
A20/1/202222/1/20223 days
A1/2/202210/2/202210 days
A15/2/202216/2/20222 days
A20/2/202222/2/20223 days

 

thanks

1 ACCEPTED SOLUTION
v-luwang-msft
Community Support
Community Support

HI @ktt777 ,

Based on the data you provided, try the following steps.

Step1,create a date table:

Date = CALENDAR("2022,1,1","2022,12,31")

vluwangmsft_0-1657096254552.png

 

 

Step2, calculate date between two date:

day = CALCULATE(COUNTROWS('Date'),FILTER('Date','Date'[Date]>='Table'[From date]&&'Date'[Date]<='Table'[To date]))

Then use the below dax to create new column in order to calculate overlap  day:

rank = RANKX('Table','Table'[From date],,ASC,Dense)
datediff datediff = 
IF (
    'Table'[rank] <> 1,
    IF (
        DATEDIFF (
            'Table'[From date],
            CALCULATE (
                MAX ( 'Table'[To date] ),
                FILTER ( ALL ( 'Table' ), 'Table'[rank] = EARLIER ( 'Table'[rank] ) - 1 )
            ),
            DAY
        ) < 0,
        0,
        DATEDIFF (
            'Table'[From date],
            CALCULATE (
                MAX ( 'Table'[To date] ),
                FILTER ( ALL ( 'Table' ), 'Table'[rank] = EARLIER ( 'Table'[rank] ) - 1 )
            ),
            DAY
        ) + 1
    ),
    0
)

 

Final to get sum  value for every month:

month = MONTH('Table'[From date])

 

sum = CALCULATE(SUM('Table'[day])-SUM('Table'[datediff datediff]),FILTER(ALL('Table'),'Table'[month]=EARLIER('Table'[month])))

Output:

vluwangmsft_1-1657096413815.png

 

You could download my pbix file to learn more details.

 

 

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


Best Regards

Lucien

View solution in original post

3 REPLIES 3
v-luwang-msft
Community Support
Community Support

HI @ktt777 ,

Based on the data you provided, try the following steps.

Step1,create a date table:

Date = CALENDAR("2022,1,1","2022,12,31")

vluwangmsft_0-1657096254552.png

 

 

Step2, calculate date between two date:

day = CALCULATE(COUNTROWS('Date'),FILTER('Date','Date'[Date]>='Table'[From date]&&'Date'[Date]<='Table'[To date]))

Then use the below dax to create new column in order to calculate overlap  day:

rank = RANKX('Table','Table'[From date],,ASC,Dense)
datediff datediff = 
IF (
    'Table'[rank] <> 1,
    IF (
        DATEDIFF (
            'Table'[From date],
            CALCULATE (
                MAX ( 'Table'[To date] ),
                FILTER ( ALL ( 'Table' ), 'Table'[rank] = EARLIER ( 'Table'[rank] ) - 1 )
            ),
            DAY
        ) < 0,
        0,
        DATEDIFF (
            'Table'[From date],
            CALCULATE (
                MAX ( 'Table'[To date] ),
                FILTER ( ALL ( 'Table' ), 'Table'[rank] = EARLIER ( 'Table'[rank] ) - 1 )
            ),
            DAY
        ) + 1
    ),
    0
)

 

Final to get sum  value for every month:

month = MONTH('Table'[From date])

 

sum = CALCULATE(SUM('Table'[day])-SUM('Table'[datediff datediff]),FILTER(ALL('Table'),'Table'[month]=EARLIER('Table'[month])))

Output:

vluwangmsft_1-1657096413815.png

 

You could download my pbix file to learn more details.

 

 

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


Best Regards

Lucien

tamerj1
Super User
Super User

Hi @ktt777 

Sample file https://we.tl/t-IuoukK27zQ
Here is a measure to calculate monthly working days. However, removing the overlapping days shall be a bet complex as there is no consistant criteria. If this is a msut do then I need to write more DAX.  

1.png2.png3.png

Number of Days = 
SUMX ( 
    VALUES ( 'Date'[Date].[Month] ),
    CALCULATE (
        SUMX (
            CROSSJOIN ( Data, VALUES ('Date'[Date] ) ),
            VAR CurrentDate = 'Date'[Date]
            VAR FromDate = Data[From date]
            VAR ToDate = Data[To date]
            RETURN
                IF ( CurrentDate >= FromDate && CurrentDate <= ToDate, 1 )
        )
    )
)

 

amitchandak
Super User
Super User

@ktt777 , Create a new column like

 

COUNTROWS(FILTER(ADDCOLUMNS(CALENDAR(Table[from Date],Table[to Date]),"WorkDay", if(WEEKDAY([Date],2) <6,1,0)),[WorkDay] =1))

 

How to calculate Business Days/ Workdays, with or without date table: https://youtu.be/Qv4wT8_P-AA

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.