Blog Post

Power BI Community Blog
2 MIN READ

How To: Display All Selected Filters at Once

Alex_RM's avatar
Alex_RM
Icon for Advocate I rankAdvocate I
3 months ago

Here is what we want to achieve: a dynamic list of all our filters, directly on our report pages.

 

dsfdsfdssdf

Example of filters list displayed on the report pages.

 

Here is the process:

  1. Create a new DAX query in your PowerBI Desktop file,
  2. Copy/paste the DAX code at the end of this message,
  3. Execute it and copy the result,
  4. Create a new measure and paste the copied result,
  5. Display the newly created measure (called AppliedFiltersHTML) in a HTML-rendering visual, such as the excellent HTML Content Lite.

And that's all!

 

If you make any change to your data model, just re-run the steps 3 & 4 to refresh the list of fields monitored by the measure.

 

You can adapt the following code to your needs (to display datetime in your local format for example):

DEFINE
	VAR excludedTables = {"Table you want to exclude here", "Some other table"}
	
    VAR tabCols = INFO.VIEW.COLUMNS()
    VAR tabTabs = INFO.VIEW.TABLES()
	VAR tabHierarchies = INFO.LEVELS()

    VAR filteredTabTabs = FILTER(tabTabs, [IsHidden] = FALSE())

    VAR filteredTabCols =
        FILTER(
            tabCols,
            [Table] IN SUMMARIZE(filteredTabTabs, [Name]) &&
			NOT [Table] IN excludedTables &&
            [DataCategory] = "Regular"
        )

    VAR addCols =
    GROUPBY(
        filteredTabCols,
        [ID],
        [Table],
		[DataType],
        "Col",
        MINX(CURRENTGROUP(), "'" & [Table] & "'[" & [Name] & "]"),
        "ColDesc",
        MINX(CURRENTGROUP(), [Name])
    )

    VAR addExpressions = 
        ADDCOLUMNS(
            addCols,
			"EXP_ColHierarchy",
			VAR colId = [ID]
			VAR tabHierarchiesFiltered = FILTER(tabHierarchies, [ColumnID] = colId)
			VAR hierarchyId = MINX(tabHierarchiesFiltered, [HierarchyID])
			
			VAR tabHierarchiesFiltered2 = FILTER(tabHierarchiesFiltered, [HierarchyID] = hierarchyId)
			VAR hierarchyOrdinal = MINX(tabHierarchiesFiltered2, [Ordinal])
			
			VAR r = hierarchyId & "_" & FORMAT(hierarchyOrdinal, "00")
			
			RETURN r,
            "EXP_ColValues",
            "
            VAR isColFiltered = ISFILTERED(" & [Col] & ") 
            VAR selectedValues = VALUES(" & [Col] & ") 
            
			VAR filteredLabel = ""<u>" & [ColDesc] & "</u> → ""
            VAR filteredValues = 
				" & 
					SWITCH(
						[DataType],
						"Date",
						"
						VAR minDate = MIN(" & [Col] & ")
						VAR maxDate = MAX(" & [Col] & ")
						VAR r = IF(minDate = maxDate, minDate, ""from "" & minDate & "" to "" & maxDate)
						RETURN r
						",
						"CONCATENATEX(selectedValues, " & [Col] & ", "", "") "
					)
				& "
			
			VAR r = IF(isColFiltered, filteredLabel & filteredValues, BLANK())
            RETURN r
            "
        )
    
    VAR r = 
        "AppliedFiltersHTML = 
        VAR t = {" & CONCATENATEX(addExpressions, [EXP_ColValues], ", ", [Table], ASC, [EXP_ColHierarchy], ASC) & "}
        VAR f = FILTER(t, NOT ISBLANK([Value]))
        VAR r = ""<p style=""""line-height: 1.6;"""">"" & CONCATENATEX(f, [Value], ""<br>"") & """"
        RETURN r"

    VAR rt = {r}

EVALUATE rt

 

Updated 4 months ago
Version 1.0

3 Comments

  • I had a similar scenario where I needed to show a list of what slicer selections were applied, across about 20 different slicers.  The challenge though, was that I wanted to conditionally format when at least one value from the slicers was selected, so rendering it all as one measure or field wouldn't work with out of the box PBI visuals.  (In my scenario custom visuals are not supported) 

    So the way that I achieve that was to first create a UDF that concatenates the slicer selections togehter, and if nothing was selected to show "No filters selected": 

     

    slicerSelections_dyn = (
    //fieldName: string,
    columnName: anyref	
    ) =>
    
    VAR _slicerSelected =
        IF (
            ISFILTERED ( columnName ),
            CONCATENATEX (
                DISTINCT ( columnName ),
                columnName ,
                ", ",
                columnName , ASC
            ),
            "No Filter Selected"
        )
    	
    	RETURN 
    	 _slicerSelected & REPT ( UNICHAR ( 10 ), 2 )

     I then created a Calculation Group called "Slicer Selections" and created calculated items for each of the slicers that were used in the report.  The initial set up was a bit of overhead, but ultimately from the reporting side, it makes things easier if anything changes.  So for each calculated item, it uses the above UDF: 

     

    Slicer(s) = slicerSelections_dyn('Table'[column name])

     

    I then created a measure called slicer selections which would be used in the table alongside the calculated items: 

    slicer selections = SELECTEDVALUE('Slicer Selections'[Name])


    With everything now set up, I used a table visual which contains the 'Slicer selection'[Name] field, and the slicer selections measure for columns.  I then set up conditional formatting for the measure (renamed Item(s) Selected here) so when a value is selected, it renders green, otherwise the default light grey would apply.  

     

    Then the rest is formatting in terms of the table visual.  

     

     

  • Luishonores there is likely a problem escaping the double-quotes in your PowerBI.

     

    You can try to write a simple measure : TestMeasure = "Display of double-quotes: """.

    It should display exactly Display of double-quotes: ".

    If it is, be sure you exactly copy/pasted the generated AppliedFiltersHTML formula, without changing anything. I re-tested it on one of my reports, it worked great.

    If it's not, you probably have a localization problem, try to change your settings.