cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

Difference between index columns

I am trying to find the difference in quanity by year (index columns), but my current measure is not calculating correctly. 

 

My table currently has columns of date as mm/dd/yyy, location, and quanity. I want to find the difference between each year, so my date column will be an index column, as it calculates total quanity by year. Please see the below attachments:

  • Date format in my data table
  • How my visual is displayed
  • Visual with the DAX for Difference measure 

 

The DAX for my difference measure is currently: 

 

Difference =
If (HASONEVALUE('Table 1'[QTR_END_DATE]),
BLANK(),
CALCULATE (
SUM ( 'Table 1'[QUANTITY] ),
FILTER ( 'Table 1', 'Table 1'[QTR_END_DATE]= MAX ( 'Table 1'[QTR_END_DATE]) )
)
- CALCULATE (
SUM ( 'Table 1'[QUANTITY] ),
FILTER ( 'Table 1', 'Table 1'[QTR_END_DATE] = MIN ( 'Table 1'[QTR_END_DATE] ) )
))

 

However, this is not calculating the difference correctly as you can see that the difference between 2003 Quanity and 2002 Quanity is displayed as 5,271 when the actual difference is 11,713 (214,915 - 203,202 = 11,713). Please avise. Thank you!!! 

 

table screenshot.jpgtable date.jpgDifference across year.jpg

1 ACCEPTED SOLUTION
nandukrishnavs
Community Champion
Community Champion

@mstuve

Try this

Difference = 
var _current= CALCULATE(SUM('Table'[Quantity]))
var _CY= YEAR(MAX('Table'[QTR_END_DATE]))
var _prior= CALCULATE(SUM('Table'[Quantity]),PREVIOUSYEAR('Table'[QTR_END_DATE].[ Date]))
var _diff= _current-_prior
return _diff

Capture.JPG



Did I answer your question? Mark my position as a solution!
Appreciate with a kudos
🙂

View solution in original post

6 REPLIES 6
nandukrishnavs
Community Champion
Community Champion

@mstuve

Try this

Difference = 
var _current= CALCULATE(SUM('Table'[Quantity]))
var _CY= YEAR(MAX('Table'[QTR_END_DATE]))
var _prior= CALCULATE(SUM('Table'[Quantity]),PREVIOUSYEAR('Table'[QTR_END_DATE].[ Date]))
var _diff= _current-_prior
return _diff

Capture.JPG



Did I answer your question? Mark my position as a solution!
Appreciate with a kudos
🙂

Anonymous
Not applicable

This worked. Thank you so much! 

amitchandak
Super User
Super User

@Anonymous , Not very clear, what you are trying to get using Min and Max Qtr end date?



Power BI Features || Datamarts: https://youtu.be/8tskWsJTEpg || Field Parameters : https://youtu.be/lqF3Wa1FllE?t=70
Time Intelligence Decoded : https://youtu.be/aU2aKbnHuWs&t=145s
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !! Proud to be a Super User! !!
Dashboard of My Blogs !! Connect on Linkedin !! Subscribe to my youtube Channel !!
Want To Learn Power BI | Beginners !! Advance Concepts !! Power BI For Tableau User !!

@Anonymous Regardless of what you are trying to achieve, underline improvement you need to your model is to add a date dimension. For any time intelligence calculation, it is a best practice to have a date dimension. There are many posts that talk about how you can add this dimension in your model, once it is done, I'm sure all the calculations will be super easy.

 

Let's try to follow the best practices rather than making some convoluted DAX expressions. My 2cent.

 

 






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

Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.





Anonymous
Not applicable

You are correct. I am very new to DAX, and I do need to learn best practices. I appreciate your help and advice! 

 

Marie

nandukrishnavs
Community Champion
Community Champion

@mstuve

Try this

Difference = 
var _current= CALCULATE(SUM('Table'[Quantity]))
var _prior= CALCULATE(SUM('Table'[Quantity]),PREVIOUSYEAR('Table'[QTR_END_DATE].[ Date]))
var _diff= _current-_prior
return _diff

Capture.JPG



Did I answer your question? Mark my position as a solution!
Appreciate with a kudos
🙂

Helpful resources

Announcements
August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power BI Dev Camp Session 24 without aka link and time 768x460.jpg

Ted's Dev Camp - July 28, 2022

Watch Session 24 of Ted's Dev Camp along with past sessions!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Solution Authors
Top Kudoed Authors