Forum Discussion

victor_erathos's avatar
victor_erathos
Frequent Visitor
4 years ago
Solved

Relationship on field that has aggregation value

I need to relate a dataset of target sales and one of executed sales, with the important detail that the sales can be done on two different channels (ON TRADE and OFF TRADE). I've reduced the data to...
  • DataInsights's avatar
    4 years ago

    victor_erathos,

     

    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: