Forum Discussion
Subtract values from different rows
- 10 years ago
Hi gdssiqueira
1. Create a new table summarized:
TableWorkSumm =
SUMMARIZE (
TableWork;
TableWork[Date ];
TableWork[Category ];
"Result"; IF (
CALCULATE ( SUM ( TableWork[Value] ); TableWork[Type] = "Purchase" )
- CALCULATE ( SUM ( TableWork[Value] ); TableWork[Type] = "Refund" )
= CALCULATE ( SUM ( TableWork[Value] ); TableWork[Type] = "Purchase" );
BLANK ();
CALCULATE ( SUM ( TableWork[Value] ); TableWork[Type] = "Purchase" )
- CALCULATE ( SUM ( TableWork[Value] ); TableWork[Type] = "Refund" )
)
)2. When you use the Column Result; Select Visual Filter to Result to Is not Blank and Applied
Relying heavily on-
"(there's never more than one row with purchase and one row with refund, for each category, for each day, but there might be less than one like Fruit in 1/1, which has no refunds or purchases, or Fruit in 2/1 which has a purchase but no refunds and doesn't interest me)",
Adding a calculated column with the following DAX formula should do the trick:
difference =
var refund_val = LOOKUPVALUE(table1[value], table1[type], "refund", table1[date], [date], table1[category], [category])
return IF(AND([type] = "purchase", ISNUMBER(refund_val)), [value] - refund_val, BLANK())Which basically says "For each row, if 'type' is 'purchase', look up for 'value' where 'type' is 'refund' and 'date' is the current row's date and category is the current row's category. If such a value was found(I.e. a number was returned), return its subtruction from the current row's value. otherwise, return a blank value".
Again, this formula relies heavily on the fact that there is no more than one "refund" counterpart for each "purchase" type, and would fail otherwise.
Also, there are other ways to do this, but this is the most straightforward one I can think of.
That's actually why I added these remarks - it might simplify things a lot and, in my case, is always true.
I will experiment with your solution and come back with feedback. Thanks a lot !