Declare Parameters in Teradata query OR If-then-else - if-statement

I am new to Teradata. Here is the situation:
If Current_date is < 15 of month then ?Startdate parameter = 1st of prev month and ?EndDate param = last date of current month
SELECT ... FROM ViewA
WHERE date BETWEEN ?Startdate AND ?EndDate
ELSE if Current_date >= 15 then Startdate parameter = 1st of Current month and EndDate param = last date of current month
SELECT ... FROM ViewA
WHERE date BETWEEN ?Startdate AND ?EndDate
I was able to calculate dates in each case, but can't figure out:
#1) how to put them in parameters. If I can put them in params then all I have to do is
SELECT ... FROM ViewA
WHERE date BETWEEN ?Startdate AND ?EndDate
OR
#2) Write a IF-THEN-ELSE
If Current_date is < 15 of month
SELECT ... FROM ViewA
WHERE Date BETWEEN
ADD_MONTHS((DATE - EXTRACT(DAY FROM DATE)+1), -1) AND ADD_MONTHS(LAST_DAY(DATE),0)
ELSE
SELECT ... FROM ViewA
WHERE date BETWEEN
ADD_MONTHS((DATE - EXTRACT(DAY FROM DATE)+1), 0) AND ADD_MONTHS(LAST_DAY(DATE),0) )
END IF
Please guide how to achieve #1 or #2. I CANNOT create function or stored procedure in teradata

#2 using calendar as an example.
SELECT calendar_date from Sys_Calendar.CALENDAR
WHERE calendar_date
BETWEEN
case when extract (day from current_date) < 15
then ADD_MONTHS((current_date - EXTRACT(DAY FROM current_date)+1), -1)
else current_date - EXTRACT(DAY FROM current_date)+1
end
AND ADD_MONTHS(LAST_DAY(current_date),0)
order by calendar_date
;
I prefer the current_date, can't be confused with the datatype.

WHERE date
BETWEEN Trunc(current_date - 14 , 'mon') -- first day of the month 14 days before today
AND last_day(current_date) -- last day of current month

Related

Compare todays data against end of month of the previous month DAX

Lets just say that I want to compare todays data (3/10/2021) against the data of the last day of the previous month (2/26/2021), I am using February 26 because is the last business day of the previous month, Is there a Way to do this?
We can get the last day of February that is not a weekend (DAX doesn't have holidays, because each country can have its own free days).
calcForLastDayPrevMonth_noWeekend =
var __firstOfMonth = DATE( YEAR(TODAY()), MONTH(TODAY()), 1)
var __lastDayPrevMonth = calculate( MAX(DateTable[Date]), FILTER(ALL(DateTable), DateTable[Date] < __firstOfMonth && WEEKDAY(DateTable[Date]) not in {1,7} ))
return
calculate( sum(Table[SomeVal]), Table[date] = __lastDayPrevMonth )

Output value from Pervious Month (December) in January

Hopefully someone can help me.
I use the measure below to output a value from the previous month, however it appears to have broken this month (January).
In March it outputs Februarys value, and in April it outputs Marchs value. However as it is January it doen't appear to be outputting Decembers value.
Can anyone advise how I would adapt this measure to retrieve Decembers value?
Previous Month = var current_month= MONTH(TODAY()) return CALCULATE( AVERAGE('DATA_TABLE'[VALUE]), FILTER(DATA_TABLE,MONTH(DATA_TABLE[DATE])=current_month-1))
Thanks for your support.
Dax's Month commmand returns a number. In your case, you're asking for month 0, as january is month number 1. Since there is no month numbered as 0, it returns no data (blank).
You just need to add a condition for when the month is 0, to instead return data for the previous year's last month.
This is a possible solution:
VAR current_month = MONTH(TODAY())
VAR current_year = YEAR(TODAY())
VAR previous_month = current_month - 1
VAR previous_year = current_year - 1
RETURN
CALCULATE(
AVERAGE('Table'[Values]);
FILTER('Table';
IF(previous_month = 0;
MONTH('Table'[Date]) = 12 && YEAR('Table'[Date]) = previous_year;
MONTH('Table'[DATE]) = current_month - 1)
)
)
This way you accomodate for the 0 month value and filter according to it.
If it is not 0, the measure behaves as before. Otherwise it filters for the last month of the previous year.

Calculate Number of Working Days based on a Month and Year Column - DAX

I have a column like below for which I would want to extract the number of working days (excluding just weekends- Saturday and Sunday, holidays needs not be addressed).
As of now I just want:
Required_Column = Total No of Days in that month - No of weekends in that month
Month_Year
01-2018
02-2018
03-2018
...
...
01-2019
02-2019
I am a newbie in Power query and DAX , i tried looking up various methods using DAX however, could not find any relevant lead.
Expected Output:
Month_Year Required_Column
01-2018 23 (31-8)
02-2018 20 (28-8)
03-2018 22 (31-9)
... ...
... ...
01-2019 23 (31-8)
02-2019 20 (28-8)
Appreciate your help on this.
Although a Calendar table based approach is recommended as in the comment by RADO and Strawberryshrub, it is also possible to do this with DAX calculated column.
In the example below, I'm assuming MonthYear column contains the first day of each month.
WorkingDays =
VAR Year = YEAR( [MonthYear] )
VAR Month = MONTH( [MonthYear] )
VAR DatesInMonth = GENERATESERIES( [MonthYear], DATE( Year, Month + 1, 1 ) - 1, 1 )
RETURN SUMX(
DatesInMonth,
IF( WEEKDAY( [Value] ) IN { 1, 7 }, 0, 1 )
)

Redshift function syntax

I have study this over and over, without figuring out what is wrong I feel like the syntax for this function is right
I have tried to take single quotes out of plpgsql, triple checked for commas, added body to the dollar signs with no luck, I can't figure out why this i erroring. returns table should be the correct syntax right?
CREATE OR REPLACE FUNCTION fn_ytd_costs_of_business (param1 date)
RETURNS TABLE (year INTEGER, month TEXT , revenue FLOAT8 ,transactiondate TIMESTAMP , Flag varchar(28),)
AS
$$
BEGIN
select
extract(year from transactiondate) as year
, to_char(transactiondate, 'Mon') as month
--, extract(month from transactiondate) as month_number
, sum(netamount) as revenue
,transactiondate
,Flag
from
vw_costs_of_businesss_copy a
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(transactiondate, 'YYYY-MM-DD') ->= concat(to_char(extract(year from '2019-01-01'), 'YYYY'),'01-01') --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(extract(year from to_date('2019-01-01', 'YYYY-MM-DD'))),'01-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(Cas(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as text ),'-01-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(Cast(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as Text),'0000'),'-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
where to_date(transactiondate, 'YYYY-MM-DD') <= $1 and to_date(transactiondate, 'YYYY-MM-DD')>= to_date(concat(to_char(Cast(extract(year from to_date($1, 'YYYY-MM-DD')-1) as Text),'0000'),'-01') , 'YYYY-MM-DD')
group by
year
, month
,transactiondate
, Flag;
on a.
--order by year;
END;
$$
LANGUAGE plpgsql;
this is the error I am receiving:
RETURNS TABLE (year INTEGER, month TEXT , revenue FLOAT8 ,transactiondate TIMESTAMP , Flag varchar(28),)
The error is probably because TABLE is not a valid RETURN type.
However, this is unimportant because an Amazon Redshift User-Defined Function is not allowed to SELECT data from tables. It can only operate on the data that is passed into the function.
See: UDF Constraints - Amazon Redshift
Depending upon your use-case, a Stored Procedure might be suitable. It would not "return" a table, but it could CREATE INTO a table.

how to select last day (max day) of each month using dax

I have the following table:
I need to make a measure to return the values of "last day", but I cant use EOMONTH, because I have the current month, and the current month doesnt end yet, so, the last day of current month is today.
You can use EOMONTH with a bit of extra logic:
LastDay =
VAR CurrDate = MAX(Table1[Date])
RETURN CALCULATE(MAX(Table1[Date]),
FILTER(ALL(Table1),
Table1[Date] > EOMONTH(CurrDate, -1) &&
Table1[Date] <= EOMONTH(CurrDate, 0)))
This takes the max of the dates you have that occur between the end of the previous month and the end of the current month.
Once you have this measure, you can use it to calculate the sum of Value:
Last Date Sum = CALCULATE(SUM(Table01[Value]),
FILTER(Table01, Table01[Date] = [LastDay]))