Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago
Solved

Calculate average of values between two boundaries

I need to calculate the average of values in a column for those values that are >= a lower limit and <= an upper limit. These limits are defined as measures (lower and upper). I thought that was pre...
  • Zubair_Muhammad's avatar
    8 years ago

    Anonymous

     

    Give this a shot

     

    EHT =
    VAR myupper = [upper]
    VAR mylower = [lower]
    RETURN
        AVERAGEX (
            VALUES ( 'STA Core'[Total TouchTime for the record] ),
            CALCULATE (
                AVERAGE ( 'STA Core'[Total TouchTime for the record] ),
                FILTER (
                    'STA Core',
                    'STA Core'[Total TouchTime for the record] <= myupper
                        && 'STA Core'[Total TouchTime for the record] >= mylower
                )
            )
        )
  • OwenAuger's avatar
    OwenAuger
    8 years ago

    Anonymous

    The core problem which Zubair_Muhammad fixed in this example is that if you need to reference the lower/upper bounds within FILTER (or any other iterator) you must compute those bounds first and store them in variables, so that their values remain fixed.

     

    In your original formula, [lower] and [upper] measures were used within the row context created by FILTER, which meant they were computed in a filter context corresponding to each individual row of the table being iterated (due to context transition). Since it looks like [lower] and [upper] are defined using lower/upper quartiles, this would have meant that the value in every row ended up falling between [lower] and [upper] when computed in the context of that row, giving you an unfiltered result.