Forum Discussion

ShaelynFrench's avatar
3 years ago

Problem with DateTime.LocalNow() in the service

Hi,

 

I have a date table that uses a fixed date as the start date of the date Table and Date.From(DateTime.LocalNow()) as the end date. So the date table should always be updated to go through today. It works great in Desktop but as soon as I publish the report, it doesn't work in the Service. The end date stays the date that I published the report and isn't updating. Any advice? Is there any way to get it to work as expected? Thanks!

4 Replies

  • edhans's avatar
    edhans
    Community Champion

    You have to refresh the report. DateTime.LocalNow() only changes on a refresh. Think of it like the TODAY() function in Excel. It doesn't change until there is a recalculation necessary. If you just put that in cell A1 of an spreadsheet and leave Excel open, it will never change.


    • ShaelynFrench's avatar
      ShaelynFrench
      Helper I

      edhans oh, yeah, sorry, I've obviously tried refreshing the dataset, refreshing the visual and I tried clearing the browser cache which is the only suggestion I could find online. None of those worked.

      • edhans's avatar
        edhans
        Community Champion

        Does this happen for days? DateTime.LocalNow() reports the server time, not your time, and the server is on UTC. So it will be several hours off and appear not to change. To force it to be your time, try this code, where -8 is my timezone offset, so adjust for your offset.

         

        DateTime.Date(DateTimeZone.SwitchZone(DateTimeZone.LocalNow(),-8))

        I go into more depth on this in this article. And it has worked for years.

        Add a Refresh Time Stamp To Your Power BI Reports — ehansalytics

         

        Otherwise, you'll need to post the code for the query you are using as something else is preventing that code from being read/used.