versioninfo() using DataFramesMeta panel_data = DataFrame( sample_id = [1, 1, 2, 2, 3, 4, 5, 5], treatment = ["T", "T", "F", "F", "F", "T", "F", "T"], measure = [2, 3, 1, 1, 4, 3, 4, 5] ) function latest(treatment, measure) i = findlast(==("T"), treatment) isnothing(i) ? missing : measure[i] end @linq panel_data |> groupby(:sample_id) |> combine( has_treated = any(==("T"), :treatment), initial_value = first(:measure), latest_value_when_treated = latest(:treatment, :measure) )