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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

How to use EPOCH (Unix Time in milliseconds of 13 digit characters) in REST API call with date(s)

Hi all,

 

For the last week, I have been trying to call a rest api with epoch or unix time (milliseconds 13 digits) in my rest api calls but I could not find any that I can reuse or refer to. Hopeful, this post helps who are seeking such solutions.

 

Regards,

Anand

let

//Define the endDate of the query by converting system date time to EPOCH
    UTCDate = Duration.TotalSeconds(DateTimeZone.UtcNow()- #datetimezone(1970,1,1,0,0,0,0,0)),
    ConvertUTCDate = Text.From(UTCDate),
    RemoveDelimiter = Text.BeforeDelimiter(ConvertUTCDate,"."),
    PrefixEpoch = (RemoveDelimiter&"000"),
    EndEPOCH = PrefixEpoch,

//Define the startDate of the query by converting system date time to EPOCH
    
    PreviousUTC = DateTimeZone.From(Number.From(DateTimeZone.UtcNow())),
    PreviousUTCDate = Date.AddDays(PreviousUTC,-X), //replace -x with days e.g., 60 days prior as startdate
    PreviousUTCDateEpoch = Duration.TotalSeconds(PreviousUTCDate - #datetimezone(1970,1,1,0,0,0,0,0)),
    ConvertPreviousUTCDate = Text.From(PreviousUTCDateEpoch),
    RemoveDelimiterPreviousUTCDate = Text.BeforeDelimiter(ConvertPreviousUTCDate,"."),
    PrefixEpochPreviousUTCDate = (RemoveDelimiterPreviousUTCDate&"000"),
    StartEPOCH = PrefixEpochPreviousUTCDate,

//Construct the api call with start and end date from earlier step as text as numeric values are not considered for call by default
    Custom2 = "SearchURL?QueryID=ID&startDate="& StartEPOCH,
    Custom3 = Custom2 & "&endDate=",
    Custom4 = Custom3 & EndEPOCH,
//this is the query that will used to get data using API
    APIcall = Custom4,

    
//App Details
    client_id = #"API Key", //create parameter to define API Key value
    client_secret = #"API Secret", //create parameter to define API secret value
    
//Authentication
    
//URI's
    token_uri = #"Token URL", //create parameter to define Token URL value
    resource =  #"Event URL", //create parameter to define Event URL value
    
    //User Details  
    username = username, //create parameter to define username
    password = password, //create parameter to define password
      
//response token generation with the above steps and parameters

   tokenResponse = Json.Document(Web.Contents(token_uri,          
                                [
                                    Content=
                                    Text.ToBinary(Uri.BuildQueryString(
                                    [
                                        client_id = client_id
                                        ,client_secret=client_secret
                                        ,username=username
                                        ,password=password
                                        ,resource=resource
                                        ,grant_type = "password"
                                        
                                    ]))
                                    ,Headers=
                                        [Accept="application/json"]
                                        , ManualStatusHandling={400}
                                ])),
                                access_token = tokenResponse[access_token],
    token = "Bearer " & tokenResponse[access_token],

//call rest api with the above created parameters i.e., start, end date converted in EPOCH format with REST API for Events

 GetJsonQuery = Json.Document(Web.Contents(Custom4, 
                                                [Headers=
                                                    [Authorization=token]
                                                    ]
                                        )
                            )
in

 GetJsonQuery

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @v-juanli-msft ,

 

Thank you for sharing the article but my message was to communicate how to do when you there is a need to convert system time to EPOCH time format without decimal, 13 digit milliseconds and use it part of the REST API.

 

The query shared by me works for me and it was shared for members seeking solution.

 

Kind regards,

Anand

View solution in original post

3 REPLIES 3
v-juanli-msft
Community Support
Community Support

Hi @Anonymous 

It seems you need to convert datetime format to epoch or unix time(milliseconds), please see methods how to convert datetime to seconds and then use seconds *1000 to get milliseconds.

https://social.technet.microsoft.com/Forums/en-US/64b96320-1872-44d8-931f-68749914bf6c/how-to-convert-datetime-to-unix-time?forum=powerquery

 

Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi @v-juanli-msft ,

 

Thank you for sharing the article but my message was to communicate how to do when you there is a need to convert system time to EPOCH time format without decimal, 13 digit milliseconds and use it part of the REST API.

 

The query shared by me works for me and it was shared for members seeking solution.

 

Kind regards,

Anand

Thanks for sharing the info @Anonymous - I marked your reply as a solution so others will recognized it is solved and has a workable answer.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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