I am very new to BigQuery by google
I want to parse time stamp (yyyy/mm/dd:hh:mm:ss) based on the day and the month wish to bucket days into weeks.
I didn’t find any BigQuery function which does this.
Hence, I was wondering if there was a way in which I can write a UDF and then access it in a BigQuery query
There are two questions here, so two answers:
BigQuery does support UDFs: docs. (It didn’t when I first answered this.)
Even without UDFs, the date bucketing is still doable. BigQuery has one time parsing function,
PARSE_UTC_USEC, which is expecting input in the formYYYY-MM-DD hh:mm:ss. You’ll need to useREGEXP_REPLACEto get your date into the right format. Once you’ve done that,UTC_USEC_TO_WEEKwill block things into weeks, and you can group by that. So tying all that together, if your table has a column calledtimestamp, you could get counts by week via something likeNote that the
0here is the argument for which day of the week to use as the “beginning”; I’ve used Sunday, but for “business”-y things using1(i.e. Monday) would likely make more sense.Just in case you need it, the section on timestamp functions in the docs is helpful.