Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Help with basic DAX Code

Hi to all. Maybe this is really basic but i've been staring at the code and can't get why this two measures give different results. I'm using Contoso database to practice:   Measure 1 = SUMX ( ...
  • AlexisOlson's avatar
    AlexisOlson
    4 years ago

    [Total Sales] has to be a measure for it to work inside CALCULATE like this. How is it defined?

     

    If it's defined as SUM ( Sales[Quantity] ) * SUM ( Sales[Net Price] ), then it's nonsensical. If it's defined as SUMX ( Sales, Sales[Quantity] * Sales[Net Price] ), then the explanation has to do with granularity.

     

    The FILTER removes the rows with the product under 1000, but Sales and SUMMARIZE ( Sales, Sales[Quantity], Sales[Net Price] ) have different numbers of rows. In Measure 1, you add up the product for each row in the summary table but in Measure 2, you add up each row in Sales, which contains multiple rows with the same Quantity and Net Price instead of just a single row for each distinct combination.

     

    Measure 1 is grouping before summing while Measure 2 only has grouping inside the filter; the actual sum has no grouping by Quantity and Net Price.

     

    Your Measure 2 give the same result as

    SUMX (
        FILTER (
            Sales,
            Sales[Quantity] * Sales[Net Price] >= 1000
        ),
        Sales[Quantity] * Sales[Net Price]
    )