Forum Discussion
Environment Switching in Power BI Custom Connector Using M language(Staging <-> Production)
- 1 year ago
Hi Ravindra_,
Thank you for reaching out to the Microsoft Fabric Forum Community.For your scenario implementing environment switching in a Power BI Custom Connector the recommended best practice is to use user-defined parameters via the connector UI. This approach ensures:
- Compatibility with both Power BI Desktop and Power BI Service
- Compliance with security and sandbox restrictions
- Flexibility to scale across different environments without hardcoding or relying on local files
If this information is helpful, please “Accept it as a solution” and give a "kudos" to assist other community members in resolving similar issues more efficiently.
Thank you.
Hi Ravindra,
It is possible to implement a mechanism to switch between Staging and Production environments dynamically while developing a Power BI Custom Connector. You can achieve this using M language. Here are two suggestions:
Using System Environment Variable:
- Power BI Custom Connector can read system environment variables. You can use these variables to determine which environment the connector is running in.
- For example, you can create an environment variable named Environment. This variable can have values like Staging or Production.
- In M language, you can read this variable and use the appropriate connection strings.
Using Local Configuration File:
- The connector can read a local configuration file (e.g., .txt or .json) where you store environment information.
- By reading the file content using M language, you can determine which environment the connector is running in.
- For example, you can have a config.json file with the following structure
{
"environment": "Staging"
}
- In M language, you can read this file and use the appropriate connection strings.
Example M Code:
let
// Reading environment variable
environment = Environment.GetVariable("Environment"),
// Reading configuration file
config = Json.Document(File.Contents("config.json")),
environment = config[environment],
// Connection strings based on environment
source = if environment = "Staging" then
"https://staging.api.example.com/data"
else
"https://production.api.example.com/data"
in
sourceWith these methods, you can dynamically determine which environment your Power BI Custom Connector is running in and use the appropriate connection strings. I hope this helps!
Thanks
translation and formatting supported by AI
- shripathikamath1 year agoRegular Visitor
I have a question related to the second approach (using a local configuration file.) Are there special permissions I need to set? Because I get a security error when trying to read a file placed in the C:\documents\Public folder.