I'm trying to create a date table in redshift with the following columns:
date
year
month
month name
year month
month name year
note that year month is in the format : '202001' and month name year is in the format: january2020
. how do I insert data in the table after I create it?
appreciate the help..
This is fairly straight forward in Redshift. A recursive CTE can be use to make the list of dates and some simple formatting can be used to get the fields you desire. This example makes all the dates from Jan 1, 2020 until today.
with recursive dates(dt) as
( select '2020-01-01'::date as dt
union all
select dt + 1
from dates d
where d.dt <= current_date
)
select dt, extract(year from dt) as year, extract(month from dt) as month,
decode(month, 1, 'January', 2, 'February', 3, 'March', 4, 'April', 5, 'May', 6, 'June', 7, 'July', 8, 'August', 9, 'September', 10, 'October', 11, 'November', 12, 'December') as mname,
year * 100 + month as yearmo, mname || year::text as monameyr
from dates order by dt;
Frankly, the easiest way to create a Calendar table is to make it in Excel!
Make a row for each date, the use formulas to add columns for day, month, year, day number, etc. You can also add booleans to identify weekends and public holidays. You could add a column for the first and last days of the month (eg to lookup the last day of a month after a given date).
Then, save the data as a CSV file and import it into the Redshift table.
Yes, you could write some fancy SQL using the generate_series() function that makes a table purely within Redshift, but it can be a little painful to write and doing it in Excel gives you a chance to add other functionality (such as public holidays).
Related
In Power BI, my ideal solution is a between slicer with date values of the form yyyy-QQ where the default (THAT CAN BE MODIFIED) is the prior quarter and extends for the next year (e.g., 2022-Q1 to 2023-Q1), like so:
I have a date table with dates, including quarters, and am open to achievable modifications on this theme, such as a start quarter and number of quarters, date slicer that defaults to quarter boundaries, etc.
Requirements:
Must use quarters
Quarters/range (including number of quarters) must be modifiable by user
Cannot use list or dropdown (my date range is over 50 years)
Thanks for both ideas and code snippets!
The solution I ended up going with was to use a slicer named "Quarters From Today." In my date table, I added a column to compute this value:
Date = ADDCOLUMNS(
CALENDAR( DATE(2018, 01, 01), DATE(2032, 12, 31) ),
"Quarters from Today", DATEDIFF(TODAY(), [Date], QUARTER) )
I then added a slicer on the Quarters from Today field:
And to assist the user, I added the dates to the chart title using MIN('Date'[Date]) and MAX('Date'[Date]):
I have a column called Snapshot Effective Period (next month) which is the Nextmonth([Snapshot Period]). This function stops when the snapshot period is 12/1/2021, what I expect to see is 1/1/2022 when the snapshot period is 12/1/2021. The value is not rolling over a year. Any advice?
Snapshot effective period next month c1 is a calculated column in date format.
Snapshot Effective Period (Next Month) c1 = NEXTMONTH('Forecast Detail'[Snapshot Period])
NEXTMONTH function should be used on Calendar
Notes In order to use any time intelligence calculation, you need a
well-formed date table. The Date table must satisfy the following
requirements:
All dates need to be present for the years required. The Date table
must always start on January 1 and end on December 31, including all
the days in this range. If the report only references fiscal years,
then the date table must include all the dates from the first to the
last day of a fiscal year. For example, if the fiscal year 2008 starts
on July 1, 2007, then the Date table must include all the days from
July 1, 2007 to June 30, 2008. There needs to be a column with a
DateTime or Date data type containing unique values. This column is
usually called Date. Even though the Date column is often used to
define relationships with other tables, this is not required. Still,
the Date column must contain unique values and should be referenced by
the Mark as Date Table feature. In case the column also contains a
time part, no time should be used – for example, the time should
always be 12:00 am. The Date table must be marked as a date table in
the model, in case the relationship between the Date table and any
other table is not based on the Date.
https://dax.guide/nextmonth/
Instead of NEXTMONTH use DATE
Snapshot Effective Period (Next Month) c1 = DATE( YEAR('Forecast Detail'[Snapshot Period]), MONTH('Forecast Detail'[Snapshot Period]) +1, DAY('Forecast Detail'[Snapshot Period]))
I have 3 slicers Year, Month, and Category from different tables joining to main table. Image below
The Year comes from the Year table, Month from the month table, category from the category table, they are used as slicers and joined to main table named pbi image below
If there is no data associated with the category then I want to display 100
Here is the Measure Code:
ACTprod =
IF ( ISFILTERED('Yr'[Year]) && ISFILTERED('Mon'[Month]) &&
ISFILTERED('Category'[Cat]),
if(HASONEVALUE(pbi[avail]), LASTNONBLANK(pbi[avail],""), 100),
"Select Year, Month& Category"
)
Here is my Data:
when I filter 2021, 2, and oracle, I get 100 which is not true, it should be 99.97
when I filter 2021, 3, and oracle, I get 99.39 which is not true, it should display 100
Can someone explain why it doesn't work? and how to make it work?
I want to add a dynamic column to a date table where the most recent month is 1 so June = 1 and it goes descending from there (please see image below).. is this possible? thank you
What do you want to achieve ?
Indeed, you could use automatic time intelligence in Power BI.
For example, you can add a calendar date table by using
=CALENDAR (DATE (2015, 1, 1), DATE (2020, 12, 31))
and then, eventually, create an index for your month :
"NMOIS"; FORMAT ( [Date]; "MM" )
OR use Power Query => add an index column
Then, order by your "Month Column" with this index.
On the Desktop, you will filter your slicer as you want.
If I have two dates e.g. 22MAR2005 to 01MAR2006 and I want to create season intervals (spring, summer, autumn, winter) based on this interval, how can this be done in a data step?
Season's are defined as:
Spring: March to May
Summer: June to August
Autumn: September to November
Winter: December to February
I need to calculate how long they spent in each season.
You need to convert that to MONTH first and then to SEASON. This exact question was asked recently so it's relatively easy to find via search (I'm assuming some course is using this as homework?).
data want;
set have;
*create month;
month_date = month(date);
*assign to season;
if month_date in (6, 7, 8) then season = 'Summer';
else if month_date in (9, 10, 11) then season ='Fall';
....etc;
run;
You could also use a format but since you're just starting out this is likely easier.
Other users question which seems really really similar:
https://communities.sas.com/t5/SAS-Enterprise-Guide/proc-glm/m-p/492142
EDIT: Use INTCK() to calculate the number of intervals.
Then use INTNX to increment across the intervals and count your days.
How you align your dates can be controlled with the first parameter to the INTNX() function. You can check the documentation for the exact specifics.
data want;
start_date="22Mar2005"d;
end_date="01Mar2006"d;
num_intervals=intck('quarter', start_date, end_date, 'C') ;
do interval=0 to num_intervals;
season_start=intnx('Month3.3', start_date, interval, 'b');
season_end=intnx('Month3.3', start_date, interval, 'e');
Number_Days=season_end - season_start + 1;
output;
end;
format start_date end_date season: yymmddd10.;
run;