C# Script To Get A List Of Measures Matching Column Names
C# script to get a list of measures that are same as column names
This is not a Fabric related blog; well technically it is because Power BI is part of Fabric :P. It's just easier for me to write the blog here compared to my previous blog at www.pawarbi.com so dropping it here.
I was working on a project that had several measures with the same name as column names in tables which caused some issues. Avoid doing that, especially working with a large model. It's easy to refer to one instead of the other. You cannot create a measure in a table that matches the name of one of the columns in that table but if you use a measures table, there is no such restriction. Use the below script in Tabular Editor to return a list of measures that matches column names. Not sure if it's already part of the BPA, but it should be.
// Sandeep Pawar | fabric.guru
var _cols = new List<string>();
var _measures = new List<string>();
// Loop through each table in the model to get column names
foreach (var table in Model.Tables)
{
_cols.AddRange(table.DataColumns.OrderBy(c => c.SourceColumn).Select(c => "[" + c.SourceColumn + "]"));
}
// Loop through each table in the model to get measure names
foreach (var table in Model.Tables)
{
_measures.AddRange(table.Measures.OrderBy(m => m.Name).Select(m => "[" + m.Name + "]"));
}
// Combine the lists and find duplicates
var duplicates = _cols.Intersect(_measures).ToList();
// output
var outputMessage = "Number of duplicates found: " + duplicates.Count + "\r\n";
outputMessage += "==========================\r\n";
outputMessage += string.Join(",\r\n", duplicates);
// Output the message
outputMessage.Output();