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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Super User
Super User

@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
🙂


Regards,
Nandu Krishna

View solution in original post

6 REPLIES 6
nandukrishnavs
Super User
Super User

@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
🙂


Regards,
Nandu Krishna

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?

@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.

 

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


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
Super User
Super User

@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
🙂


Regards,
Nandu Krishna

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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