When I work with customers‘ pbix files, I often deal with files with several hundred measures. At the beginning I like to get an overview which of these measures might not be used at all and could possibly be removed. For this purpose, I have developed a method to locate these measures, which I would like to share in this article.
You can download my report file at the end of this post. The download does not require a password. If you are still asked for your OneDrive password, change your browser to Edge.
The definition of unused measures
Unused Measures are Measures that are neither used in any visualization in the .pbix file nor referenced in other Measures, calculated columns or calculated tables! So how can I identify those Measures? Let’s find the answer together…
Gather the necessary information
To find the unused measures, I need the following information:
- A list of all existing measures in the .pbix file,
- A list of the measures used in the visualizations within the .pbix file
- A list of measures referenced by other measures, calculated columns and/or calculated tables.
The logic is the following: When I remove from the all measures list those measures that are used in visualizations, I have those measures that are not used in visualizations. But they can still be referenced in other calculations (and therefore cannot be removed). So, if I hold against the list of all referencing calculations here, I finally get the measures that are not used within the .pbix file.
Retrieve a list of all measures
Since Power BI Desktop runs an instance of SQL Server Analysis Services Tabular in the background, I can use a DMV (Data Management View) to get information about my data model. You can use Power Query to gather this information using the following single line of code:
= OleDb.Query("Provider=MSOLAP.8;Data Source=localhost:61200;Update Isolation Level=2", "SELECT * FROM $SYSTEM.MDSCHEMA_MEASURES")
The result looks like this:
Be aware, that the number 61200 has to be changed in your code, as this is the process ID of the instance of Analysis Services running on your machine. The easiest way to find this number on your machine is to run DAX Studio (download it here) and connect it to your (already opened) pbix file. When you are connected, you find the desired process number in the bottom line, as you can see here:
Now that we have a list of all measures, let’s find those measures, that are used in report pages.
Retrieve a list of Measures, used in report pages in Power BI Desktop
Since May 2019 Power BI Desktop has been equipped with the extremely useful „Performance Analyzer„. This tool helps to find starting points for the optimization of slow reports. But this tool also helps me to identify those measures that are actually used on report pages. Follow these steps:
- Create a blank page
- Go to View → Performance Analzer and make the pane visible
- Click on Start recording
- Then select each page, one after another and wait until the visuals are fully loaded
- Press Stop
- Export the JSON file
The JSON file contains all DAX queries, generated to update the visuals when selecting the page. If a measure is used in a visual, it is represented in the DAX query and I can extract that information from the JSON file. Of course I use Power Query to do that job for me and the result looks like this:
With the retrieval of all measures and the JSON file, with all measures used in visuals, I can now make a comparison and determine which measures are not used in visuals. So far, so good. However, just because a measure is not used directly in a visual, it is not automatically unused. It can be referenced in other measures, calculated columns, or calculated tables. Therefore I would like to check whether these now determined measures are really completely unused before I decide how I want to proceed with them…
Retrieve a list of calculation dependencies
The DMV, which I used for the retrieval of all existing measures, is by far not the only one available. Another useful DMV I will use to determine the measures referenced by other measures, calculated columns, or calculated tables is the following:
$SYSTEM.discover_calc_dependency (see documentation of this DMV here).
I can again use Power Query to retrieve the results of this DMV like so:
= OleDb.Query("Provider=MSOLAP.8;Data Source=localhost:61200;Update Isolation Level=2", "SELECT * FROM $SYSTEM.discover_calc_dependency")
Again, the process id 61200 has to be changed to find the Analysis Services instance on your machine.
The result looks like this:
This DMV returns a clear picture of the interdependencies of calculations. For me this DMV answers the question in which measures, calculated columns and/or calculated tables my measure is referenced. If a measure is not used in any visualization and does not appear in the calculation dependencies, it is unused.
The final table
With all this information, I can now create the following table, which tells me whether a measure is used in report pages and whether it is referenced in any way:
The screenshot shows in line 5 that the measure _Distinct_Medals is not used in visuals, but is at least referenced by other measures, calculated columns and/or tables. The measure „Not_used_anywhere“ is neither referenced nor used in visuals.
Download and usage of my Measure Analysis pbix file
I loaded all the described data from DMVs and the JSON export file into a pbix file and added a (very small, but helpful) report. Admittedly, I didn’t take any trouble with the optical design. I’ll leave that to you 😉
You can download the report file here.
Open the file and edit two parameters:
- the process ID of the Analysis Services instance
- the path and name of the exported JSON file (Make sure that your string does not contain quotation marks at the beginning and at the end)
After you have clicked OK, you will see the following message in the upper part of the report page:
Click on „Apply changes“ and click on „Run“ in the following dialog, to allow the run of the DMV MDSCHEMA_MEASURES:
Then you will be asked to enter your credentials. Use „Windows“ and „Use my current credentials“:
After you have entered your credentials, Power Query prompts you to determine the privacy levels for the data source. Since in this particular case the communication between this PBIX file and the Analysis Services instance is behind the file to be analyzed, you can safely click on „Ignore Privacy Levels…“:
Click on „Apply changes“ and click on „Run“ in the following dialog, to allow the run of the DMV :
After the model refreshed, you can use the following report:
Here is a short description of the report:
- Column1: Table name and Measure name.
- Column2: If the measure was stored in a folder, you can read it here.
- Column3: DAX-Expression of the measure
- Column4: Is the measure used in a visual, somewhere in this pbix file?
- Column5: Is this measure referenced by any measure, calculated column/ table in this pbix file?
I hope that this manual will help you to bring even more order into your data models 🙂
Greetings from Germany,
Lars ist Berater, Entwickler und Trainer für Microsoft Power BI. Er ist zertifizierter Power BI-Experte und Microsoft Trainer. Für sein Engagement in der internationalen Community wurde Lars seit 2017 jährlich durch Microsoft der MVP-Award verliehen. Lies hier mehr…