{"id":3391,"date":"2021-06-06T20:36:43","date_gmt":"2021-06-07T03:36:43","guid":{"rendered":"https:\/\/SUMMALAI.COM\/?p=3391"},"modified":"2021-06-06T20:36:44","modified_gmt":"2021-06-07T03:36:44","slug":"how-to-solve-dax-time-zone-issue-in-power-bi","status":"publish","type":"post","link":"https:\/\/SUMMALAI.COM\/?p=3391","title":{"rendered":"How to Solve DAX Time Zone Issue in Power BI"},"content":{"rendered":"\n<p>Power BI is a cloud service, and that means Power BI files are hosted somewhere. Some DAX functions such as Date\/Time functions work on system date\/time on the server their file is hosted on. So If you use DAX functions such as TODAY() or NOW() you will not get your local date\/time, You will fetch server\u2019s date\/time. In this blog post I\u2019ll explain methods of solving this issue, so you could use Power BI to resolve your specific time zone\u2019s date and time. If you want to learn more about Power BI read Power BI online book;\u00a0<a href=\"https:\/\/radacad.com\/online-book-power-bi-from-rookie-to-rockstar\">Power BI from Rookie to Rock Star<\/a>.<\/p>\n\n\n\n<p><a href=\"mailto:?subject=Solving%20DAX%20Time%20Zone%20Issue%20in%20Power%20BI&amp;body=Hey%20check%20this%20out:%20https%3A%2F%2Fradacad.com%2F%3Fp%3D2645\"><\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i1.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2016-05-16_21h08_11.png\"><img decoding=\"async\" src=\"https:\/\/i1.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2016-05-16_21h08_11.png?resize=640%2C317\" alt=\"2016-05-16_21h08_11\" class=\"wp-image-2649\"\/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Defining the Problem<\/h1>\n\n\n\n<p>Using DAX functions on your local Power BI file is totally different from what you will see in Power BI website especially when date and time functions has been used. The reason is that DAX works with the date and time of the system that hosted the Power BI file. Power BI is a could based service, and that means Power BI files will be hosted on a server somewhere in the world, that might not be on the same time zone as your city is. So as a result when you used functions that works with the current date and time; such as TODAY() or NOW() in DAX you will not get your local current date and time. At this stage there is time zone feature in DAX functions to help resolving this, so I suggest few options to resolve it as below.<\/p>\n\n\n\n<p>Screenshot below shows a Power BI report published on Power BI website, and the result of DAX NOW() function there compared with the local date\/time on the client system. Please note that you won\u2019t see this anomaly in Power BI Desktop, because in that case file is running on your local system, and the result would be your local date\/time, you will only face this issue when you publish solution to Power BI website.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i2.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2016-05-16_20h58_49.png\"><img decoding=\"async\" src=\"https:\/\/i2.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2016-05-16_20h58_49.png?resize=640%2C444\" alt=\"2016-05-16_20h58_49\" class=\"wp-image-2647\"\/><\/a><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Method 1 \u2013 DAX Formula Manipulation<\/h1>\n\n\n\n<p>One easy way of solving this is to add time offset to the date\/time function in DAX. Power BI date\/time seems to be GMT. So if I want to show my local time in Auckland, I have to add 12 hours to it. Or for Seattle I have to reduce 7 hours from it.<\/p>\n\n\n\n<p>So I create a new calculation as DAX NZ TIME with this code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">DAX NZ TIME = NOW()<strong>+(12\/24)<\/strong><\/pre>\n\n\n\n<p>and another for DAX Seattle Time with this code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">DAX Seattle TIME = NOW()<strong>-(7\/24)<\/strong><\/pre>\n\n\n\n<p>Here is corrected result as below;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i1.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2016-05-16_21h08_11.png\"><img decoding=\"async\" src=\"https:\/\/i1.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2016-05-16_21h08_11.png?resize=640%2C317\" alt=\"2016-05-16_21h08_11\" class=\"wp-image-2649\"\/><\/a><\/figure>\n\n\n\n<p>This method works but has an issue which I deal with it later on.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Method 2 \u2013 Power Query DateTimeZone Functions<\/h1>\n\n\n\n<p>Thanks to my friend&nbsp;<a href=\"http:\/\/www.excelguru.ca\/blog\/\" target=\"_blank\" rel=\"noreferrer noopener\">Ken Puls<\/a>&nbsp;who mentioned this method to me in PASS BA conference, I come with this second option. Fortunately in Power Query there is set of functions for&nbsp;<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/mt296609.aspx\">DateTimeZone<\/a>. Ken already has a&nbsp;<a href=\"http:\/\/www.excelguru.ca\/blog\/2015\/05\/20\/data-from-different-timezones\/\" target=\"_blank\" rel=\"noreferrer noopener\">blog post<\/a>&nbsp;about time zones with Power Query which is a good read and recommended. DateTimeZone functions has options such as fetching local time or switching time zones. For this purpose I can use&nbsp;<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/mt253522.aspx\">DateTimeZone.SwitchZone<\/a>&nbsp;function to switch server\u2019s local time to my time zone\u2019s date and time.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">= DateTimeZone.SwitchZone(DateTimeZone.LocalNow(),12,0)<\/pre>\n\n\n\n<p>12 is hours, and 0 is minutes for the new time zone. script above will turn the local time zone to NZ time. for turning that into Seattle time I have to set parameters to -7, and 0.<\/p>\n\n\n\n<p>And here is the result set:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i1.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2016-05-16_21h49_59.png\"><img decoding=\"async\" src=\"https:\/\/i1.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2016-05-16_21h49_59.png?resize=640%2C95\" alt=\"2016-05-16_21h49_59\" class=\"wp-image-2652\"\/><\/a><\/figure>\n\n\n\n<p>You can also use other functions such as&nbsp;<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/mt253514.aspx\">DateTime.AddZone<\/a>&nbsp;in Power Query to turn the local date time to specific time zone.<\/p>\n\n\n\n<p>Well above solution works like DAX method, but both suffer from similar issue; Day Light Saving Time. This is the reason that if you try code above in summer or winter you might get different result!<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Method 3 \u2013 Web Query with Power Query<\/h1>\n\n\n\n<p>Day Light Saving is a big challenge, because each time zone, city, or country might have different day light saving time. even same city might have different dates for&nbsp;<a href=\"http:\/\/www.timeanddate.com\/time\/dst\/2016.html\" target=\"_blank\" rel=\"noreferrer noopener\">DST<\/a>&nbsp;(Daylight Saving Time) for different years! Power Query is intelligence enough to help with Time Zone issue, but doesn\u2019t have a directory of all DST times for all time zones. Fortunately Power Query can query web URL. And there are some websites that give you the current date and time for your specific city, country, or time zone. And those websites usually consider DST correctly. One of these websites is&nbsp;<a href=\"http:\/\/www.timeanddate.com\/worldclock\/\" target=\"_blank\" rel=\"noreferrer noopener\">TimeAndDate.com<\/a>&nbsp;. As you see in screenshot below this website gives me the current date and time for most of cities around the world;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i2.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2016-05-16_23h16_50.png\"><img decoding=\"async\" src=\"https:\/\/i2.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2016-05-16_23h16_50.png?resize=640%2C350\" alt=\"2016-05-16_23h16_50\" class=\"wp-image-2658\"\/><\/a><\/figure>\n\n\n\n<p>In Power Query we can use functions such as Web.Page() and Web.Contents() to read tables in a web page, and then fetch part of it that we want with some other transformations. I won\u2019t be explaining details of using timeanddate.com URL to fetch the local city here because it would make this post very long. I just refer you to my other post about reading some date\/time information for different time zones which is similar to method I\u2019ve used here. If you want to understand how code below works read the post&nbsp;<a href=\"https:\/\/radacad.com\/power-query-not-for-bi-event-date-and-time-scheduler-part-1\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>. For this part I will be using&nbsp;<a href=\"http:\/\/localtimes.info\/Oceania\/New_Zealand\/Auckland\/\" target=\"_blank\" rel=\"noreferrer noopener\">another website<\/a>&nbsp;which gives me current date and time in Auckland, and here is Power Query code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let\n    Source = Web.Page(Web.Contents(\"http:\/\/localtimes.info\/Oceania\/New_Zealand\/Auckland\/\")),\n    Data1 = Source{1}[Data],\n    #\"Changed Type\" = Table.TransformColumnTypes(Data1,{{\"Column1\", type text}, {\"Column2\", type text}}),\n    date = #\"Changed Type\"{1}[Column2],\n    time=#\"Changed Type\"{0}[Column2],\n    datetime=DateTime.FromText(date&amp;\" \"&amp;time)\nin\n    datetime<\/pre>\n\n\n\n<p>And here is the result with the correct DST and time zone;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i2.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2016-05-16_23h08_59.png\"><img decoding=\"async\" src=\"https:\/\/i2.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2016-05-16_23h08_59.png?resize=640%2C326\" alt=\"2016-05-16_23h08_59\" class=\"wp-image-2655\"\/><\/a><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Method 3 Revisited \u2013 with Xml.Document<\/h1>\n\n\n\n<p>the method mentioned with web query uses Web.Page Power Query function and hence it requires gateway setup to work. Thanks to Yingwei Yang from Microsoft team who suggested this approach; there is another way which doesn\u2019t require gateway setup: using Xml.Document function. Let\u2019s go through that solution.<\/p>\n\n\n\n<p><a href=\"https:\/\/timezonedb.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Timezonedb.com<\/a>&nbsp;is the website that has an api to return timezone information, fortunately api is free to use. you need to register for the api;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2017-08-25_04h43_22.png\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2017-08-25_04h43_22.png?resize=640%2C241\" alt=\"2017-08-25_04h43_22\" class=\"wp-image-6125\"\/><\/a><\/figure>\n\n\n\n<p>after registering you will receive an API Key which you can use in a api url as below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2017-08-25_04h47_49.png\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2017-08-25_04h47_49.png?resize=640%2C416\" alt=\"2017-08-25_04h47_49\" class=\"wp-image-6126\"\/><\/a><\/figure>\n\n\n\n<p>To learn more about API&nbsp;<a href=\"https:\/\/timezonedb.com\/api\" target=\"_blank\" rel=\"noreferrer noopener\">read this link<\/a>.<\/p>\n\n\n\n<p>Now that we have an api to work with, we can use Xml.Document function to read data from it. here is how to do it. start with a Blank Query in Power Query, then go to View -&gt; Advanced Editor and replace the whole query with below script:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let \n    Source = Xml.Document(Web.Contents(\"http:\/\/api.timezonedb.com\/v2\/get-time-zone?key=XYZ&amp;format=xml&amp;by=zone&amp;zone=PDT\")),\n    Value = Source{0}[Value],\n    Value1 = Value{12}[Value]\nin\n    Value1<\/pre>\n\n\n\n<p>This method is the recommended method from all above options.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i2.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2017-08-25_05h01_00.png\"><img decoding=\"async\" src=\"https:\/\/i2.wp.com\/radacad.com\/wp-content\/uploads\/2016\/05\/2017-08-25_05h01_00.png?resize=485%2C510\" alt=\"2017-08-25_05h01_00\" class=\"wp-image-6127\"\/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Other Methods<\/h3>\n\n\n\n<p>At the time of writing this post, I\u2019ve only thought about these three methods. You might have an idea about another method. In that case, don\u2019t hesitate to share it here.<\/p>\n\n\n\n<p>Ref: <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Power BI is a cloud service, and that means Power BI files are hosted somewhere. Some DAX functions such as Date\/Time functions work on system date\/time on the server their file is hosted on. So If you use DAX functions such as TODAY() or NOW() you will not get your local date\/time, You will fetch <a class=\"read-more\" href=\"https:\/\/SUMMALAI.COM\/?p=3391\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10,621],"tags":[756],"class_list":["post-3391","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-power-bi","tag-time-zone-issue-in-power-bi"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3391"}],"version-history":[{"count":1,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3391\/revisions"}],"predecessor-version":[{"id":3393,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3391\/revisions\/3393"}],"wp:attachment":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}