Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
STS_Joshua
Helper II
Helper II

Trying to calculate SAMEPERIODLASTYEAR

Hi all, I recieve weekly sales data every saturday. I am looking for a way to compare this weekly data with the same weeknum from last year. SAMEPERIODLASTYEAR on a date table isn't doing the trick. This article: https://www.sqlbi.com/articles/previous-year-up-to-a-certain-date/ partially worked and I modified it slightly like so:

IsPast =
VAR LastSaleDate = MAX ( 'REIActivity'[period_date] )
VAR LastSaleDatePY = WEEKNUM(EDATE ( LastSaleDate, -12 ))
RETURN
WEEKNUM('Date'[Date]) <= LastSaleDatePY

I'm still having issues getting the measure to return the correct data:

period_datenet_sales_retailwkIsPastnet_sales_retail_ly
1/5/2019$235,7641TRUE 
1/5/2019 2TRUE$235,764
1/12/2019$198,2762TRUE 
1/12/2019 3TRUE$198,276
1/19/2019$235,1993TRUE 
1/19/2019 4TRUE$235,199
1/26/2019$221,9234TRUE 
1/26/2019 5TRUE$221,923
2/2/2019$208,8515TRUE 
2/2/2019 6TRUE$208,851
2/9/2019$265,5606TRUE 
2/9/2019 7TRUE$265,560
2/16/2019$285,2957TRUE 
2/16/2019 8TRUE$285,295
2/23/2019$287,5578TRUE 
2/23/2019 9TRUE$287,557
3/2/2019$324,8749TRUE 
3/2/2019 10TRUE$324,874
3/9/2019$412,76110TRUE 
3/9/2019 11TRUE$412,761
3/16/2019$537,96311TRUE 
3/16/2019 12FALSE$537,963
3/23/2019$489,68912FALSE 
3/30/2019$533,81713FALSE 
4/6/2019$568,52514FALSE 
4/13/2019$627,68515FALSE 
4/20/2019$444,76016FALSE 
4/27/2019$419,78117FALSE 
5/4/2019$462,33118FALSE 
5/11/2019$463,29219FALSE 
5/18/2019$810,19620FALSE 
5/25/2019########21FALSE 
6/1/2019########22FALSE 
6/8/2019$604,10423FALSE 
6/15/2019$681,88124FALSE 
6/22/2019$682,16525FALSE 
6/29/2019$757,04126FALSE 
7/6/2019$856,81227FALSE 
7/13/2019$727,92228FALSE 
7/20/2019$747,42029FALSE 
7/27/2019$696,06130FALSE 
8/3/2019$718,80131FALSE 
8/10/2019$688,78832FALSE 
8/17/2019$596,89733FALSE 
8/24/2019$618,75834FALSE 
8/31/2019$741,47535FALSE 
9/7/2019$597,32736FALSE 
9/14/2019$450,73637FALSE 
9/21/2019$406,75438FALSE 
9/28/2019$366,95939FALSE 
10/5/2019$341,10840FALSE 
10/12/2019$352,80141FALSE 
10/19/2019$301,74242FALSE 
10/26/2019$235,72243FALSE 
11/2/2019$232,71144FALSE 
11/9/2019$218,76745FALSE 
11/16/2019$227,20246FALSE 
11/23/2019$270,26047FALSE 
11/30/2019$250,79648FALSE 
12/7/2019$291,34549FALSE 
12/14/2019$390,51150FALSE 
12/21/2019$543,00351FALSE 
12/28/2019$403,67852FALSE 
1/4/2020$251,9741TRUE 
1/11/2020$211,1962TRUE 
1/18/2020$238,8143TRUE 
1/25/2020$220,4634TRUE 
2/1/2020$217,7775TRUE 
2/8/2020$224,5336TRUE 
2/15/2020$273,1717TRUE 
2/22/2020$264,8008TRUE 
2/29/2020$274,4379TRUE 
3/7/2020$345,53510TRUE 
3/14/2020$336,76311TRUE 

 

 

 

1 ACCEPTED SOLUTION
v-gizhi-msft
Community Support
Community Support

Hi,

 

Please try this measure:

Measure = 
VAR LastYearSales =
    CALCULATE (
        SUM ( 'Table'[net_sales_retail] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[wk] = SELECTEDVALUE ( 'Table'[wk] )
                && YEAR ( 'Table'[period_date] )
                    = YEAR ( SELECTEDVALUE ( 'Table'[period_date] ) ) - 1
        )
    )
RETURN
    IF (
        YEAR ( MIN ( 'Table'[period_date] ) )
            = YEAR ( MINX ( ALLSELECTED ( 'Table' ), 'Table'[period_date] ) ),
        BLANK (),
        ( MAX ( 'Table'[net_sales_retail] ) - LastYearSales ) / LastYearSales
    )

The result shows the increase/descrese compared with the last year same weekNo data:

56.PNG

 

Best Regards,

Giotto Zhi

View solution in original post

8 REPLIES 8
v-gizhi-msft
Community Support
Community Support

Hi,

 

Please try this measure:

Measure = 
VAR LastYearSales =
    CALCULATE (
        SUM ( 'Table'[net_sales_retail] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[wk] = SELECTEDVALUE ( 'Table'[wk] )
                && YEAR ( 'Table'[period_date] )
                    = YEAR ( SELECTEDVALUE ( 'Table'[period_date] ) ) - 1
        )
    )
RETURN
    IF (
        YEAR ( MIN ( 'Table'[period_date] ) )
            = YEAR ( MINX ( ALLSELECTED ( 'Table' ), 'Table'[period_date] ) ),
        BLANK (),
        ( MAX ( 'Table'[net_sales_retail] ) - LastYearSales ) / LastYearSales
    )

The result shows the increase/descrese compared with the last year same weekNo data:

56.PNG

 

Best Regards,

Giotto Zhi

STS_Joshua
Helper II
Helper II

Let me illustrate the problem in the report.

Please refer to the data table in my original post for the values needed.

I currently have it setup to show the correct amount in my previous year measure (highlighted):

REIReport1.JPG

When I select the last reported week on the upper right line graph it cross filters the measure to the wrong week (highlighted). The value should be the $537,963 from week 11 2019:

REIReport2.JPG

 

camargos88
Community Champion
Community Champion

Hi @STS_Joshua ,

 

Try this code:

 

SamePeriodoLastYear =
VAR _week = SELECTEDVALUE('Table (2)'[WeekNumber])
VAR _year = YEAR(SELECTEDVALUE('Table (2)'[period_date])) - 1
return CALCULATE(SUM('Table (2)'[net_sales_retail]); FILTER(ALL('Table (2)'); YEAR('Table (2)'[period_date]) = _year && 'Table (2)'[WeekNumber] = _week))
 
I hope it helps you,
 
Ricardo

 



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Thanks for the suggestion. What this formula does is returns the value for ONLY the same week of last year. I need something that will give me week 1 of 2019 when week 1 of 2020 is selected and so on. 

 

@STS_Joshua , 364 days behind measure should do, as long ad week is coming from date calendar

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
amitchandak
Super User
Super User

Week year is 364 days behind with date calendar you can use

Week Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-364,DAY))

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

That will not work because I want to compare the value I have for 3/14/2020 with the value I have for 3/16/2019.

 

@STS_Joshua,

 

If you need the see 1 year ahead, just change to:

SamePeriodoLastYear =
VAR _week = SELECTEDVALUE('Table (2)'[WeekNumber])
VAR _year = YEAR(SELECTEDVALUE('Table (2)'[period_date])) + 1
return CALCULATE(SUM('Table (2)'[net_sales_retail]); FILTER(ALL('Table (2)'); YEAR('Table (2)'[period_date]) = _year && 'Table (2)'[WeekNumber] = _week))
 
If not, keep the same code, it's gonna bring the value for last year and same week.
 
 

 



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.