Forum Discussion

mphillenga's avatar
mphillenga
Helper I
1 year ago
Solved

PowerBI data refresh due to dynamic data source. Help with adjusting the M script

Hi,   I have developed a report that fetches data from an API. I have build the URL in such a way that it should grab the newest data, everytime the data is refreshed. When refreshing the data in ...
  • v-veshwara-msft's avatar
    1 year ago

    Hi mphillenga ,

    Thanks for reaching out in Microsoft Fabric Community.
    The error "This dataset includes a dynamic data source..." occurs because the query still generates parts of the Web API request dynamically at runtime, particularly by using DateTime.LocalNow(). Power BI Service requires the URL to be fully evaluable at design time for scheduled refresh to work. Even though the dates are passed as parameters, the use of DateTime.LocalNow() results in dynamic URL generation at runtime, which causes the issue.

    To address this, you can restructure the query to use static parameters, like this:

     

    Create two parameters:

    StartDate (Type: Date, e.g., 2025-01-01)

    EndDate (Type: Date, e.g., 2025-04-28)

     

    Adjust the query like this:

    let
        FixedStartDate = StartDate,
        FixedEndDate = EndDate,
    
        PeriodFromDate = Date.ToText(FixedStartDate, "dd-MM-yyyy"),
        PeriodToDate = Date.ToText(FixedEndDate, "dd-MM-yyyy"),
    
        QueryParams = [
            onlyReportColumns = "no",
            periodFromDate = PeriodFromDate,
            periodToDate = PeriodToDate
        ],
    
        Source = Json.Document(
            Web.Contents(
                "https://organisation.software.com", 
                [
                    RelativePath = "api/v1/reportDirectGet/projectHoursEmpWeek",
                    Query = QueryParams
                ]
            )
        ),
    
        Data = Source[jsondata],
        ConvertToTable = Table.FromList(Data, Record.FieldValues, Record.FieldNames(Data{0})),
        AutoTypeConversion = Table.TransformColumnTypes(ConvertToTable, 
            List.Transform(Table.ColumnNames(ConvertToTable), each {_, type text}), 
            "en-US"
        )
    in
        AutoTypeConversion
    

     

    Some Helpful references:
    Data refresh in Power BI - Power BI | Microsoft Learn

    Chris Webb's BI Blog: Using The RelativePath And Query Options With Web.Contents() In Power Query And Power BI M Code

    Chris Webb's BI Blog: Web.Contents(), M Functions And Dataset Refresh Errors In Power BI

     

    Hope this helps. Please reach out for further assistance.
    If this post helps, then please consider to Accept it as the solution to help the other members find it more quickly and a kudos would be appreciated.

     

    Thank you.