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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
LiliLPo
Frequent Visitor

API as data source with dynamic date

Hi, 

 

I want to get data from an API. 

I need the data to be pulled each month for the previous month only, I dont need to append the data only monthly data. 

how do i write it so it will be equivalent 

to the following query in sql:

where datepart(month,UpdatedAt) = datepart(month,dateadd(month, -1,getdate()))

capture.png

 

Here is my API call:

= (PageNumber) =>

let
token_body = [
client_id = tokenClientId,
client_secret = tokenClientSecret,
grant_type = "client_credentials",
scope = tokenClientScope
],
tokenResponse = Json.Document(
Web.Contents(
tokenAddress,
[
Headers = [
ContentType ="application/x-www-form-urlencoded",
Accept = "application/json"
],
Content = Text.ToBinary(Uri.BuildQueryString(token_body))
]
)
),
Source = Json.Document(
Web.Contents("https://XXXXXXXXXXX.net/api/XXXXX/Get?receivedFromDateTime=2023-02-01&receivedToDateTime=2024-02-28&..."&Number.ToText(PageNumber),
[
Headers = [
Accept="application/json",
Authorization = "Bearer " & tokenResponse[access_token]
]
]
)
)
in
Source

 

thank you 

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @LiliLPo 

 

You can use this code to create the Start and End dates for the previous month

 

 

    Now = Date.AddMonths(Date.From(DateTime.LocalNow()), -1)
    SOM = Date.ToText(Date.StartOfMonth(Now), [Format="yyyy-MM-dd"]),
    EOM = Date.ToText(Date.EndOfMonth(Now), [Format="yyyy-MM-dd"])

 

 

Then use these in your URL like this

 

Web.Contents("https://XXXXXXXXXXX.net/api/XXXXX/Get?receivedFromDateTime="&SOM&"&receivedToDateTime="&EOM&"&......."

 

Regards

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

2 REPLIES 2
LiliLPo
Frequent Visitor

THANK YOU SO MUCH 😀 it workedI

PhilipTreacy
Super User
Super User

Hi @LiliLPo 

 

You can use this code to create the Start and End dates for the previous month

 

 

    Now = Date.AddMonths(Date.From(DateTime.LocalNow()), -1)
    SOM = Date.ToText(Date.StartOfMonth(Now), [Format="yyyy-MM-dd"]),
    EOM = Date.ToText(Date.EndOfMonth(Now), [Format="yyyy-MM-dd"])

 

 

Then use these in your URL like this

 

Web.Contents("https://XXXXXXXXXXX.net/api/XXXXX/Get?receivedFromDateTime="&SOM&"&receivedToDateTime="&EOM&"&......."

 

Regards

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.