Reply
Highlighted
Member
Posts: 67
Registered: ‎10-18-2016
Accepted Solution

Reverse Contains?

[ Edited ]

I have a table containing listings of of our sales, and each row contains (among other data) the product(s) sold in that sale.

 

I have a slicer containing various types of products. The idea is that the table will only show those sales that include one or more of the products selected in the slicer.

 

But the product names are many, and the product types are few. For example, here are some sample product names:

 

Dining Table (6)
Dining Table (8)
Card Table
Coffee Table (Pine)
Bedside Table
Chair (Wooden)
Chair (Plastic)
Collapsible Chair
Wardrobe (Wood)
Wardrobe (Built-In)
Wardrobe (Small)
Couch (Leather)
Couch (Fabric)

One of the values selectable in the slicer is 'Table'. So if the user chooses 'Table', I want to show all of the values in the table above which contain the string 'table'. I'm using a simple search to see if products should be shown - if the slicer value is found in the product name, then I show sales featuring that product. Great.

 

But if the user chooses multiple values in the slicer, I'm in trouble. For example, they might choose 'Table' and 'Chair'. In that case, I want to show all sales featuring products that contain either the string 'Table' or the string 'Chair'.

 

I can get all of the selected values in the slicer using CONCATENATEX, but there's no way using DAX that I can iterate through them, checking (for each row in the database table) whether each of the selected values can be found within the product name.

 

I thought of creating a table at runtime with the values selected in the slicer - but even if I do so, I can't use 'CONTAINS' because it goes the wrong way - I want to search for each row in the table in a single product name, rather than search for a single product name in each row in the table.

 

Thanks for any help.


Accepted Solutions
Regular Visitor
Posts: 23
Registered: ‎08-16-2017

Re: Reverse Contains?

[ Edited ]

If you have a table that contains your product types (not related to your product table), then you can write a measure that checks if the product matches any of the selected ones:

 

Visible = 
MAXX(
    VALUES(ProductTypes[Type]),
    MAXX(Products,
        IF(
            SEARCH([Type], Products[Product], 1, 0) > 0,
            1,
            0
        )
    )
)

OrLogic2.png

Then just set the visual level filter to only show [Visible] = 1.

View solution in original post


All Replies
Regular Visitor
Posts: 23
Registered: ‎08-16-2017

Re: Reverse Contains?

[ Edited ]

If you have a table that contains your product types (not related to your product table), then you can write a measure that checks if the product matches any of the selected ones:

 

Visible = 
MAXX(
    VALUES(ProductTypes[Type]),
    MAXX(Products,
        IF(
            SEARCH([Type], Products[Product], 1, 0) > 0,
            1,
            0
        )
    )
)

OrLogic2.png

Then just set the visual level filter to only show [Visible] = 1.

Member
Posts: 67
Registered: ‎10-18-2016

Re: Reverse Contains?

Thank you, Alexis - genius! Now I've just got to work out why and how it works Smiley Happy

Regular Visitor
Posts: 23
Registered: ‎08-16-2017

Re: Reverse Contains?

Sorry, I should have explained it better.

 

Let's go from the inside out:

 

  • The SEARCH function will return the position of the Type in the Product string if it finds it (returns 0 otherwise).
  • If the position is greater than 0, then Type is a substring of Product and the IF function returns 1 (0 otherwise).
  • The inner MAXX iterates through the list of products and the IF gives 1 for all the ones that match the current Type. (Within the filter context of the table, this list will only be the with the current product.)
  • The outer MAXX is iterating through each product type that has been selected with the slicer and checks for each Type whether that string is a substring of Product using the inner MAXX. If any of the Types selected returns a 1, then the max over all the types is 1, just like OR logic.

If the inner MAXX part doesn't make complete sense, that's OK. I accidentally made it more complicated than it needed to be. Here's a slightly simpler version that's a bit easier to follow.

 

Visible = 
VAR CurrentProduct = SELECTEDVALUE(Products[Product])
RETURN MAXX(VALUES(ProductTypes[Type]), IF(SEARCH([Type], CurrentProduct, 1, 0) > 0, 1, 0))