Forum Discussion

s--turn's avatar
s--turn
Icon for Helper II rankHelper II
1 year ago
Solved

Getting totals to add up

Hello,   Due to an unavoidably complex data structure, I'm struggling to work out how to calculate totals and subtotals here.   The table aims to show, for each country (e.g. Uganda) and for each...
  • DataNinja777's avatar
    DataNinja777
    1 year ago

    Hi s--turn 

     

    You're very close to the right solution — the main issue is that DAX totals behave differently than expected because they evaluate across a broader context than the row level. For your Amount Given Away measure, you're seeing the wrong total (£614.20) because it's just summing the per-member discounts without considering whether it's actually at the row level. To fix this, you can use the ISINSCOPE function to make sure the measure only shows values when a specific pricelevel[Name] is in scope. When it's not (i.e., at subtotal or total levels), we return blank. Here's the revised version of that measure:

    Amount Given Away = 
    IF(
        ISINSCOPE(pricelevel[Name]),
        SUM(productpricelevel[Discount on Price]),
        BLANK()
    )
    

    Now for the Total Amount Given Away measure — this one should calculate the number of members for each price level and multiply that by the amount given away per member. To get that, use a SUMX over the list of price levels in the current context. That way, it respects the grouping (e.g., per grade or per country) and correctly aggregates only where it makes sense. Here's the DAX formula:

    Total Amount Given Away = 
    SUMX(
        VALUES(pricelevel[Name]),
        [# Members] * [Amount Given Away]
    )
    

    This gives you the right total for each grade and country: it multiplies how many members are in each pricing tier by their individual discount, and adds that up. If you want to suppress totals at the country level (like Uganda) or higher rollups, you could optionally wrap the whole measure in another IF using ISINSCOPE to only return values when you're in a grade or price level context. But for most cases, the above setup will do exactly what you're aiming for — blank "Amount Given Away" at higher levels and a properly calculated "Total Amount Given Away" that adds up per-member discount values.

     

    Best regards,

     

  • s--turn's avatar
    s--turn
    1 year ago

    Aha... I may have solved it... see below: