Reply
DebbieE
Community Champion
Community Champion
Partially syndicated - Outbound

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

Source Community: Power BI Spanish
Syndicated - Inbound

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

Syndicated - Outbound

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

Source Community: Power BI Spanish
Syndicated - Inbound

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

Source Community: Power BI Spanish
Syndicated - Inbound

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

Syndicated - Outbound

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

FullDateAlternateKey is a date, not the numeric key

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Greg_Deckler
Super User
Super User

Syndicated - Outbound

@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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...
avatar user

Helpful resources

Announcements
March PBI video - carousel

Power BI Monthly Update - March 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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

Top Solution Authors (Last Month)
Top Kudoed Authors (Last Month)