Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Create calculated row

Hi, I need to create a difference *row* in my report view (table).   The current situation is, I have data like this:   Month JAN FEB MAR Data1 100 058 160 Data2 112 069 129 ...
  • Cmcmahan's avatar
    Cmcmahan
    7 years ago

    Ah, I didn't realize Data1 and Data2 were string values in the same column.  I set up some sample data in this format:

     

    MonthAction
    MarData1
    MarData1
    MarData1
    MarData1
    MarData1
    MarData1
    FebData1
    FebData1
    FebData1
    FebData1
    FebData1
    FebData1
    MarData2
    MarData2
    MarData2
    MarData2
    MarData2
    MarData2
    MarData2
    FebData2
    FebData2
    FebData2
    FebData2
    FebData2

     

    So I'm making a few assumptions here, let me know if any are wrong, and I can give a a better-tailored answer.  I'm assuming you've put this data into a matrix, which is how you have a COUNT for each one, but are having difficulty getting the difference. Here's the DAX measure to get the difference: 

     

    Diff = COUNTAX(FILTER('Table','Table'[Action]="Data1"),[Action])-COUNTAX(FILTER('Table','Table'[Action]="Data2"),[Action])

     

     

    I used COUNTAX so that we can count rows while applying our own filters to 'Table'.   This only works if you're hardcoding the values for Data1 and Data2, but this measure will give the difference in the amount of Data1 and Data2 action on any active/selected rows. This lets us evaluate a difference whenever we want, but isn't instantly useful for your situation.

    In order to get a matrix to give different totals than a straight sum (which it seems is what you want), you have to get tricky with measures. So I set up a matrix with the Action as the row value, and the Month as the column value.  Then I set the values equal to this measure:

     

    MatrixValue = IF(ISFILTERED('Table'[Action]),COUNTROWS('Table'),[Diff])

     

    Now this may look overwhelming at first, because there is a LOT going on.   The first thing that's happening is DAX is checking if Action is filtered.  If it is filtered, that means we are calculating a value within the matrix, so I just have it count the rows.  If it is not filtered, that means we are calculating a subtotal, in this case we want a difference so we use the [Diff] measure.  

     

    And with that, we get this matrix:

     

    Hopefully that gives you the format you're looking for.  If you have any questions about what I just did, let me know.