Forum Discussion

Analitika's avatar
Analitika
Post Prodigy
4 years ago

Sorting year weeks in Power BI

Hello,

 

I would like to ask how to properly sort year week because in visual it seems unsorted:

Here is the view of it

 

Is possible to fix it?

4 Replies

  • ddpl's avatar
    ddpl
    Solution Sage

    Hi

    Changed data type as Date and format as below

     

      • rohit_singh's avatar
        rohit_singh
        Solution Sage

        Hi Analitika ,

        Please create a new column on your date table like below :

        YearWeekIndex = dim_date[Year] * 100 +dim_date[Week of Year]
        Then select your YearWeek column, go to sort by column and click on 'YearWeekIndex'. Your yearweek field will now be sorted using this column.

        Kind regards,

        Rohit


        Please mark this answer as the solution if it resolves your issue.
        Appreciate your kudos! ğŸ˜Š

  • Adescrit's avatar
    Adescrit
    Impactful Individual

    Hi Analitika ,

     

    Due to the "-" in the middle of the Year-Week number, that value is stored as text. This is the reason the sorting is not working for you.

     

    In order to sort it correctly, you can replace the Year-Week formula you are currently using with the following:

     

    Year-Week = 
        VAR __Year = Year( [Date] )
        VAR __WeekNum = WEEKNUM( [Date] )
    
    RETURN
        IF( __WeekNum < 10, CONCATENATE( __Year, "-0" & __WeekNum ), CONCATENATE( __Year, "-" & __WeekNum ))

     

    The difference is the "0" before any single-digit week numbers. This allows the sorting to work correctly.