Forum Discussion
Weekly data from one year to another in visual
Hi Support,
I am looking for a solution on showing weekly data in a column visual for the previous 5 calendar weeks.
I tried to do by adding a year week structure, but i cannot seem to sort this column on date.
Currently showing the underneath
How to set it up to see the past 5 weeks in chronological order?
Thank you,
Emmy
Hi , Emmy88_
You currently have a YearWeek column (something like 202501, 202502, etc.), but Power BI is treating it as text, so sorting is alphabetical instead of chronological. Thatโs why the order looks wrong.
โ How to Fix It and Show the Last 5 Calendar Weeks in Order
There are two main steps:
1. Create a Proper Date Column for Sorting
Instead of sorting by the YearWeek text, create a column that represents the start date of the week. This will allow correct chronological sorting.
In Power Query (recommended for performance):
// Assuming you have a Date column AddColumn = Table.AddColumn(PreviousStep, "WeekStart", each Date.StartOfWeek([Date], Day.Monday), type date)Or in DAX (if you prefer calculated column):
WeekStart = STARTOFWEEK('Table'[Date], 2) // 2 = Monday
Use a relative filter for the Last 5 Weeks:
In the visual filter pane, choose Relative Date โ Last 5 weeks.
This works if you have a proper Date column in your model.
If this response was helpful in any way, Iโd gladly accept a ๐much like the joy of seeing a DAX measure work first time without needing another FILTER.
Please mark it as the correct solution. It helps other community members find their way faster (and saves them from another endless loop ๐.
Hi Emmy88_
1. Create a Week Sort Index in the Date table
You already have the right idea with Index, this is key.
If you donโt already have it, create
WeekIndex =
RANKX(
ALL('Date'),
'Date'[Date],
,
ASC,
DENSE
)2. Create a Year Week display column
This is what users will see:
YearWeek =
'Date'[Year] * 100 + 'Date'[Week]
Example:
202544
202545
2026013. Sort YearWeek by WeekIndex
In Model view
Select YearWeek
Sort by column โ WeekIndex4. Filter to Last 5 Calendar Weeks
Create a measure
Is Last 5 Weeks =
VAR MaxWeekIndex =
CALCULATE(
MAX('Date'[WeekIndex]),
ALL('Date')
)
RETURN
IF(
'Date'[WeekIndex] >= MaxWeekIndex - 4,
1,
0
)
Apply this as a visual-level filter = 1.5. Build the Column Chart
X-axis
Date[YearWeek]Values
Your metricFilters
Is Last 5 Weeks = 1Please mark it as a solution with headup if this helps you. Thank You!
4 Replies
- Zanqueta
Super User
Hi , Emmy88_
You currently have a YearWeek column (something like 202501, 202502, etc.), but Power BI is treating it as text, so sorting is alphabetical instead of chronological. Thatโs why the order looks wrong.
โ How to Fix It and Show the Last 5 Calendar Weeks in Order
There are two main steps:
1. Create a Proper Date Column for Sorting
Instead of sorting by the YearWeek text, create a column that represents the start date of the week. This will allow correct chronological sorting.
In Power Query (recommended for performance):
// Assuming you have a Date column AddColumn = Table.AddColumn(PreviousStep, "WeekStart", each Date.StartOfWeek([Date], Day.Monday), type date)Or in DAX (if you prefer calculated column):
WeekStart = STARTOFWEEK('Table'[Date], 2) // 2 = Monday
Use a relative filter for the Last 5 Weeks:
In the visual filter pane, choose Relative Date โ Last 5 weeks.
This works if you have a proper Date column in your model.
If this response was helpful in any way, Iโd gladly accept a ๐much like the joy of seeing a DAX measure work first time without needing another FILTER.
Please mark it as the correct solution. It helps other community members find their way faster (and saves them from another endless loop ๐.
- krishnakanth240
Super User
Hi Emmy88_
1. Create a Week Sort Index in the Date table
You already have the right idea with Index, this is key.
If you donโt already have it, create
WeekIndex =
RANKX(
ALL('Date'),
'Date'[Date],
,
ASC,
DENSE
)2. Create a Year Week display column
This is what users will see:
YearWeek =
'Date'[Year] * 100 + 'Date'[Week]
Example:
202544
202545
2026013. Sort YearWeek by WeekIndex
In Model view
Select YearWeek
Sort by column โ WeekIndex4. Filter to Last 5 Calendar Weeks
Create a measure
Is Last 5 Weeks =
VAR MaxWeekIndex =
CALCULATE(
MAX('Date'[WeekIndex]),
ALL('Date')
)
RETURN
IF(
'Date'[WeekIndex] >= MaxWeekIndex - 4,
1,
0
)
Apply this as a visual-level filter = 1.5. Build the Column Chart
X-axis
Date[YearWeek]Values
Your metricFilters
Is Last 5 Weeks = 1Please mark it as a solution with headup if this helps you. Thank You!
- Ashish_Mathur
Super User
Hi,
Drag just the week column (the column to the left of the one highlighted in the second image of your post) to the X -axis.