To use the Power Query SDK, you must install Visual Studio with the Power Query SDK extension to create, debug, and package custom M formula language connectors. This SDK allows you to build specialized data connectors that enable Power BI, Excel, and Analysis Services to communicate with unique web APIs or proprietary data sources that are not included in the standard library of connectors.
What You'll Need
- A Windows-based computer with Visual Studio installed (Community, Professional, or Enterprise versions are all compatible).
- The Power Query SDK extension installed within Visual Studio.
- Basic knowledge of the M formula language.
- A web API or data source with documentation (specifically endpoints, authentication methods, and response formats).
- An internet connection to download necessary dependencies and SDK components.
Step-by-Step
- Install Visual Studio and the SDK: Start by downloading and installing Visual Studio from the official Microsoft website. Once the installation is complete, open Visual Studio and navigate to the Extensions menu > Manage Extensions. In the search bar, type "Power Query SDK". Locate the official extension and click Download. You will need to close Visual Studio to allow the installer to run and complete the integration.
- Create a New Project: Open Visual Studio and select "Create a new project". In the search box, type "Power Query Connector". Select the template labeled "Data Connector Project" and click Next. Give your project a meaningful name that corresponds to the data source you are connecting to (for example, "MyCustomAPI.pq"). Choose a location on your local drive and click Create.
- Understand the Project Structure: Once the project loads, you will see several files in the Solution Explorer. The most important file is the .pq file, which is where you will write your M code. You may also see a .query.pq file, which is used for testing your connector logic without needing to launch the full Power BI Desktop application.
- Define the Data Source Credentials: Before writing the logic, you must define how the user will authenticate. In your .pq file, you will use the
DataSource.KindandDataSource.Implementationfunctions. You need to specify if the connector uses Anonymous, Windows, Basic, Web API, or OAuth authentication. For a standard Web API, you will likely implement a function that handles the header requirements, such as anAuthorizationheader containing a Bearer token. - Write the Core Connector Logic: Begin writing your M code. You will typically define a function that takes a URL or a set of parameters as input. Use
Web.Contentsto fetch data from your source. A standard pattern involves creating a function that calls the API, parses the JSON response usingJson.Document, and then converts that data into a table format usingTable.FromRecords. Ensure you handle pagination if your API returns data in multiple chunks; this usually requires a recursive function or aList.Generateloop to fetch subsequent pages based on a token or offset provided in the response. - Implement Navigation and Schema: To make your connector user-friendly, you should define a navigation step. This allows users to see a list of available tables or datasets within the data source. Use
NavigationTable.FromColumnsto create a table that lists the available entities, their descriptions, and their data types. This ensures that when a user clicks on your connector in Power BI, they see a clear hierarchy of data rather than a single, overwhelming blob of information. - Test the Connector with a Query File: Instead of constantly opening Power BI, use the built-in testing capability. Right-click your project in Solution Explorer and select "Add New Item" > "Power Query Query". This creates a .query.pq file. In this file, you can write a simple line of code like
let Source = MyConnectorFunction([ApiKey="12345"]) in Source. Press F5 to run the query. The results will appear in a data grid within Visual Studio, allowing you to verify that your parsing logic is correct and your schema is accurate. - Build and Package the Connector: Once your tests pass, you need to compile the project. Right-click the project in the Solution Explorer and select "Build". If there are no errors, Visual Studio will generate a .mez file (or sometimes a .pq file depending on the version) in the bin/Debug or bin/Release folder. This .mez file is your compiled connector.
- Deploy the Connector: To use the connector in Power BI Desktop, you must move the .mez file to the specific Power Query folder on your local machine. The path is typically
Documents\Power BI Desktop\Custom Connectors. If the "Custom Connectors" folder does not exist, you must create it manually. Once the file is moved, restart Power BI Desktop, go to Get Data > More..., and search for your connector name.
Common Mistakes
One of the most frequent errors is failing to handle sensitive information correctly. Never hard-code API keys or passwords directly into the .pq file. Instead, always use the authentication mechanisms provided by the SDK so that credentials are encrypted and stored securely by the host application (Excel or Power BI). Another common mistake is ignoring the privacy levels of data. If your connector combines data from a local file with data from a web API, Power BI may trigger a privacy level error. You must ensure your code is written to be compatible with the Privacy Levels settings in the Power BI Options menu.
Developers often forget to implement error handling. If an API returns a 404 (Not Found) or a 429 (Too Many Requests), a poorly written connector will simply crash or return a generic error. Use try...otherwise blocks in your M code to catch these errors and provide a meaningful message to the end user, such as "The API limit has been reached. Please try again later." Additionally, failing to define specific data types (like specifying a column is a 'number' rather than 'any') can cause downstream issues in Power BI reports where calculations fail because they encounter unexpected text values.
Tips
To make your connector performant, always attempt to perform as much filtering as possible on the server side. This is known as "Query Folding." If your API supports OData or specific URL parameters for filtering, pass those parameters directly in the Web.Contents call rather than downloading the entire dataset and filtering it within M. This significantly reduces memory usage and speeds up refresh times. Use the Value.Metadata function to attach extra information to your data, which can help users understand the context of the data they are viewing.
When debugging complex M logic, use the "Step Into" feature in the Visual Studio debugger. This allows you to watch how the data transforms at every single line of your code. It is much easier to find where a column name changes or a value becomes null if you can see the state of the table at every step of the transformation process.
Troubleshooting
If your connector does not appear in the list of available data sources in Power BI, first check that the .mez file is in the correct folder: Documents\Power BI Desktop\Custom Connectors. Ensure that you have restarted Power BI Desktop after moving the file. If it still does not appear, check the Power BI settings under Options and Settings > Options > Security and ensure that "Data Extensions" is set to allow custom connectors.
If you receive a "Formula.Fire" error during execution, this usually indicates a syntax error in your M code or a runtime error from the API. Check the Visual Studio output window for specific line numbers. If the error is related to authentication, verify that your API credentials are correct and that the authentication method you defined in the SDK matches the requirements of the web service. If the error is related to data types, use the Type.Table function to explicitly define your schema and prevent the engine from guessing incorrectly.
Frequently Asked Questions
Can I use the Power Query SDK with Excel?
Yes, once you have built and deployed your .mez file to the Custom Connectors folder, it will be available in Excel under the Data > Get Data > From Power Query menu, provided your version of Excel supports custom connectors.
Do I need a premium license to use custom connectors?
You can develop and use custom connectors in Power BI Desktop without a premium license. However, if you want to use these connectors in the Power BI Service (the cloud version), you will need a Power BI Premium capacity or Premium Per User license to enable the gateway to use custom connectors.
Is the M language the same as Power Query in the UI?
Yes, the M language is the underlying engine for everything you do in the Power Query editor. The UI simply generates M code for you. When using the SDK, you are writing this code manually to gain more control and create reusable logic.
How do I handle OAuth2 authentication?
OAuth2 is more complex than Basic authentication. You must implement a function that handles the token request, the redirection URI, and the storage of the refresh token. The SDK provides specific structures to help manage these authentication flows securely.
Can I distribute my connector to other users?
Yes, you can share the .mez file with colleagues. They will need to place the file in their own local Custom Connectors folder. For enterprise-wide distribution, many organizations use a centralized installation script or a managed deployment process to ensure all users have the necessary connectors installed.