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
SuddenClarity
Helper I
Helper I

SAMEPERIODLASTYEAR doesn't work as a variable

I tried to write the following:

 

Sales YoY =
VAR CY = [Total Sales]
VAR PY = CALCULATE(CY, SAMEPERIODLASTYEAR('Calendar'[Date]))

RETURN
IF ( ISBLANK (PY),
BLANK(),
CY - PY  )
 
But it returns 0. I then put the 'PY' variance in the return statement and it returns the CY measure, hence why its calculating 0. But when I simply write the measure CALCULATE([Total Sales], SAMEPERIODLASTYEAR('Calendar'[Date])), it works.. 
 
Can you not use SAMEPERIODLASTYEAR in a variable?
1 ACCEPTED SOLUTION
FreemanZ
Super User
Super User

hi @SuddenClarity 

the issue is from the nature of variable in DAX. Once defined, a variable is stored as a constant. So whatever filter you add in expressions like

CALCULATE(CY, SAMEPERIODLASTYEAR('Calendar'[Date]))

it simply return CY, a constant defined before. 

So CALCULATE([Total Sales], SAMEPERIODLASTYEAR('Calendar'[Date])) is the right approach.

View solution in original post

2 REPLIES 2
Clinical_Epi
Advocate III
Advocate III

Dear Sudden Clarity,

 

You could use same period last year as a Variable - see my example where as Variable is using same periodlastyear

 

Test measure =
VAR TY = TOTALYTD(COUNT('IP SQL Data'[URN]),_Date[Date filter])
VAR LY = CALCULATE(COUNT('IP SQL Data'[URN]),SAMEPERIODLASTYEAR(_Date[Date filter]))
RETURN TY / LY
 
Hope this confirms with you it can be done 🙂
Untitled.png

and with date change,

 

Untitled_next.png

Kind regards,
Clin Epi
FreemanZ
Super User
Super User

hi @SuddenClarity 

the issue is from the nature of variable in DAX. Once defined, a variable is stored as a constant. So whatever filter you add in expressions like

CALCULATE(CY, SAMEPERIODLASTYEAR('Calendar'[Date]))

it simply return CY, a constant defined before. 

So CALCULATE([Total Sales], SAMEPERIODLASTYEAR('Calendar'[Date])) is the right approach.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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