Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
StSupQ
Frequent Visitor

How to remove context from a temporary contextually-calculated table-variable ?

Hello all,

I am currently working on a moving average measure.
The underlying data i want to smooth that way is already a measure. I am already displaying it on a visual as a fonction of a [Time] column, and i want to display the smoothed curve on the same visual. My visual will be filtered on another [Perimeter] column, that is why the underlying data is already a measure.

I have created a temporary table with the ADDCOLUMNS function as follows: 

VAR tempTable=
ADDCOLUMNS(
    FILTER(
        'Table1',
        [Time]>=windowStart && [Time]<= windowEnd && [Perimeter]=perim
    ),
    "Measure Value",
    [Measure]
)


I want to return the average value of the column [Measure Value] from that table.
I can't just use AVERAGEX as it keeps the underlying filter context, and I can't use ALL, ALLSELECTED or REMOVEFILTERS as it will display as an error that i must reference a table and not use the expression of one. Since this temporary table is contextually created, I can't just use a fixed table which would be easy. I would love to just use AVERAGE(tempTable[MeasureValue]) but it seems like you can't reference columns from table variables that way.

Any help of any kind would be greatly appreciated.
Thank you, 
StSupQ

1 ACCEPTED SOLUTION

Within the definition of the column you're adding using ADDCOLUMNS you can manipulate the filter context in any way you want, or you can manipulate the filter context in a CALCULATETABLE as in the above example where the date is being manipulated.

View solution in original post

8 REPLIES 8
johnt75
Super User
Super User

You can use AVERAGEX over the temporary table, 

Avg of measure =
VAR tempTable =
    ADDCOLUMNS (
        FILTER (
            'Table1',
            [Time] >= windowStart
                && [Time] <= windowEnd
                && [Perimeter] = perim
        ),
        "Measure Value", [Measure]
    )
RETURN
    AVERAGEX ( tempTable, [Measure Value] )

Hi, 

Thank you for the proposition.
However, when I do so, it keeps the filter context of the base table, which filters this calculated table to only the current date. So it will do the average of only one value, which returns the same curve as the underlying measure. 
What I want to do is to remove the filter context on that calculated table (while keeping it only between the windowStart and windowEnd time variables) and only then calculate the average.

What are you trying to get the average over - over time, over products ?

My final result is the curve of the moving average over time. As to what that is, each data point of the moving average would be the average of the 3 preceiding data points, the current one and the next 3 from the underlying measure. (See this wikipedia article for further detail: https://en.wikipedia.org/wiki/Moving_average)

I have managed to create that moving average as a calculated column with another column as underlying. What I am trying to do here is have it as either a column or a measure based on an underlying that is a measure. I want that this way so that it can be dynamically filtered using an important range of possible filters.

The way that I wanted to process was by having my underlying data copied and filtered in a temporary table, to which I would add the in-context measure and calculate the average of the column of the whole table.

I would love it if you had any idea of a possible solution

The general method is to use ADDCOLUMNS .. SUMMARIZE to build the temporary table with an additional column containing the value you want to average, then use AVERAGEX over that table.

For example, if you wanted the average sales by brand over a given period you could use something like

Average by brand over time =
VAR MinDate =
    MIN ( 'Date'[Date] )
VAR MaxDate =
    MAX ( 'Date'[Date] )
VAR SummaryTable =
    CALCULATETABLE (
        ADDCOLUMNS (
            SUMMARIZE ( Sales, 'Date'[Year month], 'Product'[Brand] ),
            "@sales amount", CALCULATE ( SUM ( 'Sales'[Amount] ) )
        ),
        'Date'[Date] >= MinDate
            && 'Date'[Date] <= MaxDate
    )
VAR Result =
    AVERAGEX ( SummaryTable, [@sales amount] )
RETURN
    Result

Thank you for trying to help me.

I still get the same result as my underlying measure, as my context is still inherited from the underlying. Is there no way to extand the time context to the adjacent entries ?

Within the definition of the column you're adding using ADDCOLUMNS you can manipulate the filter context in any way you want, or you can manipulate the filter context in a CALCULATETABLE as in the above example where the date is being manipulated.

Thank you very much that was really helpfull. I didn't know you could redefine the context like that.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.