Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

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

 

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

6 Replies

  • nandukrishnavs's avatar
    nandukrishnavs
    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's avatar
      Anonymous
      Not applicable

      This worked. Thank you so much! 

  • nandukrishnavs's avatar
    nandukrishnavs
    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
    🙂

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

    • parry2k's avatar
      parry2k
      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.

       

       

      • Anonymous's avatar
        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