Bigquery - Exclude Current Month rows in a table - google-cloud-platform

Can I please get some help with excluding current month data only
I am using the below code but it excludes the same month from previous years as well.
Example: I would like all rows for the current month Feb-23 to be excluded but the below code is excluding Feb-23, Feb-22, Feb-21 etc as well.
SELECT * FROM TaBLE WHERE
EXTRACT(YEAR FROM CalendarMonth) != EXTRACT(YEAR FROM CURRENT_DATE()) AND
EXTRACT(MONTH FROM CalendarMonth) != EXTRACT(MONTH FROM CURRENT_DATE())

Would you try below ?
SELECT * FROM `table`
WHERE DATE_TRUNC(CalendarMonth, MONTH) != DATE_TRUNC(CURRENT_DATE, MONTH);

Related

Week Ending Date from Week no. - GCP

How do I get the week ending date from week no. in GCP ?
SELECT 
Session,
Store_Num,
Store_Name,
EXTRACT (WEEK(Monday) from Route_Date) as Weekno,
AVG(Planned_DPH) as Avg
FROM `TABLE` 
WHERE Route_Date  between current_date() - 60 and current_date()
and Store_Num = 8881
GROUP BY 1,2,3,4
ORDER BY Weekno
You can get the first day of weekno with PARSE_DATE function and %W format string.
%W - The week number of the year (Monday as the first day of the week) as a decimal number (00-53).
And by adding 6 more days to the first day of the week, you will get the week ending date.
Consider below sample query.
WITH sample_data AS (
SELECT DATE '2022-11-10' AS Route_Date
)
SELECT *,
PARSE_DATE('%Y%W', Year || Weekno) AS startday_of_weekno,
PARSE_DATE('%Y%W', Year || Weekno) + 6 AS lastday_of_weekno
FROM (
SELECT EXTRACT(WEEK(Monday) from Route_Date) as Weekno, -- 45
EXTRACT(YEAR FROM Route_Date) AS Year,
FROM sample_data
);
Query results

How to order date & month column correctly in Power BI?

I have a table that looks like the below photo.
table
I am trying to order Month No and Month Name correctly (starts from January), but the year and month no columns are in Whole number format.
None of the sorting methods is working.
add a calculated column and sort your table by this new column
Year&Month =
'Table'[Year] & FORMAT ( 'Table'[Month No], "00" )

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?
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

Custom Calendar for sales data aggregated by weeks and months

I am struggling with creating Custom Calendar that would allow me to use time intelligence function on data that is already aggregated by weeks and months. The original table with transactions contains over 20M rows, so for performance and space saving reasons the grouping is necessary and I perform it while querying the database in SQL Server. I want to create:
Month vs Same Month Last Year
Week vs Same Week Last Year
Year-To-Date vs Last Year Year-To-Date (by Months)
Year-To-Date vs Last Year Year-To-Date (by Weeks)
My idea was to assign some dummy dates to each row of data, and then create custom calendar that would have each of these dummy dates along with other details. I just cannot figure out the key (how to create dummy date having Year number Month number and Week number - please keep in mind that for example Week 5 of 2020 is partially in January and partially in February)
Is it possible? Maybe I have to create 2 separate Calendars, one for Weeks and one for Months?
Add calculated table:
Calendar =
GENERATE (
CALENDAR (
DATE ( 2016, 1, 1 ),
DATE ( 2020, 12, 31 )
),
VAR VarDates = [Date]
VAR VarDay = DAY ( VarDates )
VAR VarMonth = MONTH ( VarDates )
VAR VarYear = YEAR ( VarDates )
VAR YM_text = FORMAT ( [Date], "yyyy-MM" )
VAR Y_week = YEAR ( VarDates ) & "." & WEEKNUM(VarDates)
RETURN
ROW (
"day" , VarDay,
"month" , VarMonth,
"year" , VarYear,
"YM_text" , YM_text,
"Y_week" , Y_week
)
)
You can customize it. In relation pane connect your Date field of FactTable (which is by week, as you say. This table can have missing dates and duplicate dates, to be precise) to the field Date of the Calendar table (which has unique Date, by day). Then, in all your visuals or measures always use the the Date field of the Calendar table. It is a good practice to hide the field Date in your FactTable.
More explanations here https://stackoverflow.com/a/54980662/1903793

How to create a list of Months in power bi

I need to create an dynamic list of months from current month back to 10 months ago using Power Query M. I found that there is only List.Dates to create a list of date between specific duration but no function help to create the months list.
Could anybody has some way to help?
Thanks
Not sure what you are looking for, but the following code will give you 3 columns for the previous 10 months and the current month (at last refresh, you need to refresh each month):
Start of Month (date)
End of Month (date)
Name of Month in Vietnamese (text)
let
Source = Table.FromList({-10..0}, each{_}),
AddedStartOfMonth = Table.AddColumn(Source, "StartOfMonth", each Date.StartOfMonth(Date.AddMonths(Date.From(DateTime.LocalNow()),[Column1])), type date),
AddedEndOfMonth = Table.AddColumn(AddedStartOfMonth, "EndOfMonth", each Date.EndOfMonth([StartOfMonth]), type date),
AddedNameOfMonthInVietnamese = Table.AddColumn(AddedEndOfMonth, "MonthName", each Date.MonthName([StartOfMonth],"vi-VN"), type text),
RemovedColumn = Table.RemoveColumns(AddedNameOfMonthInVietnamese,{"Column1"})
in
RemovedColumn
Assuming you want to achieve a filter, I would just hit the Filter drop-down button for your date column in the Query Editor, then choose Date/Time Filters / In the Previous / 10 / months. This will generate something like this:
Filtered Rows = Table.SelectRows(Source, each Date.IsInPreviousNMonths([My Date], 10))