Search code examples
c#powerbissas-tabulartabular-object-model

Convert Power BI File to json


I am wanting to convert a Power BI File I have to a json file. The goal is to have a template that I can then take and programmatically modify (add tables/measures) and then upload the new file to client workspaces as needed. This would save us from having to manually edit each power bi file as we get a new client.

I know I can use TOM (Tabular Object Model) to edit power bi models via programs such as Visual Studio and Tabular Editor but was considering a json file may be easier to manipulate.

I attempted the below code in Visual Studios. I got an: does not contain a definition "toJson" error when I tried building this. Haven't found how to add ToJson yet. I am rather new to c# so this could be a simple ask.


namespace ConvertPowerBIFileToTMSL
{
    class Program
    {
        static void Main(string[] args)
        {
            string pbixFilePath = @"C:\Users\USER\Downloads\MyDashboard.pbix";
            string tmslFilePath = @"C:\Users\USER\Downloads\MyTMSLFile.json";

            using (var server = new Server())
            {
                server.Connect("localhost");

                var database = server.Databases.GetByName("MyDatabase");
                var model = database.Model;

                var tmsl = model.ToJson(pbixFilePath);
                File.WriteAllText(tmslFilePath, tmsl);
            }
        }
    }
}

Solution

  • Don't recreate the wheel. There are lots of tools now to help with this. I think the one you need is pbi-tools, which is a command line tool to go from .pbix to TMSL and back again.

    Start here: https://pbi.tools/cli/
    Lots of video links from here: https://pbi.tools/resources/

    Though you might just as well save as .pbip direct from Power BI. See https://learn.microsoft.com/en-us/power-bi/developer/projects/projects-overview