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
bmk
Helper II
Helper II

DAX Date Table : Week Range - Sorting issue

Hello,

 

I'm using the below given Date Table in my report (open to suggestions to optimize/tweak if needed). I am currently using Date, Month, Week Range as the drill down heirarchy.

 

The Date and Month fields sorts correctly. However, when I drill down to Week Range, the sorting is all mixed up.

Yes, the data type for both Month and WeekRange is Text at the moment. I figure this is/maybe the reason that the sorting is done incorrectly, and not technically by the month's integer equivalent.

Is there a way to make sure sorting is done correctly without affecting the whole table?

 

Date Table =
ADDCOLUMNS (
CALENDAR (
DATE ( YEAR ( today() -365), MONTH ( today() - 365), DAY ( today()- 365) ),
DATE ( YEAR ( today() ), MONTH ( today() ), DAY ( today() ) )
),
"DateAsInteger", FORMAT ( [Date], "YYYYMMDD" ),
"Year", YEAR ( [Date] ),
"Monthnumber", FORMAT ( [Date], "MM" ),
"YearMonthnumber", FORMAT ( [Date], "YYYY/MM" ),
"YearMonthShort", FORMAT ( [Date], "YYYY/mmm" ),
"Month", FORMAT ( [Date], "mmm" ),
"MonthNameLong", FORMAT ( [Date], "mmmm" ),
"DayOfWeekNumber", WEEKDAY ( [Date] ),
"DayOfWeek", FORMAT ( [Date], "dddd" ),
"DayOfWeekShort", FORMAT ( [Date], "ddd" ),
"Quarter", "Q" & FORMAT ( [Date], "Q" ),
"YearQuarter", FORMAT ( [Date], "YYYY" ) & "/Q"
& FORMAT ( [Date], "Q" ),
"Start of Week",[Date] - WEEKDAY([Date],2) +1,
"Week Ending",[Date] + 7 - WEEKDAY([DATE],2),
"Week Range",[Date] - WEEKDAY([Date],2) +1&" - "&[Date] + 7 - WEEKDAY([DATE],2),
"MonthYear", FORMAT ( [Date], "mmmm" )&"/"&YEAR ( [Date] ),
"WeekNum",weeknum([Date],2)-1
)
1 ACCEPTED SOLUTION

Hi @bmk ,

 

This error is because the column which used to sort the other column need to be the unique, the error is here (In my PBIX file, it can be seen in the ERRORHERE Table):

vyinliwmsft_0-1668668641863.png

 

So we can do a little change to the column Weeknum,

New a column:

 

Index =
IF (
    'Date Table'[Date] >= DATE ( 2021, 12, 27 )
        && 'Date Table'[Date] <= DATE ( 2022, 1, 2 ),
    152,
    ( YEAR ( 'Date Table'[Date] ) - 2020 ) * 100 + 'Date Table'[WeekNum]
)

 

You can sort by the Index column.

 

The result is:

vyinliwmsft_1-1668668641873.png

 

 

Here is my PBIX file.

Hope this helps you.

 

Best Regards,

Community Support Team _Yinliw

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
bmk
Helper II
Helper II

Great to see your reply, Greg. Been following your posts on the PBI Totals issue, quite frustrating for me too.

I did try to sort out by WeekNum and few other columns, and I get the following error: 

bmk_0-1668623425068.png


However, sorting by 'Start of Week' looks like it works. I'm just not sure if it would fail any edge case(s) yet. Usually depends on that one user trying to get data for a specific date period.

 

Hi @bmk ,

 

This error is because the column which used to sort the other column need to be the unique, the error is here (In my PBIX file, it can be seen in the ERRORHERE Table):

vyinliwmsft_0-1668668641863.png

 

So we can do a little change to the column Weeknum,

New a column:

 

Index =
IF (
    'Date Table'[Date] >= DATE ( 2021, 12, 27 )
        && 'Date Table'[Date] <= DATE ( 2022, 1, 2 ),
    152,
    ( YEAR ( 'Date Table'[Date] ) - 2020 ) * 100 + 'Date Table'[WeekNum]
)

 

You can sort by the Index column.

 

The result is:

vyinliwmsft_1-1668668641873.png

 

 

Here is my PBIX file.

Hope this helps you.

 

Best Regards,

Community Support Team _Yinliw

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you. Could you please explain the logic of the Index measure here? I did not follow the IF condition completely. 

Hi @bmk ,

 

I post a formula here:

vyinliwmsft_0-1668734376305.png

This is to calculate a unique column to avoid the same value, but in the week 12/27/2021 - 1/2/2022, it has a same value, so, I create a value 152 to prescribe a rule. Because I use the Year function, and this week have two years.

 

That is the logic of the formula. Just for sorting.

 

In my file, you can see two other tables to explain this.

 

Best Regards,

Community Support Team _Yinliw

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Greg_Deckler
Super User
Super User

@bmk Can you Sort By WeekNum?


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.