same week last year with DAX

Get data from same week last year in Power BI

Here is how to get data from the same week last year in Power BI using DAX measure. I recommend using ISO week and year numbering to get an equal number of days each week and distinguish each year. Sometimes, when you have an unequal number of weeks in years, it might not be easy, and I will show you how to deal with that.

The weekly dimension is not my favorite way how to group my data. On the other hand, if you have weekly seasonality, then it might look better in charts. You might be interested in looking at your data using grouping by ISO weeks or 7-day moving average.

To demonstrate the technique, I will generate data in Power Query – dates for a couple of years, ISO week and year, and random numbers. You can recreate this data set by using this M code in a blank query. The code is below this post.

generate data in Power Query

Get data from the same week last year in Power BI

1. Create a column with week and year combinations that will help to calculate previous year numbers and calculate data from certain rows. It might not be useful for sorting data but in this situation is what you need.

WY Key = 'my table'[ISO Week] * 10000 + 'my table'[ISO Year]

year and week number combination DAX Power BI

2. Create a measure that will calculate data from the same week last year.

Weekly Data Previous YearX = 
CALCULATE (
    SUM ( 'my table'[Values] ),
    FILTER (
        ALL ( 'my table' ),
        'my table'[WY Key]
            = MAX ( 'my table'[WY Key] ) - 1
    )
)

Weekly data comparison issue with week number 53

Let’s take a look at the results in the diagram. As you can see in the picture below, there is no previous year’s data for week 53. The year 2019 doesn’t have that.

Weekly data comparison issue with week number 53

If it is alright for you, then you can leave it as it is. Or if you want to repeat results from week 52 and get the line uninterrupted, it could be done like this.

Weekly Data Previous Year = 
VAR wdpy1 =
    CALCULATE (
        SUM ( 'my table'[Values] ),
        FILTER (
            ALL ( 'my table' ),
            'my table'[WY Key]
                = MAX ( 'my table'[WY Key] ) - 1
        )
    )
VAR wdpy2 =
    CALCULATE (
        SUM ( 'my table'[Values] ),
        FILTER (
            ALL ( 'my table' ),
            'my table'[WY Key]
                = MAX ( 'my table'[WY Key] ) - 1 - 10000
        )
    )
RETURN
    IF ( ISBLANK ( wdpy1 ), wdpy2, wdpy1 )

same week last year with DAX

For the measures at this length, I recommend the DAX formatter.

Here are some useful tips on how to organize measures in Power BI.

Data sample M code.

let
    Source = List.Dates(#date(2019, 1, 1), 365 * 3, #duration(1, 0, 0, 0)),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Date"}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}}),
    #"Added ISO Week" = Table.AddColumn(#"Changed Type", "ISO Week", each if
Number.RoundDown((Date.DayOfYear([Date])-(Date.DayOfWeek([Date], Day.Monday)+1)+10)/7)=0

then 
Number.RoundDown((Date.DayOfYear(#date(Date.Year([Date])-1,12,31))-(Date.DayOfWeek(#date(Date.Year([Date])-1,12,31), Day.Monday)+1)+10)/7)

else if
(Number.RoundDown((Date.DayOfYear([Date])-(Date.DayOfWeek([Date], Day.Monday)+1)+10)/7)=53
and (Date.DayOfWeek(#date(Date.Year([Date]),12,31), Day.Monday)+1<4))

then
1

else 
Number.RoundDown((Date.DayOfYear([Date])-(Date.DayOfWeek([Date], Day.Monday)+1)+10)/7)),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added ISO Week",{{"ISO Week", Int64.Type}}),
    #"Added ISO Year" = Table.AddColumn(#"Changed Type1", "ISO Year", each Date.Year(Date.AddDays([Date], 26 - [ISO Week]))),
    #"Changed Type2" = Table.TransformColumnTypes(#"Added ISO Year",{{"ISO Year", Int64.Type}}),
    #"Added random Values" = Table.AddColumn(#"Changed Type2", "Values", each List.Random(1)),
    #"Expanded Values" = Table.ExpandListColumn(#"Added random Values", "Values"),
    #"Multiplied Column" = Table.TransformColumns(#"Expanded Values", {{"Values", each _ * 100, type number}}),
    #"Rounded Off" = Table.TransformColumns(#"Multiplied Column",{{"Values", each Number.Round(_, 0), type number}})
in
    #"Rounded Off"

 





Posted

in

,

Comments

4 responses to “Get data from same week last year in Power BI”

  1. Adam Fuehrer

    Hello, this solution looks perfect for what I need on a report I’m working on. I’m not able to make it work, though. When I try I am getting a blank column. My project is a bit different and I’m wondering if that’s why I’m not getting the correct results. I have a table of data for 46 KPIs and need to get the value from the same week in the prior year for each. Can you help me modify the formula for my use case? I tried using ALLEXCEPT instead of ALL but, that didn’t work. Thanks for any help!

    1. Janis Sturis

      Hello

      Are there unique dates in the table?
      If not, add a date table in your data model and use that in calculations.

      Here is more about that:
      https://docs.microsoft.com/en-us/power-bi/guidance/model-date-tables
      https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

      1. Adam Fuehrer

        Yes, I do have a Calendar table in the model. I’m wondering, do I need to create a surrogate key between the Calendar and Fact tables? I went down that road for a bit but, didn’t have the best luck. I know it’s a bit difficult to troubleshoot something like this over comments. Any extra ideas would be greatly appreciated. Thank you!

  2. Nilima

    This was really helpful. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *