cancel
Showing results for
Search instead for
Did you mean:
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!!!

1 ACCEPTED SOLUTION
Community Champion

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``````

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

6 REPLIES 6
Community Champion

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``````

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

Anonymous
Not applicable

This worked. Thank you so much!

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

Community Champion

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``````

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

## Helpful resources

Announcements

#### The Power BI Community Show

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

#### Ted's Dev Camp - July 28, 2022

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

#### 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