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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
DebbieE
Community Champion
Community Champion

Trying to create TYD Variance measure with variables and not getting a result

YTD Sales Var = VAR CY = [Total Sales]
VAR TYTD = TOTALYTD([Total Sales],DimDate[FullDateAlternateKey])
VAR LYTD = CALCULATE(TYTD,SAMEPERIODLASTYEAR(DimDate[FullDateAlternateKey]))
RETURN TYTD-LYTD
 
this is coming up as nothing
YOY.JPG
 
When I test I look just at TYTD
YTD Sales Var = VAR CY = [Total Sales]
VAR TYTD = TOTALYTD([Total Sales],DimDate[FullDateAlternateKey])
VAR LYTD = CALCULATE(TYTD,SAMEPERIODLASTYEAR(DimDate[FullDateAlternateKey]))
RETURN TYTD
 
And this brings up the year to date values no problem
And the next test I replace the last part with RETURN LYTD
And again, you get the Last year to date totals showm so both  TYTD and LYTD return the correct values
 
So its just this RETURN TYTD-LYTD
 
this returns 0 and I cant figure out why at the moment. Im using Adventureworks data warehouse data to test on
 
Any help would be appreciated
1 ACCEPTED SOLUTION
DebbieE
Community Champion
Community Champion

Ive got it

YTD Sales Var = VAR CY = [Total Sales]
VAR LYTD = CALCULATE(TOTALYTD([Total Sales],DimDate[FullDateAlternateKey]),SAMEPERIODLASTYEAR(DimDate[FullDateAlternateKey]))
RETURN TYTD-LYTD
I have to add the TYTD into LYTD to make that work because I cant use a variable within another variable like you said

View solution in original post

5 REPLIES 5
v-xicai
Community Support
Community Support

Hi @DebbieE ,

 

In DAX, variables are calculated within the scope in which they are written, and then the result of them is stored and used in the rest of the expression. See more: Caution When Using Variables in DAX and Power BI.

 

You may change the formula like DAX below.

 

YTD Sales Var =

VAR TYTD = TOTALYTD(SUM(Table1[Sales]), DimDate[FullDateAlternateKey])

VAR LYTD = CALCULATE(SUM(Table1[Sales]) ,SAMEPERIODLASTYEAR(DimDate[FullDateAlternateKey]))

RETURN TYTD-LYTD

 

For reference:

 

https://community.powerbi.com/t5/Desktop/var-different-result/td-p/283143

 

https://community.powerbi.com/t5/Desktop/VAR-different-result-than-without-it/td-p/518723

 

Best Regards,

Amy 

 

Community Support Team _ Amy

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

DebbieE
Community Champion
Community Champion

VAR LYTD = CALCULATE (SUM (Table1 [Sales]), SAMEPERIODLASTYEAR (DimDate [FullDateAlternateKey]))

But this isnt calculating the Year to date measure that Im basing last year on so this wouldnt work?

As in my TYTD variable

TOTALYTD (SELECTEDMEASURE (), DimDate [FullDateAlternateKey])

I should say, Im trying to do some work on calulated groups with Tabular Editor and Im trying to find ways of recreating my measures so they could be used for calculated groups and its proving really difficult

DebbieE
Community Champion
Community Champion

Ive got it

YTD Sales Var = VAR CY = [Total Sales]
VAR LYTD = CALCULATE(TOTALYTD([Total Sales],DimDate[FullDateAlternateKey]),SAMEPERIODLASTYEAR(DimDate[FullDateAlternateKey]))
RETURN TYTD-LYTD
I have to add the TYTD into LYTD to make that work because I cant use a variable within another variable like you said
amitchandak
Super User
Super User

@DebbieE , this should work. DimDate is marked as date table (Option on right click)

FullDateAlternateKey is a date, not the numeric key

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
Greg_Deckler
Community Champion
Community Champion

@DebbieE - You may find this helpful - https://community.powerbi.com/t5/Community-Blog/To-bleep-With-Time-Intelligence/ba-p/1260000

Also, 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...

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.

Top Solution Authors