Dynamic measure selectors
For certain kinds of reports, it sometimes makes sense to be able to select which measures should be displayed by checking off members on a dimension, rather than including individual measures from the field list. There are many blogs and articles online that describe this pattern in more details. You can find them by searching for dynamic measure selector switch.
Based on a question I received on GitHub recently, this article shows you how to auto-generate a disconnected table for selection together with the SWITCH measure to go along with that table. The technique here uses a Tabular Editor script, that is executed while a number of measures are multi-selected in the explorer tree.
Before executing the script below, you have to decide on a number of things:
- What would you like the disconnected 1-column selector table to be named?
- What would you like the column on the disconnected selector table to be named?
- What would you like to name the dynamic switch measure?
- On what table should the dynamic switch measure reside?
- If no filter or more than value is filtered on the selector table, what should the dynamic measure return?
For each question, substitute the strings at the top of the script with the values you decided upon.
Using the script
Simply paste the script into Tabular Editor, select a number of measures in the explorer tree (hold SHIFT or CTRL to multi-select). If your measures are scattered across multiple tables, Tabular Editor will not let you multiselect them (as you cannot select objects from different parts of the tree at once). However, a workaround is to use the filter functionality with search results shown in a flat list (click the button at the very right of the screen). For example, you can put a wildcard * in the filter box, or type :ObjectType = “Measure” to restrict the search to only show measures.
Once you selected the measures you want to include in the measure selector and the dynamic measure, hit F5 to execute the script. If there was a mistake, you can always hit CTRL+Z to undo the effects of running the script.
For example, if I run the script with this selection of measures:
The script sets the calculated table expression as follows:
…and the dynamic switch measure expression as follows: