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
lawadaa
Helper I
Helper I

count number of days in a month week

I'm trying to create a column that calculates the number of days within a week in month. for example week1 of june has the following dates: 1-6-2023 till 3-6-2023 so number of days =3 

w2 dates 4-6-2023 till 10-6-2023 so number of days =7 

and so on for every week in the month

1 ACCEPTED SOLUTION

Hi @lawadaa ,

 

(1) We can create a calculated column.

WeekOfMonth = 
VAR WeekStart = 0 
VAR CurrentDate = 'DateTable'[Date]
VAR MonthStart = DATE(YEAR(CurrentDate), MONTH(CurrentDate), 1)
VAR MonthEnd = EOMONTH(CurrentDate, 0)
VAR WeekNumber = INT((CurrentDate - MonthStart + WEEKDAY(MonthStart - WeekStart) + 6) / 7)
RETURN WeekNumber

(2) We can create a measure.

DaysInWeekOfMonth = 
VAR SelectedWeek = SELECTEDVALUE('DateTable'[WeekOfMonth])
VAR MinDate = CALCULATE(MIN('DateTable'[Date]), 'DateTable'[WeekOfMonth] = SelectedWeek)
VAR MaxDate = CALCULATE(MAX('DateTable'[Date]), 'DateTable'[WeekOfMonth] = SelectedWeek)
RETURN IF(ISBLANK(SelectedWeek), BLANK(), DATEDIFF(MinDate, MaxDate, DAY) + 1)

(3)Then the result is as follows.

vtangjiemsft_0-1686535933858.png

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

View solution in original post

3 REPLIES 3
v-tangjie-msft
Community Support
Community Support

Hi @lawadaa ,

 

According to your description, here are my steps you can follow as a solution.

(1) We can create a date table.

 

DateTable = CALENDAR(DATE(2023,1,1),DATE(2023,12,31))

 

(2) We can create two calculated columns.

 

WeekNumber = WEEKNUM([Date], 2)
MonthNumber = MONTH('DateTable'[Date]) 

 

(3) We can create a measure.

 

DaysInWeekInMonth = 
VAR SelectedMonth = SELECTEDVALUE('DateTable'[MonthNumber])
VAR SelectedWeek = SELECTEDVALUE('DateTable'[WeekNumber])
RETURN
CALCULATE(
    COUNTROWS('DateTable'),
    FILTER(
        'DateTable',
        'DateTable'[MonthNumber] = SelectedMonth &&
        'DateTable'[WeekNumber] = SelectedWeek
    )
)

 

(4)Then the result is as follows.

vtangjiemsft_0-1686275901909.png

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

this measure calculates the number of days in yearly week not in a monthly week. and i need the week to start from sunday to saturday not monday to sunday

Hi @lawadaa ,

 

(1) We can create a calculated column.

WeekOfMonth = 
VAR WeekStart = 0 
VAR CurrentDate = 'DateTable'[Date]
VAR MonthStart = DATE(YEAR(CurrentDate), MONTH(CurrentDate), 1)
VAR MonthEnd = EOMONTH(CurrentDate, 0)
VAR WeekNumber = INT((CurrentDate - MonthStart + WEEKDAY(MonthStart - WeekStart) + 6) / 7)
RETURN WeekNumber

(2) We can create a measure.

DaysInWeekOfMonth = 
VAR SelectedWeek = SELECTEDVALUE('DateTable'[WeekOfMonth])
VAR MinDate = CALCULATE(MIN('DateTable'[Date]), 'DateTable'[WeekOfMonth] = SelectedWeek)
VAR MaxDate = CALCULATE(MAX('DateTable'[Date]), 'DateTable'[WeekOfMonth] = SelectedWeek)
RETURN IF(ISBLANK(SelectedWeek), BLANK(), DATEDIFF(MinDate, MaxDate, DAY) + 1)

(3)Then the result is as follows.

vtangjiemsft_0-1686535933858.png

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

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.

Top Solution Authors