Forum Discussion
How expanded tables work
- 8 years ago
Your confusion lies in the fact that even though an expanded table is a series of left-outer joins of tables from many -> 1, the expanded table still has all the columns of all the tables. And tables are a collection of independent columns. Meaning that a filter on one column does not affect another until after applied to the model. (and even then the impact if from cross filtering rows) Take this code here:
Sum Amount = CALCULATE(SUM(t3Sales[Amount]),t3ProductName[ProductID])
internally, it is written as this:
Sum Amount = CALCULATE ( SUM ( t3Sales[Amount] ), FILTER ( ALL ( t3ProductName[ProductID] ), t3ProductName[ProductID] ) )So it will return all the ProductID's from the t3ProductName column. But the table also has a filter on t3Sales[ProductID] because you put it on the rows which when combined filters down the t3Sales table to just where t3Sales[ProductID] = 100 for row 1, and so on for rest of the table. Grand total has no filter on t3Sales[ProductID] and only has the above filter on tsProductName[ProductID] column which returns all values, so you get the correct grand total.
next is your Sum Amount2
Sum Amount2 = CALCULATE(SUM(t3Sales[Amount]),t3Sales[ProductID])
which internally is rewritten as:
Sum Amount2 = CALCULATE ( SUM ( t3Sales[Amount] ), FILTER ( ALL ( t3Sales[ProductID] ), t3Sales[ProductID] ) )So, even though you put t3Sales[ProductID] on the rows, since you also referenced that column as a filter argument to CALCULATE, the table row filter is overridden by the above FILTER statement which returns all ProductID for t3Sales for every row which is why you get the grand total amount for every row in table. Difference from Sum Amount is now there is no filter on the t3ProductName[ProductID].
Make sense?
- 8 years ago
"Context Transition" refers to moving a column filter from row contex to filter context.
"Modify filter context" means just that. When you have columns on the rows, columns, filter area, slicers of report that is the initial filter context. Either row context transition or arguments to 'CALCULATE' may change ( or modify) that filter context.
Hi gvg,
Based on my test, I found that if we add t3Sales[ProductID] into table visual, and for measure [SumAmount1], we refer to its related table column t3ProductName[ProductID], it will calculate total values grouped by ProductID. However, for measure [SumAmount2], we refer to its own column t3Sales[ProductID], the formula is considered as SumAmount2 = CALCULATE(SUM(t3Sales[Amount]),ALLSELECTED(t3Sales[ProductID])), means regardless of t3Sales[ProductID]. So, in your scenario, you should modify the formula for SumAmount2 to:
SumAmount2 = SUM(t3Sales[Amount])
Best regards,
Yuliana Gu
Well, I have t3Sales[ProductID] in both tables, that I am putting to test. However interestingly, if I change t3Sales[ProductID] to t3ProductName[ProductID] in the last visual, I get correct results. Getting totally confused :(
Here is my test pbix file Expanded table test .
- mattbrice8 years agoSolution Sage
Your confusion lies in the fact that even though an expanded table is a series of left-outer joins of tables from many -> 1, the expanded table still has all the columns of all the tables. And tables are a collection of independent columns. Meaning that a filter on one column does not affect another until after applied to the model. (and even then the impact if from cross filtering rows) Take this code here:
Sum Amount = CALCULATE(SUM(t3Sales[Amount]),t3ProductName[ProductID])
internally, it is written as this:
Sum Amount = CALCULATE ( SUM ( t3Sales[Amount] ), FILTER ( ALL ( t3ProductName[ProductID] ), t3ProductName[ProductID] ) )So it will return all the ProductID's from the t3ProductName column. But the table also has a filter on t3Sales[ProductID] because you put it on the rows which when combined filters down the t3Sales table to just where t3Sales[ProductID] = 100 for row 1, and so on for rest of the table. Grand total has no filter on t3Sales[ProductID] and only has the above filter on tsProductName[ProductID] column which returns all values, so you get the correct grand total.
next is your Sum Amount2
Sum Amount2 = CALCULATE(SUM(t3Sales[Amount]),t3Sales[ProductID])
which internally is rewritten as:
Sum Amount2 = CALCULATE ( SUM ( t3Sales[Amount] ), FILTER ( ALL ( t3Sales[ProductID] ), t3Sales[ProductID] ) )So, even though you put t3Sales[ProductID] on the rows, since you also referenced that column as a filter argument to CALCULATE, the table row filter is overridden by the above FILTER statement which returns all ProductID for t3Sales for every row which is why you get the grand total amount for every row in table. Difference from Sum Amount is now there is no filter on the t3ProductName[ProductID].
Make sense?
- gvg8 years agoPost Prodigy
Yes, this makes sense. Thanks!
- gvg8 years agoPost Prodigy
Well, I am still a little confused. Let's break this all down in small steps for Sum Amount2 for the first value. My understanding is that before CALCULATE starts its job
1. The expanded table is filtered on t3Sales{ProductID]=100 since this field is in the visual. Thus our expanded table looks like this:
2. CALCULATE starts evaluating its filter. This FILTER function sees the table shown in step 1.
3. Since FILTER includes ALL as per your description, the expanded table gets expanded to the original like this:
Or does it expand to this only:
4. CALCULATE begins interpreting the filtered expanded table, which in fact is the whole (?) table.
5. CALCULATE makes context transition, i.e. filters the expanded table with t3Sales[ProductID]=100 like this:
6. Sums the Amount. Shouldn't it arrive at 50?