Forum Discussion

P0ach3r1's avatar
P0ach3r1
Frequent Visitor
2 years ago
Solved

How many Mondays in month

Hi All,

 

I've searched the forum and found some solutions to this but the closest that works is here:

 

https://community.fabric.microsoft.com/t5/Power-Query/How-Many-Certain-Weekdays-are-in-a-Month/m-p/2067485#M60863

 

Old thread so I didn't want to re-open.

 

I've pinched Jakinta's solution and reworked it:

 

= Table.AddColumn(#"Added Custom5", "MondaysCount", each let s=Number.From(Date.StartOfMonth(Date.From([Start of Week]))),
e=Number.From(Date.EndOfMonth(Date.From([Start of Week]))), Dates=List.Transform({s..e}, Date.From)
in List.Count( List.Select( Dates, each Date.DayOfWeek(_)=1 ) ))

 

So it's working for all months apart from July/August.  Sample data is below - any idea why it thinks there are 5 mondays in August?  

 

Thanks

 

Start of WeekDistinctCountSource_NameMonthYearStartOfMonthMondaysCount
12/06/202312023-0601-Jun-234
19/06/202312023-0601-Jun-234
26/06/202312023-0601-Jun-234
21/08/202312023-0801-Aug-235
28/08/202312023-0801-Aug-235
04/09/202312023-0901-Sep-234
02/10/202312023-1001-Oct-235
31/07/202312023-0701-Jul-234
01/05/202312023-0501-May-235
18/09/202312023-0901-Sep-234
17/07/202312023-0701-Jul-234
17/04/202312023-0401-Apr-234

 

 

3 Replies

  • HotChilli's avatar
    HotChilli
    Icon for Community Champion rankCommunity Champion

    The dayofWeek test (Date.DayOfWeek(_)=1 )is looking for 1 so there's probably a cultural problem for which day is the start of the week.  Why don't you replace that with a test for Monday explicitly?

    Date.DayOfWeekName(_) = "Monday"

     

  • P0ach3r1's avatar
    P0ach3r1
    Frequent Visitor

    Hi Both,

     

    Thanks for replying.  looks like 0 does the trick (Monday returned blanks) - but problem now fixed.

     

    Thanks