Forum Discussion

Cymbolz's avatar
Cymbolz
Helper III
8 years ago
Solved

Contexts and Expanded Tables

I feel I have a good understanding of explaned tables now, but this bit I cannot work out...simple example I've created:   Two Tables:   Fact id amount Dim id name category   Relati...
  • OwenAuger's avatar
    8 years ago

    I won't pretend this is a complete explanation...

     

    First off, this is probably a good reference article I recall reading:

    https://www.sqlbi.com/articles/context-transition-and-filters-in-calculate/

    (see the section Understanding the Evaluation Order)

     

    As I understand it, when CALCULATE() is called within the row context of a physical table (such as in a calculated column), the physical table (corresponding to the current row) is added to the filter context. Whenever a physical table is added to the filter context via CALCULATE, it is interpreted as the expanded verison of the table.

     

    From the article above, this can be interpreted as the expanded table being added to the filter context first within an outer CALCULATE, then an inner CALCULATE is performed with the actual filter arguments specified (none in your example).

     

    The expanded table lets you access columns on the 1-side of the relationship.

     

    So the calculated column added to 'Fact'

    Dim Name = CALCULATE(VALUES(Dim[name]))

    is translated to something like:

    Dim Name =
    VAR T = <filter from context transition> //which is the expanded Fact table corresponding to current row
    RETURN
    CALCULATE (
        CALCULATE (
            VALUES ( Dim[name] ),
        ),
        T
    )

    For all intents and purposes in a calculated column,

    CALCULATE ( VALUES ( Column on 1-side of relationship ) )

    is the same as

    RELATED ( Column on 1-side of relationship )

    So you could use CALCULATE (VALUES(...)) to refer to any column accessible by following many=>1 relationships, just like RELATED.

     

    Anyone else like to add/clarify? :)