The second DAX: computational context

Computing environment is context calculation formula, any DAX expressions are evaluated in this context. Filter-line context and context is the only DAX context type, context referred to these two calculation context. Calculating the context of defining formula for the environment, when the context change, the same formula will calculate different results.

First, the computational context

Created in PowerBI in a row computed column is executed in the context calculation, measurement (meassure) is created to perform calculations in a screening context.

  • Context is always contains a row line context, DAX automatically when creating a computed column lines of context definitions.
  • Screening by context slicer (Slicer,), the user selects the packet field, the relationship between the direction of the screening filters such as a common definition, and context to use filters DAX formula (metric).

Second, the lines of context

Line context exists only in the same row of the table, the effect is the result of different columns in the same row of the table, the application of the formula, the formula for calculating the progressive.

For example, to create a calculated column Profit, profit sales of goods by the formula - Tax - Cost - shipping, DAX formula is as follows:

Profit = FactSales [ SalesAmount ] - FactSales [ TaxAmt ] - FactSales [ TotalProductCost ]  - FactSales [ the Freight ]

DAX progressive application of the formula for each row in the table, will be according to the formula, the values ​​are substituted on different columns of the same row into the formula, the calculated result of the formula:

 

In fact, the line and for performing calculations in the formula is not stored, but is defined in the context of the row. From a logical point of view, it is computed columns workflow:

  • When you define a calculated column, DAX start from the first row of the table iteration, first create a line and expression evaluation context contains the line,
  • It then moved to the second line, find expression again;
  • This happens in all the rows in the table, if you have 100 million lines, you can think DAX create a million lines of context to evaluate this formula million times.

Third, screening context

Performing metric calculation formula is created in the context of screening, the screening context is a context after the application after the filter table, the data being filtered. Interaction context is screened by the filter, filters are: slicer (Slicer,), grouping columns, and relationships. The power of that DAX, DAX automatically applies not only to the screening context, also support the modification, removal and re-define the screening context.
1, the automatic application screening context
The definition of a metric PowerBI FactSlaes table, the total sales for the calculation:
Measure SumOfAccount = CALCULATE(SUM(FactSales[SalesAmount])) 

A packet metric for increasing FullName column, it can be seen that a measure of the grouping columns context be filtered to only calculate the sales FullName each object, and the context of the following metrics automatically apply the filter, calculate each of FullName sales:

FullName DimCustomers table is a field, because this field can be automatically filtered FactSales table is in the data model, and there is a relationship between table FactSlaes DimCustomers table, and the relationship between the direction pointed by DimCustomer FactSales, which shows, DimCustomers can filter FactSales:

2 direction, relationship

When the direction of the point A is determined by the relationship B, A can be described filter B, which can be used to screen A. Figure above, between the table and the table FactSlaes DimCustomers there is a relationship, the relationship between the direction and directed by DimCustomer FactSales, which shows, DimCustomers can filter FactSales. We have to test whether FactSales can filter DimCustomers? Create a new metric, the number of tests using FactSales ProductKey to users of statistics:

Measure CountCustomer = CALCULATE(COUNT(DimCustomers[CustomerKey])) 

The results will be the value of Measure CountCustomers is the total number of DimCustomers table, that is, ProductKey not filter DimCustomers table.

When modifying the relationship between DimCustomer and FactSales is two-way:

For FactSales can filter DimCustomers, DAX context automatically apply the filter, the number of statistics corresponding to each customer ProductKey:

3, modify, remove and re-screening context definitions

DAX function can be used to modify, remove and re-defined screening context.

The CALCULATE () function is the most complex functions DAX, modify the expression for calculating the filter specified by the context.

CALCULATE(<expression>,<filter1>,<filter2>…) 

 The first parameter is used to calculate the metric value of the polymerization, the latter parameter is an optional filter, there are two types:

  • Logical expression returns a Boolean value
  • Expression that returns a table

CALCULATE complication is that the function of the variable data context. If the data has been filtered, the CALCULATE function changes the context filter data, and evaluates the expression in the new context that you specify. For each column used in the filter parameter, to remove any existing filter on the column, and apply the filter used in the filter parameter.

 

 

Reference documents:

The Bible [DAX] Chapter IV: Understanding calculation contexts (1)

Detailed CALCULATE Series - The Basics

Guess you like

Origin www.cnblogs.com/ljhdo/p/4577188.html