How it works...

The first seven VAR statements simply store components of the text that we will display. The __TOOMANY_MAX variable stores the maximum number of items that will be displayed. We need this since, otherwise, the text output can become too long to display within a visualization. The next variable, __TOTAL_ROWS, is the count of how many total distinct items we have in our base table. The variable after that, __CURRENT_ROWS, is the count of how many current distinct items we have chosen in our slicer.

The next variable, __MAINTEXT, is more complex and the heart of our dynamic slicer title. The value of this variable is determined by a SWITCH statement with various conditions. The first condition we check is whether the current rows equal the total rows in our base table and, if so, __MAINTEXT gets the value for all items, __ALLTEXT. The second condition checks whether there are no rows left and, in this case, __MAINTEXT gets the value for no items, __NONETEXT. The third condition checks whether there is only a single row and, in this case, __MAINTEXT simply takes on the value of the single item we have selected in our slicer. The next condition checks to make sure that we have not reached the maximum individual items that we will display. If this is not the case, then we use CONCATENATEX to concatenate all of the slicer items together, separated by __CONCATENATE_TEXT, except for the last item. We tack the last item onto the text by concatenating the __LASTCONCATENATE_TEXT variable, coupled with using LASTNONBLANK to grab the last selected item. Finally, if none of the previous conditions are met, we know that we have too many items to display individually, and so __MAINTEXT gets the value stored by the __SIMPLEMULTIPLETEXT variable.

In this simple dynamic title slicer, we set a variable called __PRETEXT to the value stored in the __DIRECTFILTERPRETEXT variable, and finally return the concatenation of our __PRETEXT, __MAINTEXT, and __POSTTEXT variables.