Forum Discussion

leodec11's avatar
leodec11
Frequent Visitor
6 months ago
Solved

Fix the UTC and PST timezone issue

hi    Need help with UTC and PST timezone issue.   PowerBI report showing data from oracle and in PBI desktop using local PST date but as soon as it is published on PBI service it is using UTC da...
  • Natarajan_M's avatar
    6 months ago

    Hi leodec11  , you are calling the function inside your query logic . first we need to isolate where the issue lies whether in you pst/utc function or the main query where you are passing the parameter .

    first we need to check if your function is giving the correct result in local machine and server .


    I created a sample pbix to check if the timezone converison is givig me the expected result .

    I'm based out of CST so the in my macchine system time and CST should match .

     


    in service UTC and System time should match since the service will be defaulted to utc 

     


    PQ to get the table :

    let
    // 1. Get Current Times
    Source = let
    UtcNow = DateTimeZone.UtcNow(),
    SystemNow = DateTime.LocalNow(),

    // 2. DST Logic
    // Get the date from UTC to determine the year
    CurrentDate = DateTime.Date(DateTimeZone.RemoveZone(UtcNow)),
    CurrentYear = Date.Year(CurrentDate),

    // DST Starts: 2nd Sunday in March
    StartDST = Date.StartOfWeek(#date(CurrentYear, 3, 14), Day.Sunday),
    // DST Ends: 1st Sunday in November
    EndDST = Date.StartOfWeek(#date(CurrentYear, 11, 7), Day.Sunday),

    IsDST = CurrentDate >= StartDST and CurrentDate < EndDST,

    // 3. Set Offsets
    CST_Offset = if IsDST then -5 else -6,
    PST_Offset = if IsDST then -7 else -8,

    // 4. Calculate Zone Times AND REMOVE ZONE info

    UTC_Fixed = DateTimeZone.RemoveZone(UtcNow),
    CST_Fixed = DateTimeZone.RemoveZone(DateTimeZone.SwitchZone(UtcNow, CST_Offset)),
    PST_Fixed = DateTimeZone.RemoveZone(DateTimeZone.SwitchZone(UtcNow, PST_Offset))
    in
    [
    Now_System = SystemNow,
    UTC_Time = UTC_Fixed,
    CST_Time = CST_Fixed,
    PST_Time = PST_Fixed,
    Is_DST_Active = IsDST
    ],

    // 5. Convert Record to Table
    #"Converted to Table" = Record.ToTable(Source),

    // 6. Transpose
    #"Transposed Table" = Table.Transpose(#"Converted to Table"),

    // 7. Promote Headers
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),

    // 8. Data Types
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{
    {"Now_System", type datetime},
    {"UTC_Time", type datetime},
    {"CST_Time", type datetime},
    {"PST_Time", type datetime},
    {"Is_DST_Active", type logical}
    })
    in
    #"Changed Type"


    now you check if this works as expected in service , then use the max(pst) in your main query logic 


    Thanks 

    If this response was helpful in any way, I’d gladly accept a kudo.
    Please mark it as the correct solution. It helps other community members find their way faster