use hahn;
$week=($date)-> { 
RETURN DateTime::Format("%Y-%m-%d")(Datetime::MakeDate(DateTime::StartOfWeek(CAST($date as Date))));};

select 
week,
debug_type,
sum(plan) as plan,
sum(fact) as fact,
sum(plan) - sum(fact)as underplan
FROM `home/vipplanners/weekly_pack/media_under_plan_w_reasons`
GROUP BY $week(day) as week, debug_type