Forum Discussion
How to limit number of rows using M parameters on a web API json data source query?
- 2 years ago
To manage data volume when importing data from an API into Power BI Desktop and then handle a full data load in the Power BI Service, you can use parameters to dynamically adjust the amount of data fetched. Here’s how to set up such a parameter and use it to limit the number of rows pulled during development and then pull in all rows once published to the Power BI Service.
1. Create a Parameter
First, you need to create a parameter in Power BI Desktop that will control the number of rows fetched from the API.- **Go to** Home > Manage Parameters > New Parameter.
- **Name** your parameter (e.g., `MaxRows`).
- **Type**: Choose Integer.
- **Current Value**: Set a default value that your system can handle comfortably, say 100.
- **Minimum**: 0 (or as required).
- **Maximum**: Set according to the expected maximum rows or leave it unbounded.
- **Suggested Values**: Any Value (or as required).2. Modify the API Query to Use the Parameter
Adjust your M query to incorporate this parameter into the API request. APIs vary in how they handle row limits, so you'll need to check if your API supports limiting the number of records returned via URL parameters or request body modifications. Here’s a conceptual example assuming the API allows URL parameter to limit rows:```powerquery
let
MaxRowsParam = Text.From( #"MaxRows" ),
URL = "https://sample.net/api/GetExport?maxRows=" & MaxRowsParam,
Source = Json.Document(Web.Contents(URL)),
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"mxId", "mxName", "mxStatus", "sxId", "wDate", "startDateUtc", "endDateUtc"}, {"mxId", "mxName", "mxStatus", "sxId", "wDate", "startDateUtc", "endDateUtc"})
in
#"Expanded Column1"
```3. Use the Parameter in Power BI Desktop
During development in Power BI Desktop, use the parameter's default setting to fetch only a limited set of data (e.g., 100 rows). This helps to prevent memory issues and speeds up development time.4. Adjust the Parameter After Publishing
Once you publish your report to the Power BI Service:- **Go to** your dataset settings in the Power BI Service.
- **Find the dataset** for your report, and select the Parameters option.
- **Update the `MaxRows` parameter** to a higher value or the total number of records you expect to fetch (you can set it high enough to not limit the rows or adjust according to your maximum capacity).
- **Apply the changes** and refresh your dataset to pull in the complete data set.Notes and Tips
- Always verify if the API you are using supports pagination or row limitations through parameters, as this approach depends on such functionality.
- Consider implementing error handling in your M query to manage cases where the API might return errors due to too many requests or data limits.
- Use pagination if the API supports it, to fetch data in chunks rather than all at once, which can also help avoid memory issues in Power BI Desktop.This method not only allows you to control the data volume during development but also leverages Power BI's capability to handle larger datasets in the service environment where more resources may be available.
Does your API have an appropriate record limiter parameter?
It has a parameter you can use in the query URL to limit it, but Power BI doesn't let you change the source URL once its published to the PowerBI service.
Is there a way I can create a parameter to change the URL?
Can I put a PowerBI M parameter inside Web.Contents()?
let
Source = Json.Document(Web.Contents("https://sample.net/api/GetExport?startDateUTC=10/01/2023")),
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"mxId", "mxName", "mxStatus", "sxId", "wDate", "startDateUtc", "endDateUtc"}, {"mxId", "mxName", "mxStatus", "sxId", "wDate", "startDateUtc", "endDateUtc"}),