MySQL 5.6.27: How to get latest pairs for different points in time in a single query?
SET @date:='2018-01-03';

select * from ( select ta.*,
                       if(@typex=ta.group_id, 
                          @rownum:=@rownum+1, 
                          @rownum:=1+least(0,@typex:=ta.group_id)) rown 
                from stats_kpi ta, (select @rownum:=1, @typex:='_') zz
                where ts_week_start &lt;= timestamp(@date)
                order by group_id, ts_week_start DESC
              ) yy
where rown &lt; 3


fiddle