Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
DryMouse555
Frequent Visitor

Add dynamic dates in API Source url

Hi,

 

I have connected to a online system using API url and Auth key. Below is my code. This I enter in the blank query as source step.

= Json.Document(Web.Contents("https://xyz.com.au/api/",
[Query=[
dbtable="statementitems",
fundingtype= "2",
DateStart = "Date.IsInPreviousMonth",
DateEnd = "Date.IsInCurrentDay",
Authkey = ApiKey,
includedeleted = "yes",
toprestrict = "1000000",
source= "powerBI"
]]
))


I want to add in DateStart where whenever it refreshed, it pulls the last 3 months data and DateEnd to Current day.
Can you tell me how to do that? Thanks in advance.

 

5 REPLIES 5
DryMouse555
Frequent Visitor

Hi, I am still getting these errors. I have added the code above.


DryMouse555_0-1719877205464.png

DryMouse555_1-1719877229059.png

 

 

Also, before I put static date values, I used this code. I was told to use this date format to get the output

= Json.Document(Web.Contents("https://xyz.com.au/api/",
[Query=[
dbtable="statementitems",
fundingtype= "2",
DateStart = "01/Jun/2024",
Date.End = "29/Jun/2024",
Authkey = ApiKey,
includedeleted = "yes",
toprestrict = "1000000",
source= "powerBI"
]]
))

 

DryMouse555
Frequent Visitor

Hi,

 

Sorry for the late reply.

My input: 

let
EndDate= DateTime.LocalNow(),
StartDate = Date.AddMonths(DateTime.Date(EndDate), -3),
DateStart = DateTime.ToText(StartDate, "dd/mm/yyyy"),
DateEnd = DateTime.ToText(Today, "dd/mm/yyyy"),
apiUrl = "https://xyz.com.au/api/",
Source = Json.Document(Web.Contents(apiUrl,
[
Query = [
dbtable = "statementitems",
fundingtype = "2",
DateStart = DateStart,
DateEnd = DateEnd,
Authkey = ApiKey,
includedeleted = "yes",
toprestrict = "1000000",
source = "powerBI"
]
]
))
in
Source

 

I am getting this message 

DryMouse555_0-1719801271816.png

 

Anonymous
Not applicable

Hi @DryMouse555 ,

Base on the error message in the screenshot, it seems that there’s an issue with the date format or conversion. Please update the codes as below and check if it can return the expected data...

let
    EndDate= DateTime.LocalNow(),
    StartDate = Date.AddMonths(DateTime.Date(EndDate), -3),
    DateStart = Date.ToText(StartDate, "yyyy-MM-dd"),
    DateEnd = Date.ToText(DateTime.Date(EndDate), "yyyy-MM-dd"),
    apiUrl = "https://xyz.com.au/api/",
    Source = Json.Document(Web.Contents(apiUrl,
    [
        Query = [
            dbtable = "statementitems",
            fundingtype = "2",
            DateStart = DateStart,
            DateEnd = DateEnd,
            Authkey = ApiKey,
            includedeleted = "yes",
            toprestrict = "1000000",
            source = "powerBI"
        ]
    ]
    ))
in
    Source

Best Regards

Anonymous
Not applicable

Hi @DryMouse555 ,

To dynamically set the 'DateStart' to pull data from the last 3 months and 'DateEnd' to the current day in your API call, please modify the codes as below and check if it can work:

Date.AddMonths - PowerQuery M | Microsoft Learn

 

let
    EndDate= DateTime.LocalNow(),
    StartDate = Date.AddMonths(DateTime.Date(EndDate), -3),
    DateStart = DateTime.ToText(StartDate, "yyyy-MM-dd"),
    DateEnd = DateTime.ToText(Today, "yyyy-MM-dd"),
    apiUrl = "https://xyz.com.au/api/",
    Source = Json.Document(Web.Contents(apiUrl,
        [
            Query = [
                dbtable = "statementitems",
                fundingtype = "2",
                DateStart = DateStart,
                DateEnd = DateEnd,
                Authkey = ApiKey,
                includedeleted = "yes",
                toprestrict = "1000000",
                source = "powerBI"
            ]
        ]
    ))
in
    Source

 

Best Regards

Hi, I am still getting these errors. I have added the code above.


DryMouse555_0-1719899050576.png

 

DryMouse555_1-1719899050573.png

 

 

 

Also, before I put static date values, I used this code. I was told to use this date format to get the output

= Json.Document(Web.Contents("https://xyz.com.au/api/",
[Query=[
dbtable="statementitems",
fundingtype= "2",
DateStart = "01/Jun/2024",
Date.End = "29/Jun/2024",
Authkey = ApiKey,
includedeleted = "yes",
toprestrict = "1000000",
source= "powerBI"
]]
))

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.