Forum Discussion

Read39487's avatar
Read39487
Regular Visitor
8 months ago
Solved

Dynamic text based on filter selection

Hi all,   I'm looking for a method to display a text dynamically based on filter selection. I have checked different methods suggested in other articles but none works as expected so far.   The ...
  • rohit1991's avatar
    8 months ago

    Hii Read39487 

     

    To return dynamic text based on the selected products, you first need to detect how many items from Group 1 are selected. Once you know the count, you can build the text accordingly. A simple pattern is:

    SelectedText :=
    VAR L = VALUES('Table'[Product])
    VAR C = COUNTROWS(L)
    RETURN
    SWITCH(
        TRUE(),
        C = 3, "Group 1",
        C = 1, MAX('Table'[Product]),
        C = 2, CONCATENATEX(L, [Product], "/"),
        "Multiple"
    )

    This returns “Group 1” when A,B,C are all selected, returns A, B, or C when only one is selected, and returns A/B, A/C, etc. when two are selected. This approach reliably handles all three scenarios you described.

  • MFelix's avatar
    8 months ago

    Hi Read39487 ,

     

    Try the following code:

    Dynamic Text =	SWITCH(
    		TRUE(),
    		COUNTROWS('Table') = COUNTROWS(FILTER(
    			ALL('Table'),
    			'Table'[Group] = MAX('Table'[Group])
    		)), SELECTEDVALUE('Table'[Group]),
    		COUNTROWS(SUMMARIZE(
    			'Table',
    			'Table'[Group]
    		)) = 1 &&
    		COUNTROWS('Table') <= COUNTROWS(FILTER(
    			ALL('Table'),
    			'Table'[Group] = MAX('Table'[Group])
    		)), CONCATENATEX(
    			'Table',
    			'Table'[Product],
    			"/"
    		),
    		COUNTROWS('Table') = 1, SELECTEDVALUE('Table'[Product]),
    		"Multiple Values"
    	)

     

     

     

  • danextian's avatar
    8 months ago

    Hi Read39487 

    Try this:

    text = 
    -- Get the currently selected product (if only one is selected)
    VAR _prodSelected = SELECTEDVALUE(Tbl[Product])
    
    -- Get the currently selected group (if only one is selected)
    VAR _group = SELECTEDVALUE(Tbl[Group])
    
    -- Get all products in the current filter context
    VAR _productsSelected = VALUES(Tbl[Product])
    
    -- Get all products that belong to Group 1, ignoring current filters
    VAR _group1Products = 
        CALCULATETABLE(
            VALUES(Tbl[Product]), 
            FILTER(ALL(Tbl), Tbl[Group] = "Group 1")
        )
    
    -- Check if all selected products are exactly Group 1 products
    VAR _allGroup1Selected = 
        VAR _selectedVsGroup = COUNTROWS(EXCEPT(_group1Products, _productsSelected))   -- Items in Group 1 not selected
        VAR _groupVsSelected = COUNTROWS(EXCEPT(_productsSelected, _group1Products))   -- Items selected not in Group 1
        RETURN ISBLANK(_selectedVsGroup) && ISBLANK(_groupVsSelected)                  -- True if selection matches all of Group 1 exactly
    
    -- Precompute concatenated product names for multiple selections
    VAR twoProducts = CONCATENATEX(_productsSelected, [Product], "/")
    
    -- Check if a subset of Group 1 products is selected
    VAR _subsetGroup1Selected = 
        COUNTROWS(INTERSECT(_productsSelected, _group1Products)) = COUNTROWS(_productsSelected) &&
        COUNTROWS(_productsSelected) < COUNTROWS(_group1Products)
    
    -- Determine the final result based on selection logic
    VAR _result  = 
    SWITCH(
        TRUE(),
        -- If a single product D or E is selected, return its group
        _prodSelected IN {"D", "E"}, _group,
    
        -- If all Group 1 products are selected and no other products, return "Group 1"
        _allGroup1Selected, "Group 1",
    
        -- If a subset of Group 1 products is selected, return the concatenated product names
        _subsetGroup1Selected, twoProducts,
    
        -- If any other single product is selected, return that product
        NOT _prodSelected IN {"D", "E"}, _prodSelected
    )
    
    -- Return the result
    RETURN
    _result
    

     

  • Kedar_Pande's avatar
    8 months ago

    Read39487 

     

    Dynamic Selection =
    VAR SelectedItems = VALUES('Table'[Product])
    VAR ItemCount = COUNTROWS(SelectedItems)
    VAR GroupName = MAX('Table'[Group]) // Assumes Group column exists
    RETURN
    SWITCH(
    TRUE(),
    ItemCount = 1, SELECTEDVALUE('Table'[Product]), // Single item: "A"
    ItemCount = 3 && HASONEVALUE('Table'[Group]), GroupName, // All Group 1: "Group 1"
    CONCATENATEX(SelectedItems, 'Table'[Product], "/", "") // Multiple: "A/B"
    )

     

    If this answer helped, please click Kudos or Accept as Solution.
    -Kedar
    LinkedIn: https://www.linkedin.com/in/kedar-pande