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
_n_MarianLein
Helper II
Helper II

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

 

I now need to add a row below called "diff" to have this result:

Month   JAN   FEB   MAR
Data1   100    58   160
Data2   112    69   129
Diff     12    11   -31

Problem is, the counts in "Data1" and "Data2" are sums by month, and not directly taken as raw data, meaning in January I'd have 212 rows in the source table, in February 169, and so on...

How can I achieve this?

1 ACCEPTED SOLUTION

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:

snipo.PNG

 

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

View solution in original post

4 REPLIES 4
Cmcmahan
Resident Rockstar
Resident Rockstar

I'm assuming the calculation by month is a raw sum.  If you're doing weird math here, you'll have to adapt.

You can do this by creating a calulated column on your original data. Set it up like so: 

Diff = Table[Data1] - Table[Data2]

And then just sum the Diff row like you do the other rows. Here's an example set to show the math works out:

 Data1Data2Diff
1-Jan321
2-Jan220
3-Jan12-1
SUM660

 

From here you can see that it doesn't matter how many rows each month has, PBI will sum them just the same as your original data!

Hi,

 

Sorry, but I can't figure it out.

 

My source data is one table that's got 100 lines with ("month" = JAN) and ("Action" = Data1), 112 lines with ("month" = Jan) and ("Action" = Data2), and so on. These are not two separate tables

 

Can you post a step-by-step instruction how to get the difference between the two rows to work?

Thanks...

Hi @_n_MarianLein ,

 

Kindly share your sample data to me if you don't have any Confidential Information. Please upload your files to One Drive and share the link here.

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

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:

snipo.PNG

 

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

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.