ISINSCOPE in DAX: different logic for totals and levels

How ISINSCOPE tells which hierarchy level a matrix cell is on in DAX, blanking totals, percent of parent at each level, different calculations per level, ISINSCOPE against HASONEVALUE and ISFILTERED, and ISATLEVEL in visual calculations.

Short answer: ISINSCOPE ( column ) returns TRUE when the visual is grouping by that column at the current cell, that is, on the rows of that level and below, and FALSE on subtotals and totals above it. Use it to blank totals that make no sense (a rank, an average price), to show percent of parent at each level of a matrix, and to switch the calculation by level. Unlike HASONEVALUE, it is not fooled by a slicer that selects one value.

A matrix with Category, Product and a "rank" measure shows a rank on the total row, which means nothing. ISINSCOPE says "only show this at product level".

How does it differ from HASONEVALUE?

RowISINSCOPE ( Product )ISINSCOPE ( Category )HASONEVALUE ( Category )
Bikes, Mountain-200TRUETRUETRUE
Bikes (subtotal)FALSETRUETRUE
TotalFALSEFALSETRUE
A matrix with Category and Product on rows, slicer on Category = Bikes

On the grand total, HASONEVALUE ( Category ) is TRUE because the slicer leaves one category; ISINSCOPE ( Category ) is FALSE because the total does not group by category. That is why ISINSCOPE is the right test for "am I on a total row?".

Which patterns use it?

-- 1 blank the total of a rank
Product Rank =
IF ( ISINSCOPE ( 'Product'[Product] ), RANKX ( ALLSELECTED ( 'Product'[Product] ), [Revenue] ) )

-- 2 percent of parent at each level
% of Parent =
SWITCH (
    TRUE (),
    ISINSCOPE ( 'Product'[Product] ),
        DIVIDE ( [Revenue], CALCULATE ( [Revenue], ALLSELECTED ( 'Product'[Product] ) ) ),
    ISINSCOPE ( 'Product'[Category] ),
        DIVIDE ( [Revenue], CALCULATE ( [Revenue], ALLSELECTED ( 'Product'[Category] ) ) ),
    1
)

-- 3 sum of per-product values on totals, a formula on rows
Bonus =
IF (
    ISINSCOPE ( 'Product'[Product] ),
    [Revenue] * 0.02,
    SUMX ( VALUES ( 'Product'[Product] ), [Revenue] * 0.02 )
)

Test the lowest level first in SWITCH ( TRUE () ): at product level both Product and Category are in scope.

Category, ProductRevenue% of Parent
Bikes2,10072%
Mountain-20090043%
Road-1501,20057%
Accessories82028%
Helmet50061%
Bottle32039%
Total2,920100%
% of Parent in a matrix

What about visual calculations?

In visual calculations, ISATLEVEL ( [Field] ) reports whether a field is present at the current level, and COLLAPSE gives the parent's value directly: DIVIDE ( [Revenue], COLLAPSE ( [Revenue], ROWS ) ) is percent of parent without any ISINSCOPE.

What goes wrong?

  • Testing the higher level first. At product level Category is also in scope; test Product first.
  • HASONEVALUE for totals. Fooled by slicers; use ISINSCOPE.
  • Columns not on the visual. ISINSCOPE is FALSE for a column that is not grouped, even if filtered.
  • DirectQuery. Not supported in calculated columns or RLS rules in DirectQuery mode.

Where can you check this?

Where do you start?

Wrap every rank and per-unit measure in IF ( ISINSCOPE ( ... ) ) so totals stop showing meaningless numbers. RANKX uses it for the total row.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.