Forum Discussion
Relationship on field that has aggregation value
- 4 years ago
This solution uses a calculated column in the Target table which summarizes the appropriate amounts in the Sales table. I attempted to solve this via data modeling with a Channel mapping table and an intermediary DistinctChannel table for relationships, but the result wasn't correct for product_id 4 (ON TRADE and OFF TRADE from Sales weren't grouped into the ANY row).
Calculated column in Target table:
actual_sales = VAR vYear = YEAR ( Target[month] ) VAR vMonth = MONTH ( Target[month] ) VAR vProduct = Target[product_id] VAR vChannel = Target[channel] VAR vTable = FILTER ( Sales, YEAR ( Sales[date] ) = vYear && MONTH ( Sales[date] ) = vMonth && Sales[product_id] = vProduct && Sales[channel] = vChannel ) VAR vTableAny = FILTER ( Sales, YEAR ( Sales[date] ) = vYear && MONTH ( Sales[date] ) = vMonth && Sales[product_id] = vProduct ) VAR vResult = SWITCH ( vChannel, "ANY", SUMX ( vTableAny, Sales[value] ), SUMX ( vTable, Sales[value] ) ) RETURN vResultMeasures:
Target Sales = SUM ( Target[target_sales] )Actual Sales = SUM ( Target[actual_sales] )Achieved = DIVIDE ( [Actual Sales], [Target Sales] )In the visual, use Product[product_id] and Target[channel], along with the measures above:
This solution uses a calculated column in the Target table which summarizes the appropriate amounts in the Sales table. I attempted to solve this via data modeling with a Channel mapping table and an intermediary DistinctChannel table for relationships, but the result wasn't correct for product_id 4 (ON TRADE and OFF TRADE from Sales weren't grouped into the ANY row).
Calculated column in Target table:
actual_sales =
VAR vYear = YEAR ( Target[month] )
VAR vMonth = MONTH ( Target[month] )
VAR vProduct = Target[product_id]
VAR vChannel = Target[channel]
VAR vTable =
FILTER (
Sales,
YEAR ( Sales[date] ) = vYear
&& MONTH ( Sales[date] ) = vMonth
&& Sales[product_id] = vProduct
&& Sales[channel] = vChannel
)
VAR vTableAny =
FILTER (
Sales,
YEAR ( Sales[date] ) = vYear
&& MONTH ( Sales[date] ) = vMonth
&& Sales[product_id] = vProduct
)
VAR vResult =
SWITCH (
vChannel,
"ANY", SUMX ( vTableAny, Sales[value] ),
SUMX ( vTable, Sales[value] )
)
RETURN
vResult
Measures:
Target Sales = SUM ( Target[target_sales] )Actual Sales = SUM ( Target[actual_sales] )Achieved = DIVIDE ( [Actual Sales], [Target Sales] )
In the visual, use Product[product_id] and Target[channel], along with the measures above:
DataInsights that's it! Thank you for the help and clear solution!