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
Kalnnie
Regular Visitor

Calculate the week number from the last day of the week

Hi everyone

 

I'm trying to create a calendar where the smallest unit is the week, but that still uses dates in the background.

all works fine until I add the month and year column, then I start to get double entries.

Kalnnie_0-1669990306635.png

Does anyone know how to make Powerbi calculate the week number based on the last day of the week (Sunday of that week in my case) so that if a week overlaps two months, only one entry will apear corresponding to the latter month?

 

Here is the query:

 

Period Table =
VAR _MinDate =
    DATE ( 2022,08,01 )
VAR _MaxDate =
    TODAY ()
VAR _CalTable =
    CALENDAR ( _MinDate, _MaxDate )
VAR _YW =
    SELECTCOLUMNS (
        _CalTable,
        "WeekNum", WEEKNUM ( [Date] ,21),
        "Month",MONTH( [Date]),
        "Year", YEAR ( [Date] ),
        "YearWeek",
            YEAR ( [Date] ) * 100
                + WEEKNUM ( [Date] ,21)
    )
RETURN
    SUMMARIZE (
        ADDCOLUMNS ( _YW, "PeriodIndex", RANKX ( _YW, [YearWeek],, ASC, DENSE ) ),
        [YearWeek],
        [Month],
        [WeekNum],
        [Year]
    )
 
Thank you in advance for you support!
Kalnnie
2 REPLIES 2
Kalnnie
Regular Visitor

I actually got the desired outcome by creating a calendar table in powerquery, filtering only the sundays (the last day of the week in my case), then adding the required columns in DAX. What I got was a calendar with the weeknumbers as the lowest granularity level, but without the repeating values. 

 

Kalnnie_0-1670513420828.png

 

lbendlin
Super User
Super User

You are looking for WEEKDAY function (DAX) - DAX | Microsoft Learn

 

My advice would be to compute this outside of Power BI, in a static reference table.

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.