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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Anonymous
Not applicable

DAX Issue.

Hi  Experts,

 

When I'm trying to create a measure I stuck up with below issue.

Basicaly here I'm trying to display previous year values. I can use datediff but my column RPT Year is in Whole number Format

 

Test = CALCULATE(SUM(Query[Jan]),Query2[RPTyear]=2019) ----It works
But whenb I'm trying to do like below, I'm getting error(basically dynamic)
Test = CALCULATE(SUM(Query[Jan]),Query2[RPTyear]=(Query2[RPTyear]-1))
 
If we select 2020, i need to pass 2019. RPTYear is showing as slicer
 
Thanks,
Anand
8 REPLIES 8
Anonymous
Not applicable

Hi  @Anonymous ,

According to your description, I created the data:

v-yangliu-msft_0-1608279255001.png

Here are the steps you can follow:

1. Use the calculated table to create a calendar table named query2, and create a calculated column to display the yearcalculated table:

Quert2 = CALENDARAUTO()

calculated column:

PRTyear = YEAR('Quert2'[Date])

2. Create measure.

Test =
var _select =SELECTEDVALUE('Quert2'[PRTyear])
return CALCULATE(SUM('Query'[Jan]),FILTER('Query',YEAR('Query'[Date])=_select-1))

3. Result

v-yangliu-msft_1-1608279255005.png

You can downloaded PBIX file from here.

 

Best Regards,

Liu Yang

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

AllisonKennedy
Super User
Super User

@Anonymous  do you have a DimDate table? Is that what Query2 is? 

 

You can do this more easily if you have a DimDate table using built in time intelligence, otherwise you need to calculate the max selected year in the context of the entire report, you can do this using a variable, but does it need to work for multiple year selections? If so, DimDate table is much easier. https://excelwithallison.blogspot.com/2020/04/dimdate-what-why-and-how.html

 

Otherwise, using a variable would look something like (I have tried to account for multiple year selections, and imitated behaviour of the DATEADD function using time intelligence and date table: 

 

Test =

VAR maxYear = MAX(Query2[RPTyear])

VAR minYear = MIN(Query2[RPTyear])

RETURN

CALCULATE(SUM(Query[Jan]),FILTER(ALL(Query2[RPTyear]), Query2[RPTyear]<= maxYear-1 && Query2[RPTyear]>= minYear-1 ))


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

amitchandak
Super User
Super User

@Anonymous , In case you need this year vs last year. If you have date then use time intelligence. else create a year table and use the formula using only year

 

YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD('Date'[Date],"12/31"))
Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))
This year Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR('Date'[Date]),"12/31"))
Last year Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR(dateadd('Date'[Date],-1,Year)),"12/31"))
Last to last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-2,Year),"12/31"))
Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))


//Only year vs Year, not a level below. This can work without the date table but need a year table

 

This Year = CALCULATE(sum('order'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])))
Last Year = CALCULATE(sum('order'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])-1))
rolling = CALCULATE(sum('order'[Qty]),filter(ALL('Date'),'Date'[Year]>=max('Date'[Year])-2 && 'Date'[Year]<=max('Date'[Year])) )

diff = [This Year]-[Last Year ]
diff % = divide([This Year]-[Last Year ],[Last Year ])

 

 

Power BI — Year on Year with or Without Time Intelligence
https://medium.com/@amitchandak.1978/power-bi-ytd-questions-time-intelligence-1-5-e3174b39f38a

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

@Anonymous 

maybe you can try this

Test = CALCULATE(SUM(Query[Jan]),Query2[RPTyear]=SLECTEDVALUE(Query2[RPTyear])-1))




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

When I'm using selectedvalue function getting below error

Error:

A function 'SELECTEDVALUE' has been used in a True/False expression that is used as a table filter expression. This is not allowed.

@Anonymous 

you need to create a calendar table and use dateadd

https://docs.microsoft.com/en-us/dax/dateadd-function-dax

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Pragati11
Super User
Super User

Hi @Anonymous ,

 

First thing - You haven't mentioned what error you are getting using this measure. Share it to describe what the issue is?

 

Thanks,

Pragati

Best Regards,

Pragati Jain


MVP logo


LinkedIn | Twitter | Blog YouTube 

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Appreciate your Kudos!!

Proud to be a Super User!!

Anonymous
Not applicable

It is displaying error as Can not find RPT year

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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