Reputation: 1604
Situation:
I have a column (refund) that takes the MAX value for each day so that I don't duplicate the refunds.
refund =
var calcul =
CALCULATE(
SUM(Query1[amount]),
USERELATIONSHIP(Query1[created_at], DateTable[Date]),
Query1[kind] = "refund",
Query1[status] = "success"
)
This works for each day but the total is the max of that column
Objective:
I need this measure to calculate the net sales. I have the gross sales measure already but my refund formula doesn't work when aggregated.
What i tried (thanks to Alexis Olson):
refund =
var calcul =
CALCULATE(
SUM(Query1[amount]),
USERELATIONSHIP(Query1[created_at], DateTable[Date]),
Query1[kind] = "refund",
Query1[status] = "success"
)
return
SUMX(DISTINCT(Query1[orderId]), calcul)
However the output is unexpected. It basically takes each value and multiplies it by the distinct count of order ids on that day (refund or not).
So i tried dividing it by the distinct count of order id but the same problem with the total row taking the max value occurs.
Here's the output i get using the provided solution below:
Relationships:
Query1[created_at] DateTable[Date] (inactive)
Query1[orderDate] DateTable[Date] (active)
My returns measure:
Returns =
CALCULATE(
MAX(Query1[amount]),
USERELATIONSHIP(Query1[created_at], DateTable[Date]),
Query1[kind] = "refund",
Query1[status] = "success"
)
Upvotes: 0
Views: 310
Reputation: 1604
One way of fixing my issue was to use CALCULATETABLE
before the summarize and then
use the SUMX
as suggested above by Alexis.
Returns =
VAR sumary =
CALCULATETABLE(
SUMMARIZE(
Query1,
Query1[orderId],
"maxValue",CALCULATE(
MAX(Query1[amount]),
Query1[kind]= "refund",
Query1[status] = "success"
)
),USERELATIONSHIP(Query1[trx_date],DateTable[Date]))
RETURN
SUMX(sumary,[maxValue])
Upvotes: 0
Reputation: 40214
You're pretty close to something that works. Try changing calcul
to a summary table instead of a single value scalar like this:
refund =
VAR Summary =
SUMMARIZE (
Query1,
Query1[orderId],
"MaxValue", CALCULATE (
MAX ( Query1[amount] ),
USERELATIONSHIP ( Query1[created_at], DateTable[Date] ),
Query1[kind] = "refund",
Query1[status] = "success"
)
)
RETURN
SUMX ( Summary, [MaxValue] )
Upvotes: 0