Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago

DAX: How to append to filter context?

Hello,

 

How are values added to the filter context of a column?

 

For example, I want to write a measure "[Products including Red]" which shows the sales value of products of colours that the users select PLUS Red colour.

 

That is, in addition to the colours the users select, "Red" is always included.

If a user does not make a colour selection in the Product Colour slicer, ALL colours must be in the filter context (default Power BI behaviour)

 

Thanks

 

 

5 Replies

  • Hey Anonymous ,

     

    adding to an existing filter is  not that simple.

    From my understanding these steps are necessary:

    1. Check if the current filter contains the color
      1. if the current filter already contains the constant filter - do nothing
      2. if not, add the constant filter

    As the Retail Analysis sample pbix I just downloaded from the link you provided did not contain a slicer "product color", I used the 'Item'[Category] column instead, here I want to add "010-Womens".

    The following measure adds the value "010-Womens" to an existing filter:

    Total Units including 010-Womens = 
    var CategoryToInclude = "010-Womens"
    var CategoryIsFiltered = ISFILTERED('Item'[Category])
    return
    IF(
        CategoryIsFiltered
        , var _CurrentContainsCategory = CONTAINSROW(VALUEs('Item'[Category]) , CategoryToInclude)
        return
        IF(_CurrentContainsCategory
            , [TotalUnits]
            , //adding the constant category to the filter
            CALCULATE(
                [TotalUnits]
                , TREATAS(UNION(VALUES('Item'[Category]) , ROW("Category" , CategoryToInclude)) , 'Item'[Category])
            )
        )
        , [TotalUnits]
    )

    Here is a table visual:

    Hopefully, this provides what you are looking for.

     

    Regards,

    Tom

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi TomMartens 

       

      thanks for your solution.

      I ended up doing the below. Please let me know if you find any errors/pitfalls in this approach.

       

       

       

       

      CALCULATE
      (
        [Sum of sales],
        FILTER( 
      	ALL('Product'[Product Colour]),
      	'Product'[Product Colour] IN VALUES('Product'[Product Colour]) || 'Product'[Product Colour] = "Red"
        )
      )

       

       

       

       

  • Hi Anonymous ,

     

    A screenshot of the sample data will be helpful here to give a solution.

     

    Thanks,

    Pragati

  • Anonymous's avatar
    Anonymous
    Not applicable

    Anonymous 

    Power bi currently do not support add values to a current column, no matter whether it is filtered or not. You can only create new measure or column using CONCATENATE(), to add values to a column.

     

    https://docs.microsoft.com/en-us/dax/concatenate-function-dax

     

    Paul Zheng _ Community Support Team
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.