Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
10 years ago

Dynamic date in web source

I currently have several connections that make calls out to Apigee to grab statistical data and visualize it in power bi.

 

The call uses a date range and has a format that looks like this:

https://api.enterprise.apigee.com...timeRange=01%2F01%2F2016%2000%3A00~04%2F01%2F2016%2024%3A00&timeUnit=day 

 

the format passed in is month, day and year.  Right now i have it hardcoded but i was wondering if there is a way to make the date dynamic so that i could do something like a rolling 6 months rather than having to change the date every time i want to update the data.

 

Is this possible?

8 Replies

  • mike_honey's avatar
    mike_honey
    Icon for Memorable Member rankMemorable Member

    This is Power BI so anything is possible :smileyhappy:

     

    It's a bit hard to read the timeRange value but it looks like 2 dates and times, e.g. Unencoded it would read:

    01/01/2016 00:00~04/01/2016 24:00

     

    You'll need to come up with a source for those 2 values.  Is it data driven? A rule based on today? Converted to UTC or a specific timezone? MM/DD/YYYY format or DD/MM/YYYY format?

     

    Once you have that figured out you can Edit the Query and change the generated URI to an expression that contatenates strings e.g.

     

    "https://api.enterprise.apigee.com...timeRange="&[Start Date Time]&"~"&[End Date Time]&"&timeUnit=day"

     

    How you pass them in depends on the source.  If they are pure expressions then you could just embed them.  If they are specific cells in the result of another Query (my preference, for debugging) then you can use a List function to reference them, e.g.

     

    List.First(#"Table1"[Start Date Time]) 

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks Mike you are correct in the date format. I actually was able to use several functions to get it to work in desktop but that caused another probelm.  So in the advanced editor i changed the source hard coded dates to the following:

       

      timeRange=" & Text.PadStart(Text.From(Date.Month(Date.AddMonths(DateTime.LocalNow(), -6))),2,"0") & "%2F" & Text.PadStart(Text.From(Date.Day(Date.AddMonths(DateTime.LocalNow(), -6))),2,"0") & "%2F" & Text.From(Date.Year(Date.AddMonths(DateTime.LocalNow(), -6))) & "%2000%3A00~" & Text.PadStart(Text.From(Date.Month(DateTime.LocalNow())),2,"0") & "%2F" & Text.PadStart(Text.From(Date.Day(DateTime.LocalNow())),2,"0")

       

      That looks like a hot mess but the short of it is it limits the start date to 6 months back and the end date to today.  It works beautifully in desktop but when I uploaded it to PowerBi to show off the auto refresh, the refresh fails.  This is disappointing as I was hoping to be able to schedule a daily refresh on this.  

       

      Is anyone aware of a work around that I can use to have a dynamic date in the string and have refresh work?

      • curth's avatar
        curth
        Icon for Power BI Team rankPower BI Team

        To make this work, you have to split the query parameters from the base URL by putting them into the options record. Change the formula from something like this

         

        =Web.Contents("https://some/site?parameter1=" & value1 & "&parameter2=" & value2)

         

        To something like this

         

        =Web.Contents("https://some/site", [Query=[parameter1=value1, parameter2=value2]])

         

        value1 and value2 have to be text values and will be properly escaped.