Monday, 12 September 2016

Concept of Row Context and Filter Context in Power BI


Row Context:

DAX expression normally operates on columns like:

GrossProfit:=SUMX(Sales, Sales[Amount]-Sales[TotalCost])

 

*All functions that end in X are iterators.

In above example, you get the value of  Sales[Amount] for currently  iterated row by SUMX()

 

NumofAllProducts := COUNTROWS(Values(Product[ProductName]))

In this example, Product[ProductName] does not retrieve value of ProductName for a specific row; instead column reference is used to get values.

 

The concept of current row in first example of DAX Expression is known as Row Context. You have a  row context whenever you iterate a table. Every iterator introduces a row context and iterators can be nested. For example:

AverageDiscountedSalesPerCustomer :=

AVERAGEX (

    Customer,

    SUMX (

        RELATEDTABLE ( Sales ),

        Sales[SalesAmount] * Customer[DiscountPct]

    )

)

Filter Context:

The filter context is the set of filters applied to the data model before the evaluation of a DAX expression starts. When you use a measure in a pivot table, for example, it produces different results for each cell because the same expression is evaluated over a different subset of the data. The Microsoft documentation describes as query context those filters applied by the user interface of a pivot table and as filter context, those other filters applied by DAX expressions that you can write in a measure. In reality, these filters are almost identical in their effects (the real differences are not important for this introductive article), so we simply defines as filter context the set of filters applied to the evaluation of a DAX expression, usually a measure, regardless of how they have been generated.

                A filter context automatically propagates through relationships, according to the cross filter direction of the relationship. You can control the filter context and its propagation using DAX functions such as CALCULATE, CALCULATETABLE, ALL, VALUES, FILTER, USERELATIONSHIP, and CROSSFILTER.