Forum Discussion

CorniTiger21's avatar
CorniTiger21
New Member
3 years ago
Solved

Custom Column Conundrum

I am trying to replicate this excel calculation in Power Bi to create a Period Offset column:-    =[@Index]-XLOOKUP(TODAY(),[Date],[Index])   Can this be done as a Custom Column?    Loving Powe...
  • KNP's avatar
    KNP
    3 years ago

    Something like this might work.

    Go to Transform Data and paste this into the advanced editor of a blank query...

    let
        Source = Table.FromRows(
            Json.Document(
                Binary.Decompress(
                    Binary.FromText(
                        "i45WMjDUNzDRNzIwtFDSUTKyQOIYAjGKLEjAsaAIpA4ioGsCJEBY18xAKVaHoGFG1DTMmJqGmVDTMFPiDYsFAA==",
                        BinaryEncoding.Base64
                    ),
                    Compression.Deflate
                )
            ),
            let
                _t = ((type nullable text) meta [Serialized.Text = true])
            in
                type table [
                    #"Period Start Date" = _t,
                    #"Period End Date" = _t,
                    Index = _t,
                    Date = _t,
                    Period = _t,
                    Month = _t,
                    Year = _t,
                    CurYearOffset = _t,
                    MonthNum = _t,
                    PeriodOffset = _t
                ]
        ),
        ChangeType = Table.TransformColumnTypes(
            Source,
            {
                {"Period Start Date", type date},
                {"Period End Date", type date},
                {"Date", type date},
                {"Index", Int64.Type},
                {"Period", Int64.Type},
                {"Year", Int64.Type},
                {"CurYearOffset", Int64.Type},
                {"MonthNum", Int64.Type},
                {"PeriodOffset", Int64.Type}
            }
        ),
        AddDaysInPeriod = Table.AddColumn(
            ChangeType,
            "DaysInPeriod",
            each Number.RoundTowardZero(Duration.TotalDays([Period End Date] - [Period Start Date])) + 1,
            Int64.Type
        ),
        AddNewPeriodOffset = Table.AddColumn(
            AddDaysInPeriod,
            "NewPeriodOffset",
            each
                Number.RoundTowardZero(
                    Duration.TotalDays([Period Start Date] - DateTime.Date(DateTime.FixedLocalNow())) / [DaysInPeriod]
                ),
            Int64.Type
        )
    in
        AddNewPeriodOffset