Scripting Magic with C# in Tabular Editor and Semantic Model for Power BI

Scripting Magic with C# in Tabular Editor and Semantic Model for Power BI

I don't think I need to introduce Tabular Editor - but I would like to showcase specific and concrete examples on how C# Scripts can "pimp my model" as ??Alexander Korn and Data Goblins provide initial setup but also assistance when maintaining and developing the semantic model ??

Beware, that creating tables and columns is unsupported when using Tabular Editor as external tool in Power BI Desktop. You need to develop the model from scratch inside Tabular Editor.

Make sure you know how to do it manually and the purpose behind, before you automate the entire process - that's my personal opinion.

For each new project or semantic model, I see recurring requests that we need somewhat of the following for initial setup and for each deployment. What if we could use a standard .PBIT or C# script to help us and make it consistent across the organisation on how we create the inital tables and naming convention ??


Initial Setup:

  • Create Measure table
  • Create display folder for global measures and for report level measures
  • Display folders for columns in tables e.g. keys and attributes and base columns
  • Create SUM measures and hide columns
  • Create date table
  • Last Refresh Table
  • Create time intelligence calculation group
  • All Key and ID columns: Set Summarize By to "None"
  • And more...


For each deployment:

  • Table Groups for documentation
  • Format the DAX
  • CamelCase to Proper Case


From tables into table groups:

Table groups, which I'm particular excited about. However it unfortunately don't propogate to Power BI Desktop, but it creates dynamically groups for the table kind.

  • Calculation Groups
  • Measure Groups
  • Facts
  • Parameter Tables
  • Dimensions
  • Misc (other)

//Create Table Groups
// Loop through all tables:
foreach(var table in Model.Tables)
{
    if (table is CalculationGroupTable)
    {
        table.TableGroup = "Calculation Groups";
    }
    else if (!table.UsedInRelationships.Any() && table.Measures.Any(m => m.IsVisible))
    {
        // Tables containing visible measures, but no relationships to other tables
        table.TableGroup = "Measure Groups";
    }
    else if (table.UsedInRelationships.All(r => r.FromTable == table) && table.UsedInRelationships.Any())
    {
        // Tables exclusively on the "many" side of relationships:
        table.TableGroup = "Facts";
    }
    else if (!table.UsedInRelationships.Any() && table is CalculatedTable && !table.Measures.Any())
    {
        // Tables without any relationships, that are Calculated Tables and do not have measures:
        table.TableGroup = "Parameter Tables";
    }
    else if (table.UsedInRelationships.Any(r => r.ToTable == table))
    {
        // Tables on the "one" side of relationships:
        table.TableGroup = "Dimensions";
    }
    else
    {
        // All other tables:
        table.TableGroup = "Misc";
    }
}        

Resources for all the C# Scripts and next step:

https://docs.tabulareditor.com/common/CSharpScripts/csharp-script-library-beginner.html

https://github.com/KornAlexander/PBI-Tools/blob/main/Power%20BI-Pimp-Script.csx

https://github.com/KornAlexander/PBI-Tools/blob/main/Data%20Model%20Toolbox/MacroActions.json

https://github.com/m-kovalsky/Tabular

https://github.com/data-goblin/powerbi-macguyver-toolbox/blob/main/tabular-editor-scripts/csharp-scripts/import-model-only/create-initial-tables.csx

https://tabulareditor.com/learn


Enjoy ??


Thank you for reading my article, hope you enjoyed it. Comment or provide some input is much appreciated.

?? Hi my name is David Kofod Hanna, Senior Advisor and Consultant at twoday and my aim is to guide self-service Power BI developers for more enterprise managable concepts in a consumable and practical way - because I have been on that journey for years, and still is...

??Alexander Korn

Senior BI Consultant @ HICO | Ex-Microsoft | ActionableReporting.com

3 个月

Great article and thanks for the mention. Totally agree, grouping tables is an underrated feature. Glad we have it with TE3. I found an idea related to this. Tabular Editor let's keep up the good work pushing Power BI to improve. Maybe just like it was with Calc Groups, always one step ahead? ?? https://ideas.fabric.microsoft.com/ideas/idea/?ideaid=9267b1d4-cf7d-4c67-8c2f-54b5e3544457

要查看或添加评论,请登录

David Kofod Hanna的更多文章

社区洞察

其他会员也浏览了