Forum Discussion

aabalosc's avatar
aabalosc
Frequent Visitor
9 years ago
Solved

DateTimeOffset data type not understood by Power BI (ODBC Driver 13 for SQL Server)

Hi Power BI Community!!   I am using the ODBC Driver 13 for SQL Server to get data from an Azure SQL database. This database contains tables with DateTimeOffset columns.   When I import one of th...
  • aabalosc's avatar
    aabalosc
    9 years ago

    Hi!! I have worked out some Power Query code to produce a function that can be easily applied to any "binary" column of DatetimeOffset data type. This simplifies the process of applying the logic to one or more columns. I hope it helps!!

    (to use it, just copy it to a blank query in Power BI and then add a new column to the data by using the "invoke custom function" option)

     

    let
        DatetimeOffsetParsing = (binaryInput as binary) =>
    
    let
        DateTimeOffsetParser = BinaryFormat.ByteOrder(
                    BinaryFormat.Record([
                        Year = BinaryFormat.SignedInteger16,
                        Month = BinaryFormat.UnsignedInteger16,
                        Day = BinaryFormat.UnsignedInteger16,
                        Hour = BinaryFormat.UnsignedInteger16,
                        Minute = BinaryFormat.UnsignedInteger16,
                        Second = BinaryFormat.UnsignedInteger16,
                        Ticks = BinaryFormat.UnsignedInteger32,
                        ZoneHours = BinaryFormat.SignedInteger16,
                        ZoneMinutes = BinaryFormat.SignedInteger16
                        ]),
                    ByteOrder.LittleEndian),
        DateTimeOffset = DateTimeOffsetParser(binaryInput),
        AsText = Text.Combine({
                    Text.PadStart(Text.From(DateTimeOffset[Year]), 4, "0"),
                    "-",
                    Text.PadStart(Text.From(DateTimeOffset[Month]), 2, "0"),
                    "-",
                    Text.PadStart(Text.From(DateTimeOffset[Day]), 2, "0"),
                    " ",
                    Text.PadStart(Text.From(DateTimeOffset[Hour]), 2, "0"),
                    ":",
                    Text.PadStart(Text.From(DateTimeOffset[Minute]), 2, "0"),
                    ":",
                    Text.PadStart(Text.From(DateTimeOffset[Second]), 2, "0"),
                    ".",
                    Text.PadStart(Text.From(DateTimeOffset[Ticks] / 100), 2, "0"),
                    " ",
                    if DateTimeOffset[ZoneHours] < 0 then "-" else "+",
                    Text.PadStart(Text.From(Number.Abs(DateTimeOffset[ZoneHours])), 2, "0"),
                    ":",
                    Text.PadStart(Text.From(Number.Abs(DateTimeOffset[ZoneMinutes])), 2, "0")}),
        myDatetime = DateTimeZone.FromText(AsText),
        Result = myDatetime
    in
        if binaryInput is null then null else Result
    
    in
        DatetimeOffsetParsing