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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
atult
Advocate I
Advocate I

Last Year Same Period Sales

Hi Experts,
I need to calculate the last year same period sales in a calculated measure.

Here, the 1 period = 4 weeks and hence there are 13 periods in an year.

 

Here is the table where Period format and Sales are given. Column 3(Required Measure) needs to be calculate:

 

PeriodSalesRequired Measure
2021P01100 
2021P02200 
2021P03300 
2021P04400 
2021P05500 
2022P01600100
2022P02700200
2022P03800300
2022P04900400
2022P051000500

 

Thanks in advance !

1 ACCEPTED SOLUTION

Hi,

1- create Dim Date with using the below script :

Date =
//************** Script developed by RADACAD - edition: July 2021
//************** set the variables below for your custom date table setting
var _fromYear=2021 // set the start year of the date dimension. dates start from 1st of January of this year
var _toYear=2022   // set the end year of the date dimension. dates end at 31st of December of this year
var _startOfFiscalYear=7 // set the month number that is start of the financial year. example; if fiscal year start is July, value is 7
//**************
var _today=TODAY()
return
ADDCOLUMNS(
    CALENDAR(
                DATE(_fromYear,1,1),
                DATE(_toYear,12,31)
),
"Year",YEAR([Date]),
"Month",MONTH([Date]),
"Day",DAY([Date]),
"Day of Year",DATEDIFF(DATE( YEAR([Date]), 1, 1),[Date],DAY)+1,
"Day Period Num", format( trunc( DIVIDE((DATEDIFF(DATE( YEAR([Date]), 1, 1),[Date],DAY)+1),28,0)), YEAR([Date])&"P0#")
)
* you could copy the complete Date Dim from the below site :
2- Then Create relationship between your table (it is named Sheet32 in my script) and Dim date :
MahyarTF_0-1658540233302.png

3- In the main table, create Measure for calculated the last year sales amount :

LastYearSales = CALCULATE( sum(Sheet32[Sales]), SAMEPERIODLASTYEAR('Date'[Date]) )
4- Now you could use the particular measure in your visual :
MahyarTF_1-1658540401374.png

 

Mahyartf

View solution in original post

6 REPLIES 6
MahyarTF
Memorable Member
Memorable Member

Hi,

In your Date dim, add the below column :

"Day Period Num", trunc( DIVIDE((DATEDIFF(DATE( YEAR([Date]), 1, 1),[Date],DAY)+1),28,0))
Then Manage Relationship between your table and Date table and then use SAMEPERIODLASTYEAR ( <Dates> ) Dax code in your measure
Mahyartf

Hi @MahyarTF ,
Created calculated as you suggested and mapped to the main table but didn't work. The values were only returned for the last period and that too were quite weird values.

Hi,

1- create Dim Date with using the below script :

Date =
//************** Script developed by RADACAD - edition: July 2021
//************** set the variables below for your custom date table setting
var _fromYear=2021 // set the start year of the date dimension. dates start from 1st of January of this year
var _toYear=2022   // set the end year of the date dimension. dates end at 31st of December of this year
var _startOfFiscalYear=7 // set the month number that is start of the financial year. example; if fiscal year start is July, value is 7
//**************
var _today=TODAY()
return
ADDCOLUMNS(
    CALENDAR(
                DATE(_fromYear,1,1),
                DATE(_toYear,12,31)
),
"Year",YEAR([Date]),
"Month",MONTH([Date]),
"Day",DAY([Date]),
"Day of Year",DATEDIFF(DATE( YEAR([Date]), 1, 1),[Date],DAY)+1,
"Day Period Num", format( trunc( DIVIDE((DATEDIFF(DATE( YEAR([Date]), 1, 1),[Date],DAY)+1),28,0)), YEAR([Date])&"P0#")
)
* you could copy the complete Date Dim from the below site :
2- Then Create relationship between your table (it is named Sheet32 in my script) and Dim date :
MahyarTF_0-1658540233302.png

3- In the main table, create Measure for calculated the last year sales amount :

LastYearSales = CALCULATE( sum(Sheet32[Sales]), SAMEPERIODLASTYEAR('Date'[Date]) )
4- Now you could use the particular measure in your visual :
MahyarTF_1-1658540401374.png

 

Mahyartf
Anonymous
Not applicable

@atult if you can have year column seperately,

 

then you can use this dax to get py sales.

 

Mohan1029_0-1658478484918.png

 

Accept the solution if it works for you.

Hi @Anonymous ,
It didn't work for me. Are you creating a calculated column or measure for this?

Anonymous
Not applicable

@atult it is a measure.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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