Forum Discussion

test2sa's avatar
test2sa
Icon for Helper I rankHelper I
2 years ago
Solved

How to limit number of rows using M parameters on a web API json data source query?

All of the examples I see are how to limit the amount of rows pulled into PowerBI Desktop are for SQL sources, not API sources. Example: https://www.youtube.com/watch?v=_zYvybVMk7k&ab_channel=GuyinaCube

 

I need to setup a parameter to only pull in a small amount of rows into the Desktop app and then edit the parameter on the PowerBI service to pull in all rows after publishing the report. But I'm using an API source, not an SQL source. How do I limit the number of rows it pulls so that my Desktop app doesn't crash from running out of memory?

 

let
Source = Json.Document(Web.Contents("https://sample.net/api/GetExport")),
#"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"}),

 

  • test2sa 

    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.

8 Replies

  • test2sa 

    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.

    • test2sa's avatar
      test2sa
      Icon for Helper I rankHelper I

      I was really hopeful this would work, but I'm running into a snag when I upload it to the PowerBI service.

       

      This dataset includes a dynamic data source. Since dynamic data sources aren't refreshed in the Power BI service, this dataset won't be refreshed. Learn more: https://aka.ms/dynamic-data-sources.

      I see the parameter in the PowerBI service, but it won't let me set up auto-refresh because I'm using a parameter in Web.Contents() ?? Currently trying to figure out why it doesn't like parameters in Web.Contents. Any ideas?

       

      MaxRows = "04/01/2024" (text type parameter) since that is what limits the API rows pulled.

       

      The code I'm using:

       

      let
      MaxRowsParam = Text.From(MaxRows),
      URL = "https://sample.net/api/GetExport?startDateUTC=" & MaxRowsParam,
      Source = Json.Document(Web.Contents(URL)),
      #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),

  • Does your API have an appropriate record limiter parameter?

    • test2sa's avatar
      test2sa
      Icon for Helper I rankHelper I

      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"}),

  • I have the same question but my data source is a huge local Excel file. The parameter limits the data load locally but when I publish, only the loaded data seems to get published. As a result, it makes no difference when I update the parameter in the PowerBI Service.

     

    Short of moving the file to Sharepoint, is there a solution?

    • lbendlin's avatar
      lbendlin
      Icon for Super User rankSuper User

      that is a very different scenario.  Please open a new thread.