Forum Discussion
Convert DateTimeOffset to DateTime
The table User contains multiple rows which need to be displayed in a chart.
The offset is stored in another table [Settings] in the column TimeZoneOffset
What I would like to know is the place where I could insert Power Query to transform my data, can you help with that?
In the query editor.
Edit your first table and "lookup" the data from your settings table by merging it.
Then add a custom column which add/subtracts the values.
- AverageAsker10 years agoHelper I
Thank you, what is Power Query equivalent to .NET TimeZoneInfo.FindSystemTimeZoneById ? My Settings table stores Time Zone info by using standard IDs, like Pacific Standard Time and I need to convert values in my DateTimeOffset column to the time zone stored in the Settings table, this is how I do that in C#:
TimeSpan timeZoneOffsetSpan = TimeZoneInfo.FindSystemTimeZoneById(settings.TimeZone).GetUtcOffset(UserCreatedDateTime);
This gives me a difference between the time in the time zone stored in my Settings table and UTC for UserCreatedDateTime. And I am executing that line of the code just once for all rows in my User table because the purpose of the code above is to find out current offset taking into account such timezone's features like DayLightSaving.
And now I can simply add that offset (which could be positive or negative) against every value in my User.CreateDateTime column, in C# I am doing that by using DateTimeOffset.ToOffset:
DateTimeOffset convertedOffset = UserCreatedDateTime.ToOffset(timeZoneOffsetSpan)
So I need to be able to do the same conversions in my Power BI report but I couldnot find any Power Query function which can do the same what TimeZoneInfo.FindSystemTimeZoneById does, if that function existed it would cover my first line of C# code and then I would need to find an equivalent to TimeZoneInfo.FindSystemTimeZoneById. DateTimeZone.SwitchZone is probably what I need but I first need to know the offset corresponding to my time zone and then I would be able to supply that offset as 2nd and 3rd parameters to that function.
So, to finalize, I need Power BI analog for TimeZoneInfo.FindSystemTimeZoneById
- ImkeF10 years agoCommunity Champion
Yes, DateTime.SwitchZone does that offset.
Also cannot spot anything similar to the other function. It looks as if it takes in a text that represents the timezone and then converts it to a timezone value. But that’s actually not what you need at the end: As that’s only the offset-value.
So how about creating a lookuptable instead where the offset-values for all your possible text-values (time zones) are stored? As this wouldn’t change, you just fetch your offset-value from there then.
- AverageAsker10 years agoHelper I
As the very worst case I would likely do something similar but I would not want to do that because it will not be just key-value pairs, it has to be something smarter which will take into account Day Light Savings Time for specific timezone which eventually means that there are multiple offsets per any time zone, so, that sounds like not just lookup but lookup + logic and if I don't implement that logic my reports would be wrong at least 2 times per year.
I could not beleive that such basic functionality is not available in Power BI, these are just fundamentals for any live report (which Power BI claims to be) to have true support for time zones (Offset can be determined by knowing a time zone but but time zone can not be determined if just offset is known because there are multiple offsets per any time zone).
Is that possible to use .NET language while shaping a data for Power BI ?