Forum Discussion

NrAg's avatar
NrAg
Frequent Visitor
3 years ago
Solved

Next highest Date per Category

Hi PBI-experts!   Recently, I faced the challenge of finding the last day of period based on the first date of the next period on a product level. Date would indicate the end-date of that specific ...
  • jbwtp's avatar
    3 years ago

    Hi NrAg,

     

    Are you looking for something like this?

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jc5BCoAgEIXhu7h2wBkVu4uITFTURkKF6PY1HSDc/Yv3wYtRtWvl3vaj9rxx6dzuPFcuSzaotCJDBAhWGr1KeghYICfAjgIHGATQKAiARgB+4HxX7ee+l3QD0wA0Sb/H0wM=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [product_name = _t, start_date = _t, price = _t]),
        
        f = (t as table) as table =>
            let
                add = Table.ToColumns(t) & {List.Transform(List.Skip(t[start_date]), each Date.AddDays(_, -1))},
                format = Table.FromColumns(add, Value.Type(Table.AddColumn(t, "end_date", each null, type date)))
            in
                format,
    
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"product_name", type text}, {"start_date", type date}, {"price", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"product_name"}, {{"Out", f, Value.Type(Table.AddColumn(#"Changed Type", "end_date", each null, type date))}}),
        #"Expanded Out" = Table.ExpandTableColumn(#"Grouped Rows", "Out", {"start_date", "price", "end_date"}, {"start_date", "price", "end_date"})
    
    in #"Expanded Out"

     

    Cheers,

    John

  • wdx223_Daniel's avatar
    3 years ago

     

    NewStep= Table.FromRecords(List.Accumulate(Table.ToRecords(Table.Sort(PreviousStepName,{"product_name",{"start_date",1}})),{{},[]},(x,y)=>{{y&[end_date=Record.FieldOrDefault(x{1},y[product_name],null)]}&x{0},Record.TransformFields(x{1},{y[product_name],each Date.AddDays(y[start_date],-1)},2)}){0})

  • NrAg's avatar
    NrAg
    3 years ago

    I wish I would fully understand the formula but will definitely spend some time to read it through. However, it works perfectly and I very much appreciate your help!