Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Slicer to filter table before UNION

Hello PowerBi Community,   I searched the forum before and couldnt find anything that wopuld help my case.   I have two tables (Letters and Numbers). Value table is the UNION between the two abo...
  • Anonymous's avatar
    Anonymous
    5 years ago

     

    // First, you have to create
    // the RIGHT union:
    
    [Values] = // calculated table
    UNION(
        SELECTCOLUMNS(
            Letters,
            "Value",
                Letters[Letter],
            "Table",
                "Letters"
        ),
        SELECTCOLUMNS(
            Numbers,
            "Value",
                // Since the column in the first
                // table is text, all the numbers
                // must be turned into text as well,
                // hence the suffix >>& ""<<.
                Numbers[Number] & "",
            "Table",
                "Numbers"
        )
    )
    
    // The column 'Values'[Table] MUST
    // be hidden. It'll only be used
    // by the filtering measure below.
    // There should be *NO* relationship
    // from any of the original tables
    // to the one above.
    
    // To the table Numbers add a hidden column
    // called NumberAsText. This will help later
    // with the filtering measure. The definition
    // of the column is:
    [NumberAsText] = Numbers[Number] & ""
        
    
    // This is the filtering measure that you'll
    // use in the visual's Filtering Pane and
    // you'll only show the rows where the measure
    // returns 1.
    [Should Show Row?] =
    IF( ISINSCOPE( 'Values'[Value] ),
        
        var vCurrentValue = SELECTEDVALUE( 'Values'[Value] )
        var vCurrentValueTable = SELECTEDVALUE( 'Values'[Table] )
        return
        SWITCH( TRUE(),
    
            // Selections made from both tables
            ISFILTERED( Letters ) && ISFILTERED( Numbers ),
                var vLetters = DISTINCT( Letters[Letter] )
                var vNumbers = DISTINCT( Numbers[NumberAsText] )
                var vShouldKeepRowVisible =
                    or(
                        vCurrentValue in vLetters,
                        vCurrentValue in vNumbers
                    )
                var vResult = int( vShouldKeepRowVisible )
                RETURN
                    vResult,
            
            // Selection made from Letters only
            ISFILTERED( Letters ),
                var vLetters = DISTINCT( Letters[Letter] )
                var vShouldKeepRowVisible =
                    or(
                        vCurrentValue in vLetters,
                        vCurrentValueTable = "Numbers"
                    )
                var vResult = int( vShouldKeepRowVisible )
                return
                    vResult,
            
            // Selection made from Numbers only
            ISFILTERED( Numbers ),
                var vNumbers = DISTINCT( Numbers[NumberAsText] )
                var vShouldKeepRowVisible =
                    or(
                        vCurrentValue in vNumbers,
                        vCurrentValueTable = "Letters"
                    )
                var vResult = int( vShouldKeepRowVisible )
                return
                    vResult,
    
            // If nothing is filtered... show everything.
            1
        )
    )