PowerBI: Using slicer to change date column into user local time - powerbi

I have a dataset that uses Direct Query. I have a timestamp column and I am trying to add another column that will show the local time of that column for differnt users based on the selection from the slicer (which contains the list of countries). The new column should show the timezone based on the slicer list but I am not getting the right timezone.
I managed to connect to a timezonedb that will give me a list of offset for different timezones based on this article in this link: https://whitepages.tygraph.com/2020/10/dynamic-time-zone-conversion-using-power-bi/
This is what I have tried:
DAX measure: selected_offset = MAX(global_timezone[offset_days])
New column(local_time): date_column + [selected_offset]
**Start Timestamp: Timestamp column is saved using UTC+0
**selected_offset = SG timezone (UTC+8) = 0.3333
Error:
selected_offset = 0.583333 (instead of 0.3333)
Start Timestamp selected_offset local_time
04/13/2022 2:38 0.583333333 04/13/2022 16:38
04/13/2022 2:37 0.583333333 04/13/2022 16:37
04/13/2022 2:37 0.583333333 04/13/2022 16:37
Desired output: (see local_time column below)
Start Timestamp selected_offset local_time
04/13/2022 2:38 0.333333333 04/13/2022 10:38
04/13/2022 2:37 0.333333333 04/13/2022 10:37
04/13/2022 2:37 0.333333333 04/13/2022 10:37
Please let me know how to get the correct date time for local_time column based on slicer selection. Most solutions I found offer solution in Power Query which is something I cannot use at the moment due to DQ I am using for this dataset. Thanks!

I'm not sure if you are certain how this solution (from the article) is working.
First of all, we get the current UTC (UTC+0) from UTCNOW() function then we check the offset to other timezones. This means you should know what time zone your timestamp is from. You need to do two calculations, first subtract the offset from the "base column" and then add the offset to the desired time zone.
For example, if I want to show time for someone in America/Chicago and I have a column with the timestamp 2022-04-14 09:55 (UTC+2 Europe/Warsaw). Offset to this timezone is 0.08333. offset to America/Chicago -0.21
then full calculation is '2022-04-14 09:55' - (0.08333) + (-0.21) == 2022-04-14 02:55

Related

If the measure value is Blank, rather show the most recent value that will not make the measure Blank based on 2 different Dates

I want my measure to display a value and not a Blank, but currently, there are two Dates that are affecting my outcome. Calculation Date and my Percentile Date.
Calculation Date is used for the overall report and it goes up until 09/02/2023
Percentile Date is specifically used in that 75thpercentile_measure calculation and goes up until 01/01/2023
My goal is to create a measure that does not go, Blank, when the Calculation Date is higher than the Percentile Date it just shows Blank in the card, but would like it to instead of going blank rather show the value of the Most Recen Calcualtion Date which is = to the Percentile Date.
It will always show blank when the Calculation Date is higher than 01/01/2023 but when they are equal or Calculation Date is lower than Percentile Date it will show the percentage values of the given days.
Sample Value of 75thpercentile_measure: 8.88%
Desired Output Measure =
IF(
ISBLANK([75thpercentile_measure])
,CALCULATE(
[75thpercentile_measure]
,overall_data[Calculation Date]=[Max Percentile Date Measure]
)
,[75thpercentile_measure]
)
If I still don't make any sense just comment below so I can clarify.

Power BI text to date in "mmm-yy" format

I have an excel file having a column with Text format values like Jan-18, Dec-19, Feb-20. I want to convert it to a date format but when I am trying to make this column a date format it printing 1/18/2021, 12/19/2021,2/20/2021. Basically its considering Jan as month(1), 18 as Date, & making all year as 2021 deafult.
Can someone help?
Original(text) After Date conversion Desired format
Jan-18 1/18/2021 01/01/2018
Feb-19 2/19/2021 01/02/2019
Mar-20 3/20/2021 01/03/2020
You can try,
DATE(2018, MONTH(convertedDate), 1)
This will convert your converted date to the first day of the relevant month in 2018. This is stored in Power BI as a datetime object. If you want to show that value in dd/MM/yyyy format you can do it by selecting correct Format in the top menu.
If you load in the Original column as text, try this trick:
Desired format = DATEVALUE ( "1-" & [Original] )
Appending the "1-" to the beginning gives it a day of the month and forces it to recognize the last digits as the year.
I faced with the same issue , while changing the format instead of using Date use the last option i.e "Using Locale... " -> in that change the Data Type from Text -> Date, and Locale to English(United Kingdom) -> Hit ok. Earlier Format ,What should be the desired format .This worked for me!

Extracting day in a new field from a date in Pentaho

I have a field with a date in yyyy-MM-dd format, I want to extract the day only, for example, if the date is 2014-01-25, I want to have "25" in a new field. I used step "Calculator" is calculation "day of month of date A" but I get 1970-01-01 01:00:00 for all records.
Could someone tell me what might be the reason?
Before you actually have the Calculator step, have a Select Values step and make sure the date field is Date by explicitly giving the Date data type and specify the format it is currently in.

Power BI - Getting a forecast date from a measure

I have a "DaysLeft" measure that I created that contains the number of days (Decimal) left until a specific Plant area is completed. So for each value in my "PlantArea" column, there is a unique "DaysLeft" measure value.
I now want to get the forecast date for when each "PlantArea" will be complete. Thus, Today()+DaysLeft for that particular plant area.
I've tried:
DATEADD(TODAY(), +[DaysLeft], DAY)
but that gives me nothing :(
Any idea how I can do this?
DATEADD() only works for column. For measure, you can simply try
Forecast Date = NOW() + [DaysLeft]
NOW() is better than TODAY() for this case as it returns the current datetime instead of just date.
Result:

Power Query: Date Time Zone operations

I am building Power BI report based on data from SQL Server's table User which contains a column CreatedDate of type DateTypeOffset(7).
I am looking for Power Query equivalent to .NET [TimeZoneInfo.FindSystemTimeZoneById]. My Report data has a Time Zone info which is stored as standard ID (like Pacific Standard Time) and I need to convert values in my DateTimeOffset column to the time zone I was talking about above, this is how I do that in C#:
TimeSpan timeZoneOffsetSpan = TimeZoneInfo.FindSystemTimeZoneById(settings.TimeZ​one).GetUtcOffset(UserCreatedDateTime);
This gives me a difference between the time in the time zone stored in my Settings 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].
Can anybody help?
You could add a table to the data model with columns for time zone ID and time zone offset. In your main query you can join this table and then create a calculation to add or subtract the offset into a new column. After that, remove the joined column.
See here How can I perform the equivalent of AddHours to a DateTime in Power Query?
Or you could use the DateTimeZone functions built into M.
You can take advantage of the AT TIME ZONE feature of TSQL to create a table of offsets (in minutes) for each day, for each timezone, and import the resultant table into PowerBI, so you can add/subtract relevant offsets to your base time.
If I was in the OP's situation, I would import (convert) all times to UTC, and then add the offset within PowerBI, dependent on the timezone you want to display. (maybe using this method?)
The example below gives a table the offsets for Australian timezones against UTC.
If my target table is in UTC and I want to change the time in PowerBI, I just have to add the specific offset to my time...
+------------+-----------+----------+----------+----------+
| TargetDate | QLDOffset | NTOffset | SAOffset | WAOffset |
+------------+-----------+----------+----------+----------+
| 2018-04-02 | -600 | -570 | -570 | -480 |
| 2018-04-01 | -600 | -570 | -570 | -480 |
| 2018-03-31 | -600 | -570 | -630 | -480 |
| 2018-03-30 | -600 | -570 | -630 | -480 |
+------------+-----------+----------+----------+----------+
You can see the offset changing for SA on 1st April
WITH MostDays AS ( --MostDays is a table of 10 years worth of dates going back from today.
SELECT Today = CAST(DATEADD(DAY, -days, GETUTCDATE()) AS DATETIMEOFFSET)
FROM (SELECT TOP 3650 Days = ROW_NUMBER() OVER (ORDER BY message_id ) -- this will give a rolling window of 10 years
FROM sys.messages
)x
)
-- This section takes the datetime from above, and calculates how many minutes difference there is between each timezone for each date.
Select TargetDate=CAST(Today AS DATE)
,QLDOffset = DATEDIFF(minute,CAST(Today at TIME ZONE 'UTC' AT TIME ZONE 'E. Australia Standard TIME' AS DateTime),CAST(Today at TIME ZONE 'UTC' AS DATETIME))
,NTOffset = DATEDIFF(minute,CAST(Today at TIME ZONE 'UTC' AT TIME ZONE 'AUS Central Standard Time' AS DateTime),CAST(Today at TIME ZONE 'UTC' AS DATETIME))
,SAOffset = DATEDIFF(minute,CAST(Today at TIME ZONE 'UTC' AT TIME ZONE 'Cen. Australia Standard TIME' AS DateTime),CAST(Today at TIME ZONE 'UTC' AS DATETIME))
,WAOffset = DATEDIFF(minute,CAST(Today at TIME ZONE 'UTC' AT TIME ZONE 'W. Australia Standard TIME' AS DateTime),CAST(Today at TIME ZONE 'UTC' AS DATETIME))
FROM MostDays
Obviously the table gets pretty big if you have no limits on dates or timezones