GCP Bigquery WORKDAY function - google-cloud-platform

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?

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

Related

DAX to calculate no of days between 2 dynamic dates

I have 2 date column in my table ie entrydate and exitdate and also containerno column and 1 seperate date table in power bi.
Now the case is -
I have 1 date slicer in report.
Need to count no of days in monthly basis.
Eg if my container enter in 20-nov-2020 and exitdate is 5-Dec-2020
So when we select 26-nov-2020 in my date slicer then
Required output is 6 days.
Means then my selected date will become my exitdate.
And when we select 1-dec-2020 to 10-dec-2020 then
Output is 5 days
So my entry and exit date can be dynamic based on selected date by comparing with entrydate and exitdate

Filter table based on a specific date plus 7 days

I have a table containing a date field (from 1 March 2020 to now) that I need to filter to a specific date and the previous 6 days to give complete week's data. So if I chose 30 March I'd get a table of 24 March to 30 March. If I then chose 31 March the table would show 25 March to 31 March.
I can use a date slicer to choose a range of dates but I want to be able to pick a single date, with Power BI automatically selecting the earlier date.
Any pointers much appreciated.
Mark.
You can create two measure - one for Slicer selected date and Another one with 7 day minus from the selected date as below-
Considering your date table name is- Dates
selected_date = SELECTEDVALUE(Dates[Date])
seven_day_starts_from = DATEADD(Dates[Date],-7,DAY)
Now create your calculated measure first like-
total_sales = SUM(Sales[sale])
Here comes how you will always calculate last 7 days sales considering the selected date in the slicer-
7_day_sales =
(
CALCULATE(
[total_sales],
DATESBETWEEN(
'Dates'[Date],
[seven_day_starts_from],
[selected_date]
)
) + 0
)
Remember, this is just a sample flow showing how it should work. You should try to follow the steps with your data and table structure. Dates table is a calendar table and Sales table is connected to the Dates table using the Date column.

How to create MTD, QTD and YTD Sum of metric

I have table with data by Region, Country, Date, month, quarter, year and sales. I am trying to design a AWS Redshift query where I could have columns for MTD (full month of sales), QTD (quarterly aggregation based on months) and YTD (yearly aggregation of months) by Region, Country, Year , Quarter and Month. Right now I am struggling with how to do running sum by quarter and by year for previous months.
Sample data:
Output:
For each year/quarter are you trying to show what the xTD values were up until that period? Seems like a strange way to present the data, usually xTD figure are shown 'as of now' and not historically as that can be very confusing.
If you are looking for regular xTD figures then use this:
select country,
sum(case when date >= date_trunc('month', CURRENT_DATE) then sales else 0 end) as mtd,
sum(case when date >= date_trunc('quarter', CURRENT_DATE) then sales else 0 end) as qtd,
sum(case when date >= date_trunc('year', CURRENT_DATE) then sales else 0 end) as ytd
from data
group by country
Interactive example here.

In sas application Set date parameter and put it in the contacted column to retrieve data in a certain period

The date in the table is not one set,
Days in the days column and months in the month column and years in the year column
I have concatenated the columns and then put these concatenation in where clause and put the parameter I have made but I got no result
I assume you are querying a date dimension table, and you want to extract the record that matches a certain date.
Solution:
I created a dates table to match with,
data dates;
input key day month year ;
datalines;
1 19 2 2018
2 20 2 2018
3 21 2 2018
4 22 2 2018
;;;
run;
Output:
In the where clause I parse the date '20feb2018'd using day, month & year functions: in SAS you have to quote the dates in [''d]
proc sql;
select * from dates
/*if you want to match todays' date: replace '20feb2018'd with today()*/
where day('20feb2018'd)=day and month('20feb2018'd)=month and year('20feb2018'd)=year;
quit;
Output:
if you compare date from day month and year, then use mdy function in where clause as shown below. it is not totally clear what you are looking for.
proc sql;
select * from dates
where mdy(month,day, year) between '19feb2018'd and '21feb2018'd ;

Drilling down periodical time data (subscriptions with start & end date) per month?

I'm currently struggling with following request in PowerBI:
I have two CSV files as PowerBI queries, one which defines fiscal months, and another one which lists all subscriptions including start and end date:
Fiscal month CSV:
Month Fiscal Start Fiscal End
January 03.01.2016 04.02.2016
February 05.02.2016 03.03.2016
March 04.03.2016 06.04.2016
April 07.04.2016 02.05.2016
May 03.05.2016 06.06.2016
June 07.06.2016 03.07.2016
July 04.07.2016 05.08.2016
August 06.08.2016 02.09.2016
Subscription CSV:
Account-ID Subscription-Start Subscription-End Item Count
101 08.01.2016 07.02.2016 5
102 15.01.2016 14.03.2016 3
103 05.01.2016 04.06.2016 10
101 08.02.2016 07.03.2016 3
104 10.04.2016 09.05.2016 5
105 16.04.2016 15.07.2016 2
My challenge now is to drill down all subscription item counts per fiscal month as a powerBI table.
Note: an Item Count is valid for a fiscal month if its Subscription-Start < Fiscal End and its Subscription-End > Fiscal End. (Example: A subscription from 15.01.2016 - 14.02.2016 should be counted in january, but not in february)
PowerBI table (schematical example):
Month Item Count
January 18
February 16
March 10
April 17
May 12
June 2
July 0
August 0
How can I implement this report in PowerBI?
THX in advance for your help and BR
bdriven
I've found following solution for my problem:
First I've created a new Table and made a crossjoin of the two queries. Then I've filtered for the lines, where my Subscription Start was before the Fiscal Month End and Subscription End was after the Fiscal Month End.
Based on this new table I can create all respective reports.
Example Code see below:
Fiscal Month Report =
FILTER(
CROSSJOIN(
ALL('Fiscal_month');
ALL('Subscription')
);
('Subscription'[Subscription-Start] < 'Fiscal_month'[Fiscal End] && 'Subscription'[Subscription-End] > 'Fiscal_month'[Fiscal End])
)