Forum Discussion

Emmy88_'s avatar
Emmy88_
Icon for Advocate I rankAdvocate I
7 months ago
Solved

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
    202601

     

    3. Sort YearWeek by WeekIndex

    In Model view
    Select YearWeek
    Sort by column โ†’ WeekIndex

     

    4. 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 metric

    Filters
    Is Last 5 Weeks = 1

     

    Please mark it as a solution with headup if this helps you. Thank You!

4 Replies

  • 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
    202601

     

    3. Sort YearWeek by WeekIndex

    In Model view
    Select YearWeek
    Sort by column โ†’ WeekIndex

     

    4. 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 metric

    Filters
    Is Last 5 Weeks = 1

     

    Please mark it as a solution with headup if this helps you. Thank You!

  • 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.

  • Hi Emmy88_ 

    Change your year week column to something like 202601 instead of 20261 for it to be sorted correctly.