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
Mainer04401
Helper III
Helper III

Same Period Last Year when Sales Data is by Week Ending Date

I'm struggling with how to calculate sales in the same period year ago because my data has sales by week ending date, not by day. 

 

For example, I want to calculate sales in the same period year ago of CYTD through 7/18/2020.  Week ending 7/18/2020 is the 29th week of 2020.  The 29th week of 2019 ends 7/20/2019, but SamePeriodLastYear will try to find data through 7/18/2019.  

 

I made a sample report but I don't know how to attach it.  Below is what my date-table looks like.  I can modify it however needed to make this work.  

 

Week EndingWeek NumberCalendar YearMonthQuarterMonth / YearQuarter / Year2020 Calendar Week Ending
1/5/201912019JanuaryQ1 2019 January2019 Q1 1/4/2020
1/12/201922019JanuaryQ1 2019 January2019 Q1 1/11/2020
1/19/201932019JanuaryQ12019 January2019 Q11/18/2020
1/26/201942019JanuaryQ12019 January2019 Q11/25/2020
2/2/201952019FebruaryQ12019 February2019 Q12/1/2020
2/9/201962019FebruaryQ12019 February2019 Q12/8/2020
2/16/201972019FebruaryQ12019 February2019 Q12/15/2020
2/23/201982019FebruaryQ12019 February2019 Q12/22/2020
3/2/201992019MarchQ12019 March2019 Q12/29/2020
5 REPLIES 5
amitchandak
Super User
Super User

I'm getting close ...  I used the formula below to get same week year ago, however I need same period year ago when multiple weeks are selected.  So, for example, if CYTD is week numbers 1 through 29, I need the sum of sales in weeks 1 through 29 last year.

 

LYWTD Year = CALCULATE([Sales], FILTER(ALL(‘Date’),’Date’[Year]=(max(‘Date’[Year]) -1) && ‘Date’[Week Number]=(max(‘Date’[Week Number])) && ‘Date’[Weekday] <=max(‘Date’[Weekday])))

Yessss, I finally got it!  For anyone who finds this from search, here is the formula to find last year's sales during the same WEEK NUMBERS as this year.  This works for individual weeks as well as an aggregate of several weeks.

 

Prev Year Sales =
CALCULATE(
    [Sum of Sales],
    FILTER(
        ALL('Date Table'),
        'Date Table'[Calendar Year] = MAX('Date Table'[Calendar Year]) -1
        && 'Date Table'[Week Number] >= min('Date Table'[Week Number])
        && 'Date Table'[Week Number] <= max('Date Table'[Week Number])
    )
)


EDIT: Nope, I got too excited...  This formula works unless the weeks selected span multiple years.  For example, last 13 weeks as of the end of January 2020.  It determines the minimum week number is 1, (first week of January 2020) and the maximum week number is 52 (last week of Dec 2019), so it returns total sales in 2019.  I'm thinking maybe I add a cumulative week # column in my dates table?  Then instead of filtering by year, I filter by -52 of the min & max cumulative week number?  Any other ideas, please let me know.  I feel like I'm now over-complicating it.  
Greg_Deckler
Community Champion
Community Champion

See if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Thanks but unfortunately I couldn't get that to work.  It worked without any weeks in the context or without any filters, but when I start filtering the weeks, the output went black.

 

Here's the DAX I used:

Yago Sales = VAR __MaxYear = MAX('Date Table'[Calendar Year])
VAR __MaxWeek = MAX('Date Table'[Week Number])
VAR __TmpTable = CALCULATETABLE('Date Table',ALL('Date Table'[Calendar Year],'Date Table'[Week Number]))
RETURN SUMX(FILTER(__TmpTable,[Calendar Year]=__MaxYear-1 && [Week Number] <= __MaxWeek),[Sum of Sales])

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!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.