viewof datadf = aq
.from(data)
.orderby(aq.desc("Total_2019")).derive({ rank_total_2019: op.avg_rank() })
.orderby(aq.desc("Total_2020")).derive({ rank_total_2020: op.avg_rank() })
.orderby(aq.desc("Total_2021")).derive({ rank_total_2021: op.avg_rank() })
.orderby(aq.desc("Total_2022")).derive({ rank_total_2022: op.avg_rank() })
.fold([
"Total_2019",
"Total_2020",
"Total_2021",
"Total_2022",
"Federal_2019",
"Federal_2020",
"Federal_2021",
"Federal_2022",
])
.rename({ key: "Year", value: "Expenditure" })
.derive({
Type: (d) => op.split(d.Year, "_")[0],
Year: (d) => +op.split(d.Year, "_")[1],
Expenditure: (d) => +op.replace(d.Expenditure, /\,/g, ""),
Rank: (d) => +d.Rank
})
.groupby(["Institution", "Rank", "Year"])
.pivot("Type", "Expenditure")
.view()