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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Pearl456
Regular Visitor

Comparison of data between two years by week number

Hello, 

Attempting to create a matrix to compare this year's sales to 2020 sales by week number. 

Current code for last year is;

Sales SPLY = CALCULATE(sum('Sales Detail'[ABC Revenue]),SAMEPERIODLASTYEAR('Date'[Date]))

 

2021 sales data is reflecting correctly by week number, but 2020 is not pulling data from 2020 week number. 
Example: 2020 data is pulling dates 1/1/20 - 1/2/20, rather than 1/1/20 - 1/4/20. 

Week #2021 Sales2020 Sales
12,5202,556
27,7089,764
37,6977,802

 

Any guidance how to achieve last years data by the correct week number would be very appreciated. 

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

Hi @Pearl456 ,

 

I have built a data sample :

Week Table = 
DISTINCT (
    SELECTCOLUMNS (
        CALENDAR ( DATE ( 2020, 1, 1 ), DATE ( 2021, 12, 31 ) ),
        "Week", WEEKNUM ( [Date], 2 )
    )
)

Eyelyn9_0-1631585749537.png

You could create measures to calculate the sum of each week:

#2020 Sales =
CALCULATE (
    SUM ( 'Sales Detail'[ABC Revenue] ),
    FILTER (
        'Sales Detail',
        YEAR ( 'Sales Detail'[Date] ) = 2020
            && WEEKNUM ( 'Sales Detail'[Date], 2 ) = MAX ( 'Week Table'[Week] )
    )
)
#2021 Sales =
CALCULATE (
    SUM ( 'Sales Detail'[ABC Revenue] ),
    FILTER (
        'Sales Detail',
        YEAR ( 'Sales Detail'[Date] ) = 2021
            && WEEKNUM ( 'Sales Detail'[Date], 2 ) = MAX ( 'Week Table'[Week] )
    )
)

Here is the final output:

Eyelyn9_1-1631586180538.png

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-eqin-msft
Community Support
Community Support

Hi @Pearl456 ,

 

I have built a data sample :

Week Table = 
DISTINCT (
    SELECTCOLUMNS (
        CALENDAR ( DATE ( 2020, 1, 1 ), DATE ( 2021, 12, 31 ) ),
        "Week", WEEKNUM ( [Date], 2 )
    )
)

Eyelyn9_0-1631585749537.png

You could create measures to calculate the sum of each week:

#2020 Sales =
CALCULATE (
    SUM ( 'Sales Detail'[ABC Revenue] ),
    FILTER (
        'Sales Detail',
        YEAR ( 'Sales Detail'[Date] ) = 2020
            && WEEKNUM ( 'Sales Detail'[Date], 2 ) = MAX ( 'Week Table'[Week] )
    )
)
#2021 Sales =
CALCULATE (
    SUM ( 'Sales Detail'[ABC Revenue] ),
    FILTER (
        'Sales Detail',
        YEAR ( 'Sales Detail'[Date] ) = 2021
            && WEEKNUM ( 'Sales Detail'[Date], 2 ) = MAX ( 'Week Table'[Week] )
    )
)

Here is the final output:

Eyelyn9_1-1631586180538.png

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

TomMartens
Super User
Super User

Hey @Pearl456 ,

 

can you please provide a screenshot that shows all the days that are in the first week of 2021 and another screenshot that are in the first week of 2020.

This article explains how the function SAMEPERIODLASTYEAR works internally: https://dax.guide/sameperiodlastyear/

 

Hopefully, this provides some ideas on how to tackle your challenge.

 

Regards,

Tom

 

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Hello @TomMartens ,

 

I don't have an additional screen shot to share. The sales difference was discovered when validating the BI data to our accounting system by journal dates. BI 2020 sales amount was only from 1/1/2020 through 1/2/2020, rather than the actual first week of 2020 which are dates 1/1/2020 through 1/4/2020. Our third party company has restricted access to the data so I am unable to add a column within the data for only 2020 sales. Is there a DAX function to hard code 2020 sales data so I can get the goal of weekly comparisons between 2020 & 2021? 

Thank you. 

Hey @Pearl456 ,

 

without further information about your data model, I'm not able to provide further guidance.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.