How to edit Power BI Desktop document parameters or data sources programmatically with C#?
Asked Answered
B

1

4

I have a report template which is built in Power BI Desktop and is saved as .pbix or .pbit file. This template uses DirectQuery SQL database as data source, whereas server address and database name are extracted to parameters. There is also a parameter which holds a ReportId, which is used in queries.

Power BI window screenshot

I also have a C# application, which is supposed to generate Power BI Desktop reports from this template.
How can create a report with specific parameters programmatically?
Something like Power BI Desktop does itself when you open .pbit template file, enter parameters and it generates .pbix document.

I didn't find any SDK \ Interop libraries for Power BI Desktop.
I have tried to open a document as ZIP, but DataModel file is archived, and therefore not a well-formed XML.
I have also tried to extract this parameters to JSON file and load it as a data source, but relative paths are not supported by Power BI.

Bentwood answered 21/1, 2019 at 17:40 Comment(0)
H
5

There is no SDK or API you can use to do that. If you publish this report to Power BI Online, you can use Update Parameters and Update Parameters In Group REST API calls to change the parameter values. I believe Power BI Reporting Server has similar API, but I do not have experience with it.

You could try to publish the report online and keep it there (it is not needed to share it with anyone). When you need a report with some parameter value, call the API methods above to change the value, and then download the modified .pbix file using Export Report or Export Report In Group methods. It's ugly and not thread safe, but may work...

You said you looked at DataModel file in the extracted .pbix. I think the parameters and M queries are in DataMashup (search for RootFormulaText or ReferencedQueriesFormulaText text to find them). The server and database names are also there. You can relatively easy modify the M query, the value of existing parameter or server/database name, and try to re-pack the .pbix file. It may work, but looks extremely unsupported.

I tried to connect with SSMS to the report's SSAS in Power BI Desktop, but I didn't find a way to modify parameter value this way. I can see their values in the source queries, though (if the parameter is used as a filter).

I'm using something similar, but instead of parameters, I'm embedding the report in my application and use filters. This way all users access one and the same report and I do not need to "tailor" the .pbix for their specific needs.

I'm afraid this isn't an answer that will solve your problem, but I hope it will give you some ideas what you can or can't do!

Hobart answered 23/1, 2019 at 7:33 Comment(1)
Thank you for your reply, this is very useful and detailed! With regard to filters, I can try to extract ReportId to a report-level filter, but I also have database address and name. Power BI Online sounds like the best option here, I think I will look into this.Bentwood

© 2022 - 2024 — McMap. All rights reserved.