Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Data source and timestamp

Hi Everyone,   I have been trying to pull some data with an API i have. In this API i use a timestamp to pull the previous months data but when i use this it actually pulls back the previous month,...
  • dharmendars007's avatar
    1 year ago

    Hello Anonymous , 

     

    1. Instead of calculating the start of the current month, I’ve used Date.EndOfMonth() to get the end of the last day of the previous month.

    2. Since timestamps are often based on seconds, I’ve added 86399 (which is the total number of seconds in a day minus one second)

    3.Ensure that the API is expecting timestamps in UTC. You may need to convert DateTime.LocalNow() to DateTime.UTCNow() if necessary.

     

    let
    CurrentDate = Date.From(DateTime.LocalNow()),
    StartOfLastMonth = Date.StartOfMonth(Date.AddMonths(CurrentDate, -1)),
    EndOfLastMonth = Date.EndOfMonth(Date.AddMonths(CurrentDate, -1)), // Corrected end of last month

    StartTimestamp = Number.ToText(Duration.TotalSeconds(StartOfLastMonth - #date(1970, 1, 1))),
    EndTimestamp = Number.ToText(Duration.TotalSeconds(EndOfLastMonth - #date(1970, 1, 1)) + 86399), // End timestamp at 23:59:59

    Url = "https://xxxxx/api/v1/xxx/xxx/xxx/xxxxx/xxxxxxxxxxxx?start=" & StartTimestamp & "&end=" & EndTimestamp,

     

    If you find this helpful , please mark it as solution which will be helpful for others and Your Kudos/Likes are much appreciated!

     

    Thank You

    Dharmendar S

    LinkedIN