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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
PaulBoden
Helper I
Helper I

FOREACH / WHERE Statement in DAX

Hi All,

 

Feels like this should be straight-forward but I'm struggling with how to apply a calculation to sub-groups in my table without a WHERE statement. 

 

I'm trying to identify how many weeks there are in each calendar month but the closest I've been able to get is the max number of weeks that can occur in any calendar month.

 

What I'm looking for is something resembling the following:

FOREACH [YearMonth] MAX(Weeks in Month)

 

Basic Table Format:

Master Date                 Year          Fisc.Yr      Quarter     Fisc. Qtr  Month    Week       Day    YearMonth  

01/01/202220222022131112022_1
02/01/202220222022131222022_1
03/01/202220222022131232022_1

...

 

Any advice would be greatly appreciated.

 

 

2 ACCEPTED SOLUTIONS
AilleryO
Memorable Member
Memorable Member

Hi,

 

In DAX it could be straightforward :

If you make a table with YearMonth (in lines) and Week number
with a COUNT or DISTINCTCOUNT as calculation, you'll get it.

 

Or a DAX measure could be something like :

# of Week = CALCULATE( DISTINCTCOUNT( MonCalendrier[NumSemISO] ) , 
ALLEXCEPT( MonCalendrier , MonCalendrier[Annee-Mois] ) )

 

Tell us if it works and mark it as solved or tell us more about your issue.

 

View solution in original post

Yes, not sure how I missed it - suspect I was overcomplicating things 🙂 - but the below seems to have worked:

 

Weeks in Month = CALCULATE(DISTINCTCOUNT([Week of Month]), ALLEXCEPT('DateMaster', DateMaster[Fiscal YearMonth]))

View solution in original post

2 REPLIES 2
AilleryO
Memorable Member
Memorable Member

Hi,

 

In DAX it could be straightforward :

If you make a table with YearMonth (in lines) and Week number
with a COUNT or DISTINCTCOUNT as calculation, you'll get it.

 

Or a DAX measure could be something like :

# of Week = CALCULATE( DISTINCTCOUNT( MonCalendrier[NumSemISO] ) , 
ALLEXCEPT( MonCalendrier , MonCalendrier[Annee-Mois] ) )

 

Tell us if it works and mark it as solved or tell us more about your issue.

 

Yes, not sure how I missed it - suspect I was overcomplicating things 🙂 - but the below seems to have worked:

 

Weeks in Month = CALCULATE(DISTINCTCOUNT([Week of Month]), ALLEXCEPT('DateMaster', DateMaster[Fiscal YearMonth]))

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors
Users online (7,304)