Sin
Sin

Reputation: 161

GCP Bigquery WORKDAY function

How do I go about calculating number of workdays in a MONTH based on a date in another column?

Example: Column 1 - 2020-06-30 Column 2 (Calculated) - 22 (i.e number of workdays in the month of June Mon to Friday)

Does BQ have a WORKDAY function?

Upvotes: 1

Views: 1321

Answers (1)

Mikhail Berlyant
Mikhail Berlyant

Reputation: 172944

You can use below approach

create temp function workdays(input date) as (( 
  select count(*)
  from unnest(generate_date_array(date_trunc(input, month), last_day(input, month ))) day
  where not extract(dayofweek from day) in (1, 7)
));
select column1, 
  workdays(column1) as column2
from your_table    

if applied to sample data in your question - output is

enter image description here

Upvotes: 2

Related Questions