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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
pmscorca
Post Prodigy
Post Prodigy

DateTime.LocalNow doesn't return the right date and time for the local zone

Hi,

I'm trying to use the DateTime.LocalNow function to add an actual date at my data, but it returns a date and time different respect to the West Europe Standard Time. The datetime value returned is 2 hours backwords.

How could I adjust the actual date respect to the West Europe Standard Time? Thanks

5 REPLIES 5
collinsg
Super User
Super User

Good day pmscorca,

One thing to watch for is that DateTime.LocalNow() in the Power BI Service is the local time of the Service. For example I am in the UK but my Power BI Service is hosted in a different time zone. A workaround I have used is to call out to an API, e.g. https://worldtimeapi.org/ to get the local time for a zone of my choice.

 

I wrote a function to do this. You can copy and paste it into a blank query. The timezones are listed https://worldtimeapi.org/timezones. Hope this helps.

 

let
//Function definition
fn = (area as text, location as text, optional region as text) as datetime =>
let
reg = if region <> null then region else "",
dt = DateTimeZone.RemoveZone(
DateTimeZone.From(
Json.Document(
Web.Contents("https://worldtimeapi.org/api/timezone/" & area & "/" & location & "/" & reg)
)[datetime]
)
)
in
dt,
//Function type definition
fnType = type function (
area as (
type text meta [
Documentation.FieldCaption = "area",
Documentation.FieldDescription = "www.worldtimeapi.org area",
Documentation.SampleValues = {"europe"},
//Documentation.AllowedValues = {},
Documentation.IsMultiline = false,
Documentation.IsCode = false
]
),
location as (
type text meta [
Documentation.FieldCaption = "location",
Documentation.FieldDescription = "www.worldtimeapi.org location",
Documentation.SampleValues = {"london"},
//Documentation.AllowedValues = {},
Documentation.IsMultiline = false,
Documentation.IsCode = false
]
),
optional region as (
type text meta [
Documentation.FieldCaption = "region",
Documentation.FieldDescription = "www.worldtimeapi.org region",
Documentation.SampleValues = {" "},
//Documentation.AllowedValues = {},
Documentation.IsMultiline = false,
Documentation.IsCode = false
]
)
) as datetime meta [
Documentation.Name = "DateTimeSafeLocalNow",
Documentation.LongDescription = "
Purpose<br>
Consider you have a table of time-series data, collected in your timezone.
You want to calculate the age of each reading and so you try a calculation,
Age = DateTime.LocalNow() - Readings[Datetime]
All works well. Then you publish to the Power BI Service, in a different timezone.
For the Service, DateTime.LocalNow() is different from your desktop.
Your calculation gives the wrong age.
<p>
The purpose of this function is to give a Local Now which is always in the same time zone.
<p>
Method<br>
The function reaches out to a 3rd party api, https://worldtimeapi.org/.
Your ask the api for the time in a timezone of your choice.
This would normally be the timezone of your readings.
<p>
The timezone is specified by area, location and region parameters.
Allowed values are documented at https://worldtimeapi.org/timezones.
<p>
The function calls the api and a record is returned.
The record contains a field ""datetime"".
Despite its name, it contains zone data.
This is converted to type datetimezone.
Then the zone is removed.
It would not work to convert type to datetime from datetimezone.
This is because the conversion converts to UTC.
You must remove the zone instead.
",
Documentation.Examples = {
[
Description = "Find the local datetime in New York",
Code = "LocalDateTime( ""America"", ""New_York"" )",
Result = "Returns the local datetime in New York"
],
[
Description = "Find the local datetime in Buenos_Aires",
Code = "LocalDateTime( ""America"", ""Argentina"", ""Buenos_Aires"" )",
Result = "Returns the local datetime in Buenos Aires"
]
}
]
in
//Make fnType the type for fn.
Value.ReplaceType(fn, fnType)

 

VN999
Resolver I
Resolver I

 

  • Verify System Time and Time Zone:

    • Ensure your computer's date, time, and time zone settings are correct.
    • On Windows, you can check this in the Date & Time settings.
    • On macOS, you can check this in the Date & Time preferences.
  • Check Power BI Desktop Regional Settings:

    • Open Power BI Desktop.
    • Go to File > Options and settings > Options.
    • Under the Regional Settings, ensure the correct locale is selected.
  • Adjust for Time Zone Differences:

    • If you're working in Power BI Service (online), note that the service runs in UTC time. You may need to adjust for your local time zone using DAX or Power Query transformations.
  • Use the following formula to adjust the time zone (e.g., UTC+2 for Central European Time)
  • Custom Column= DateTimeZone.SwitchZone(DateTimeZone.FixedUtcNow(), 2, 0)

 

Hi, the time settings on my computer are already right; the time zone is related to Amsterdam, Berlin, Rome, ... (UTC+01:00), therefore West Europe Standard Time.

I'm using Fabric dataflow gen2, applying DateTimeZone.SwitchZone(DateTimeZone.FixedUtcNow(), 1, 0) I obtain a datetime value with 1 hour backwords.

The issue is partially solved because the time zone on my computer is UTC +01:00; I'd like to see a time with the time zone hours. Thanks

Moreover, does DateTimeZone.FixedUtcNow() consider the local daylight savings time?

Ahmedx
Super User
Super User

If the objective is to remove 2 hours from each datetime then you could use,

= Table.TransformColumns( 
#"Previous Step",
{ {"Date-time", each _ -#duration( 0, 2, 0, 0 ) } }
)

Hi, the right time should have 2 hours more. It should be better to use a function that changes the time zone.

Thanks

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.

Top Solution Authors