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
rodrigooliveira
Frequent Visitor

Date Table with end of month variable

Hi,

 

In my organization, the end of the month can change based on the last day of the month.

If the last day of the month is between Thursday and Friday, the month ends the next Saturday.

If the last day is between Sunday and Wednesday, the month ends on Saturday before.

 

Ex: November 2020 - Last day of the month is Monday, so the month ends the Saturday before (28th November). This means 29 and 30th November are part of December.

For December 2020: Last day of the month is Thursday, so the month ends the Saturday after (2nd January). This means 1st and 2nd January are part of December, and so on.

 

I'm using a simple date table, however, I don't if it is possible to integrate the month logic of my organization inside it.

Any help will be amazing, even if it is to say that is better to create a date table by hand.

 

Dates = 
Var BaseCalendar = 
    CALENDAR("1/1/2017", "31/12/2021")
RETURN
    GENERATE(
        BaseCalendar,
        Var BaseDate = [Date]
        Var YearDate = YEAR(BaseDate)
        Var MonthNumber = MONTH(BaseDate)
        Var WeekDay =  [Date]
        Var WeekNumber = WEEKNUM(BaseDate,21)
        //Var FiscalQuarter = QUARTER(5) //Regler Fiscal Year si différent de 1
        Var CalendarQuarter = QUARTER(BaseDate)
        Var SameDateLY = DATE(YEAR(BaseDate)-1,MONTH(BaseDate), DAY(BaseDate))
        Var SameDateBLY = DATE(YEAR(BaseDate)-2,MONTH(BaseDate), DAY(BaseDate))

        RETURN ROW(
            "Day", BaseDate,
            "Year", YearDate,
            "Month Number", MonthNumber,
            "Month", FORMAT(BaseDate, "mmmm"),
            "Year Month", FORMAT(BaseDate, "mmm yy"),
            "Day Week", FORMAT(WeekDay, "dddd"),
            "Week Number", WeekNumber,
            //"Fiscal Quarter", FiscalQuarter,
            "Calendar Quarter", CalendarQuarter,
            "Same Date Last Year", SameDateLY,
            "Same Date Before Last Year", SameDateBLY
            )
    )

 

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

Bonjour, @rodrigooliveira , c'est un véritable casse-tête, n'est-ce pas? Essaie la formule dans une colonne calculée,

Month Adapted = 
VAR __weeknum = WEEKNUM ( [Date] )
VAR __day = DAY ( [Date] )
VAR __month_natural = MONTH ( [Date] )
VAR __eom = IF ( __day < 7, EOMONTH ( [Date], -1 ), EOMONTH ( [Date], 0 ) )
RETURN
    IF (//the very last week of a month
        __weeknum = WEEKNUM ( EOMONTH ( [Date], 0 ) ) //within the last week of a month
            || __weeknum = MOD ( WEEKNUM ( [Date] - __day ), 52 ), //weeknum of previous end of month
        IF (
            WEEKDAY ( __eom ) >= 5,
            MONTH ( __eom ),
            MONTH ( __eom + 1 )
        ),
        __month_natural
    )

Screenshot 2020-12-11 204118.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

2 REPLIES 2
CNENFRNL
Community Champion
Community Champion

Bonjour, @rodrigooliveira , c'est un véritable casse-tête, n'est-ce pas? Essaie la formule dans une colonne calculée,

Month Adapted = 
VAR __weeknum = WEEKNUM ( [Date] )
VAR __day = DAY ( [Date] )
VAR __month_natural = MONTH ( [Date] )
VAR __eom = IF ( __day < 7, EOMONTH ( [Date], -1 ), EOMONTH ( [Date], 0 ) )
RETURN
    IF (//the very last week of a month
        __weeknum = WEEKNUM ( EOMONTH ( [Date], 0 ) ) //within the last week of a month
            || __weeknum = MOD ( WEEKNUM ( [Date] - __day ), 52 ), //weeknum of previous end of month
        IF (
            WEEKDAY ( __eom ) >= 5,
            MONTH ( __eom ),
            MONTH ( __eom + 1 )
        ),
        __month_natural
    )

Screenshot 2020-12-11 204118.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!

Merci beaucoup! Thank you!

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