Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Aaron_C
Frequent Visitor

Tabular Editor 2, C# Script - Movng created measures to a different table

Hello all,

Trying to accomplish what in theory seems like a simple mechanic, but I have not found a way to accomplish this in any of the resources for generates DAX measures via C# Scripts in Tabular Editor!

When generating a basic measure, say SUM of Sales Amount the C# Script is: (thank you @dotykier )

 

/*
 * Title: Auto-generate SUM measures from columns
 * 
 * Author: Daniel Otykier, twitter.com/DOtykier
 * 
 * This script, when executed, will loop through the currently selected columns,
 * creating one SUM measure for each column and also hiding the column itself.
 */
 
// Loop through all currently selected columns:
foreach(var c in Selected.Columns)
{
    var newMeasure = c.Table.AddMeasure(
        "Sum of " + c.Name,                    // Name
        "SUM(" + c.DaxObjectFullName + ")",    // DAX expression
        c.DisplayFolder                        // Display Folder
    );
    
    // Set the format string on the new measure:
    newMeasure.FormatString = "0.00";

    // Provide some documentation:
    newMeasure.Description = "This measure is the sum of column " + c.DaxObjectFullName;

    // Hide the base column:
    c.IsHidden = true;
}


However, this code generates the measure within the Table of the selected column, i.e. the "Sales" table.

I would like to generate the Measure in a seperate table called "Calculations", or move it to a different table after it has been generated but I cannot crack how to achieve this.

Any help would be greatly appreciated.

Thanks.
Aaron

1 ACCEPTED SOLUTION
mariussve1
Super User
Super User

Hi,

Would this do the trick?

 

var measureTable = Model.Tables["Measure Table"]; // Replace "Measure Table" with your desired table name

foreach(var c in Selected.Columns)
{
    var newMeasure = measureTable.AddMeasure(
        "Sum of " + c.Name,                    // Name
        "SUM(" + c.DaxObjectFullName + ")",    // DAX expression
        c.DisplayFolder                        // Display Folder
    );
    
    // Set the format string on the new measure:
    newMeasure.FormatString = "0.00";

    // Provide some documentation:
    newMeasure.Description = "This measure is the sum of column " + c.DaxObjectFullName;

    // Optionally, hide the base column:
    c.IsHidden = true;
}

 


Br
Marius
BI Fabrikken
www.bifabrikken.no

View solution in original post

2 REPLIES 2
mariussve1
Super User
Super User

Hi,

Would this do the trick?

 

var measureTable = Model.Tables["Measure Table"]; // Replace "Measure Table" with your desired table name

foreach(var c in Selected.Columns)
{
    var newMeasure = measureTable.AddMeasure(
        "Sum of " + c.Name,                    // Name
        "SUM(" + c.DaxObjectFullName + ")",    // DAX expression
        c.DisplayFolder                        // Display Folder
    );
    
    // Set the format string on the new measure:
    newMeasure.FormatString = "0.00";

    // Provide some documentation:
    newMeasure.Description = "This measure is the sum of column " + c.DaxObjectFullName;

    // Optionally, hide the base column:
    c.IsHidden = true;
}

 


Br
Marius
BI Fabrikken
www.bifabrikken.no

You godsend! 

That's worth writing a small blog on now 🙂 

Thank you!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.