Forum Discussion

arc_gts's avatar
arc_gts
Frequent Visitor
7 months ago
Solved

Using a C# script in Tabular Editor 2 to update M code - error message

I'm using the AdventureWorks2025 DB in a locally installed SQL Server instance. Original M code is below:   let Source = Sql.Database("localhost\SQLEXPRESS", "AdventureWorks2025", [Query="select *...
  • v-pnaroju-msft's avatar
    7 months ago

    Hi arc_gts,

    Thank you for contacting the Microsoft Fabric Community Forum.

    Based on my understanding, the issue is unlikely to be caused by Tabular Editor, C# scripts, or SQL connectivity. The save failure occurs because invalid Power Query (M) syntax is introduced during the TSV export/edit/import process, and Power BI validates M code only at save/deploy time using the Mashup engine. Specifically, manually replacing line breaks or tabs with #(lf) / #(tab), embedding multi-line SQL inside Query="...", or introducing encoding/escaping changes while editing the TSV can produce invalid serialized M code and trigger the “Microsoft.Data.Mashup.Preview;Token identifier expected” error.

    The safest approach is to avoid embedding raw SQL text and instead rebuild the M expression using native Power Query navigation, for example, Sql.Database followed by schema or table navigation. This eliminates string escaping issues and is fully validated and supported by the Mashup engine:

    let
    Source = Sql.Database("localhost\SQLEXPRESS", "AdventureWorks2025"),
    Product = Source{[Schema="Production", Item="Product"]}[Data]
    in
    Product

    If embedding SQL text is unavoidable, ensure it remains single line, avoid #(lf) / #(tab) inside strings, preserve actual line breaks in the M script, and validate each updated query first in Power BI Desktop via Transform data → Advanced Editor before applying bulk changes. Once the M syntax is valid there, saving from Tabular Editor should succeed.

    Please refer to the links below for further information:
    Power Query M language specification - PowerQuery M | Microsoft Learn
    Sql.Database - PowerQuery M | Microsoft Learn
    External Tools in Power BI Desktop - Power BI | Microsoft Learn
    Tabular modeling overview - Analysis Services | Microsoft Learn

    We hope the above guidance helps resolve the issue. If you have any further queries, please feel free to contact the Microsoft Fabric community.

    Thank you.