How do I pivot a dataset?¶
Pivoting creates columns named after the data value of some column. Because
datasets must have known schema, you cannot create an arbitrary pivot, because
the names of the columns won’t be fully known for all cases. However, if you
know what the names of the columns are, you can use an aggregation function
with any_not_null()
to simulate the effect:
// assume input has fields day:string and value:float64
// pivot value based on day
timestats
monday:any_not_null(if(day='monday',value,null_float64())),
tuesday:any_not_null(if(day='tuesday',value,null_float64())),
wednesday:any_not_null(if(day='wednesday',value,null_float64())),
thursday:any_not_null(if(day='thursday',value,null_float64())),
friday:any_not_null(if(day='friday',value,null_float64())),
saturday:any_not_null(if(day='saturday',value,null_float64())),
sunday:any_not_null(if(day='sunday',value,null_float64()))
You may also want to look at pivot_array()
which lets you transpose between
arrays, and key/value objects.