Forum Discussion
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
- edhansCommunity 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.
- ShaelynFrenchHelper 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.
- edhansCommunity 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.