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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
kollasv
Helper I
Helper I

How to get number of weeks in a month

Hi all, 

i have a table and a date and a month columns are there in that table

How can i get number of weeks in that partucular month?

Example:

2022 Jan    5 weeks

2022 Feb   4 weeks

2022 Mar  4 weeks

like this i need to create a calculated column automatically,,is there any way to create like this in power bi, please tell me, please help me please....

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

Weeks = var __SoM = EOMONTH([Date],-1)+1 return CEILING((EOMONTH([Date],0)-__SoM+WEEKDAY(__SoM,2)-6)/7,1)

CNENFRNL_0-1660418621588.png


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!

View solution in original post

12 REPLIES 12
speedramps
Super User
Super User

Create calandar table:-

Calendar =
CALENDAR(DATE(2021,01,01), DATE(2023,12,31))
 
Add columns to the table:-
MONTH = MONTH('Calendar'[Date])
YEAR = YEAR('Calendar'[Date])
Week commencing on Sunday =  'Calendar'[Date] - WEEKDAY('Calendar'[Date],1) + 1
 
Alter the formular if you want the week to start on on another day
WEEKDAY('Calendar'[Date],1) = Sunday,  2= Monday. 3 = Tuesday etc etc

Create  dax measure
Week count =
VAR mysubsetFILTER('Calendar',YEAR('Calendar'[Week commencing on Monday]) = 'Calendar'[Year])
RETURN
CALCULATE(
DISTINCTCOUNT('Calendar'[Week commencing on Sunday]),
mysubset)
 
Create a table visual and drag ..
YEAR
MONTH
Week count
 
Voila you have your answer !

 

Please click thumbs up and accept as solution

 
CNENFRNL
Community Champion
Community Champion

Weeks = var __SoM = EOMONTH([Date],-1)+1 return CEILING((EOMONTH([Date],0)-__SoM+WEEKDAY(__SoM,2)-6)/7,1)

CNENFRNL_0-1660418621588.png


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!

@CNENFRNL Could you break down the different parts of your calculation for me? It seems really close for what I'm trying to achieve. However, I would like calculate the number of Fridays in a given month.

 

For example, if my Date column says '1/4/2023' the resulting number should be 4, as January 2023 has 4 Fridays in the month. If my Date column says '3/8/2023' the resulting number should be 5, as March 2023 has 5 Fridays in the month.

 

What do I need to change here to make it work for the number of Fridays in a month?

 

Weeks = var __SoM = EOMONTH([Date],-1)+1 return CEILING((EOMONTH([Date],0)-__SoM+WEEKDAY(__SoM,2)-6)/7,1)

Actually, I think I figured it out. The WEEKDAY "Return Type" was set to 2, which has Sunday as the last day of the week. I changed it to "16" which has Friday as the end of the week and that appears to work.

Thankyou sooo muchhhhh

speedramps
Super User
Super User

Try this ...

 

Create calandar table:-

Calendar =
CALENDAR(DATE(2021,01,01), DATE(2023,12,31))
 
Add columns to the table:-
MONTH = MONTH('Calendar'[Date])
YEAR = YEAR('Calendar'[Date])
Week commencing on Monday =  'Calendar'[Date] - WEEKDAY('Calendar'[Date],2) + 1
all the formular if you want the week to start on Sunday


Create  dax measure
Week count =
VAR mysubset= FILTER('Calendar',YEAR('Calendar'[Week commencing on Monday]) = 'Calendar'[Year])
RETURN
CALCULATE(
DISTINCTCOUNT('Calendar'[Week commencing on Monday]),
mysubset)
 
Create a table visual and drag ..
YEAR
MONTH
Week count
 
speedramps_0-1660390648059.png

 

 

 

Thanks for reaching out for help.

I put in a lot of effort to help you, now please quickly help me by giving kudos.

Remeber we are unpaid volunteers and here to coach you with Power BI and DAX skills and techniques, not do the users job for them. So please click the thumbs up and accept as solution button. 

If you give someone a fish then you only give them one meal, but if you teach them how to fish then they can feed themselves and teach others for a lifetime.  I prefer to teach members on this forum techniques rather give full solutions and do their job. You can then adapt the technique for your solution, learn some DAX skills for next time and soon become a Power BI Super User like me. 

One question per ticket please. If you need to extend your request then please raise a new ticket.

You will get a quicker response and each volunteer solver will get the kudos they deserve. Thank you ! 

 
 
 

Hi speedramps

May I know how to get numbers of Mondays, Tuesdays, Wednesdays, Thursdays, Fridays, Satursdays, and Sundays of each month / select date (need auto count). TIA

Hi  Thankyou so much for the valuable time and solution.,,,i want to create a column of number of weeks in the existed table only and i want to calculate number of full weeks in a month which were start with sunday and end with saturday

If you see 2022 Jan 1 st comes on saturday that is comes under 2021 december week, so if i want to calculate 2022 Jan weeks, it starts with sunday i.e.2 nd jan, it means 2 to 8th is one week,9th-15th is second week, 16-22 third week, 23rd to 29th is fourth week ,30 to feb 5th is fifth week (In Jan 5 weeks),,

In Feb first week start from sunday i.e 6th to 12th is first week ,13th to 19th is second week, 20th to 26th third week, 27th to Mar 5th is fourth week(In Feb 4 weeks) like that i want to calculate, please help me 

2022-calendar.png

Jihwan_Kim
Super User
Super User

Hi,

I am not sure if I understood your question correctly.

Please check the below picture and the attached pbix file.

It is for creating a measure.

 

Picture1.png

 

Weeknumber count: =
COUNTROWS (
    SUMMARIZE (
        ADDCOLUMNS (
            VALUES ( 'Calendar'[Date] ),
            "@weeknumber", WEEKNUM ( CALCULATE ( MAX ( 'Calendar'[Date] ) ), 21 )
        ),
        [@weeknumber]
    )
)

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Hi  Thankyou so much for the valuable time and solution.,,,i want to create a column of number of weeks in the existed table only and i want to calculate number of full weeks in a month which were start with sunday and end with saturday

If you see 2022 Jan 1 st comes on saturday that is comes under 2021 december week, so if i want to calculate 2022 Jan weeks, it starts with sunday i.e.2 nd jan, it means 2 to 8th is one week,9th-15th is second week, 16-22 third week, 23rd to 29th is fourth week ,30 to feb 5th is fifth week (In Jan 5 weeks),,

In Feb first week start from sunday i.e 6th to 12th is first week ,13th to 19th is second week, 20th to 26th third week, 27th to Mar 5th is fourth week(In Feb 4 weeks) like that i want to calculate, please help me 

2022-calendar.png

tamerj1
Super User
Super User

Hi @kollasv 
Do you want to create it in a date table? I mean what is the granularity of the table that you want to create the column in?

 

Hi i want to create a column of number of weeks in the existed table only and i want to calculate number of full weeks in a month which were start with sunday and end with saturday

If you see 2022 Jan 1 st comes on saturday that is comes under 2021 december week, so if i want to calculate 2022 Jan weeks, it starts with sunday i.e.2 nd jan, it means 2 to 8th is one week,9th-15th is second week, 16-22 third week, 23rd to 29th is fourth week ,30 to feb 5th is fifth week (In Jan 5 weeks),,

In Feb first week start from sunday i.e 6th to 12th is first week ,13th to 19th is second week, 20th to 26th third week, 27th to Mar 5th is fourth week(In Feb 4 weeks) like that i want to calculate, please help me professionals....

2022-calendar.png

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.