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.
Related
Hi i have an issue i cant fix in PowerBI i dont understand DAX that mutch.
I have a half solution in DAX and an example what i have tryed.
I have the solution in SQL.
WANTED RESULT
I need to get the "time" result summed up
that have values between the two selected values.
IMPORTANT if one rows of values starts before AND after the selected values then the operation was active that time and shall be included.
#sdate = '2020'
#sdate = '2021'
Select *
From #temp
where (datepart(year,startdate) <= #sdate and datepart(year,enddate))
or (startdate between #sdate and #edate)
or (enddate between #sdate and #edate)
If i do it in SSMS i get the right rows
But in PowerBI i have some issue
I need to be able to choose två year
Year from startdate
Year from enddate
This part work but not the full solution
I just get row 3 and 4 as it sould of this solution.
UPDATED
NOW I DONT EVEN GET THIS PART RIGHT
I want all the green to include and exclude the red
Antal (under året) =
var SelectedYearStart = CONVERT(SELECTEDVALUE(TEST[startdate].[Year]), INTEGER)
var SelectedYearEnd = CONVERT(SELECTEDVALUE(TEST[enddate].[Year]), INTEGER)
return CALCULATE(SUM(TEST[time]),ALLCROSSFILTERED(TEST),year(TEST[startdate])<=SelectedYearStart , year(TEST[enddate])>=SelectedYearEnd)
My guess was this
Antal (under året) =
var SelectedYearStart = CONVERT(SELECTEDVALUE(TEST[startdate].[Year]), INTEGER)
var SelectedYearEnd = CONVERT(SELECTEDVALUE(TEST[enddate].[Year]), INTEGER)
return CALCULATE(SUM(TEST[time]),ALLCROSSFILTERED(TEST),year(TEST[startdate])<=SelectedYearStart , year(TEST[enddate])>=SelectedYearEnd || DATESBETWEEN(TEST[startdate],SelectedYearStart,SelectedYearEnd || DATESBETWEEN(TEST[enddate],SelectedYearStart,SelectedYearEnd)))
But then i get this error
TEST DATA
declare #sdate nvarchar(4)
declare #edate nvarchar(4)
set #sdate = '2020'
set #edate = '2021'
select #sdate sdate
select #edate edate
DEclare #temp table (time decimal(18,2) , startdate date, enddate date)
INSERT INTO #temp
SELECT 5.0,'2019-01-01','2020-12-01' union all --
SELECT 5.0,'2021-01-01','2022-12-01' union all --
select 5.0,'2020-01-01','2021-12-01' union all
select 5.0,'2019-01-01','2022-12-01' union all --
select 5.0,'2019-01-01','2019-12-01' union all --
select 5.0,'2022-01-01','2022-12-01' union all
select 5.0,'2020-01-01','2020-12-01' union all
select 5.0,'2021-01-01','2021-12-01'
--select 5.0,'2020-01-01','3000-01-01' --EXTRA
SELECT *
into TEST
FROM #temp
--ORDER BY startdate,enddate
You need something like this:
var SelectedYearStart = SELECTEDVALUE(TEST[startdate].[Year])
var SelectedYearEnd = SELECTEDVALUE(TEST[enddate].[Year])
return CALCULATE(SUM(TEST[time]),
ALL ( TEST[startdate].[Year]),
ALL ( TEST[enddate].[Year]),
KEEPFILTERS(
(Year(TEST[startdate]) <= SelectedYearStart &&
Year(TEST[enddate]) >= SelectedYearEnd)
|| (Year(TEST[startdate]) >= SelectedYearStart &&
Year(TEST[startdate]) <= SelectedYearEnd)
|| (Year(TEST[enddate]) >= SelectedYearStart &&
Year(TEST[enddate]) <= SelectedYearEnd))
)
First you need to clear the filters on the table that are created via the two slicers (startdate.Year and enddate.Year)
Then you need to pass in your complex filter query. Note that I'm using the Year(..) function because you can't reference columns from different tables in the filter section of the Calculate (and startdate.year and enddate.year are coming from two separate date tables created automatically by powerbi).
Finally you need to wrap that into a KEEPFILTERS statement to ensure that only the current row context is being applied to the expression.
I have a column named status compared between the End date of the task and each month in the year to return 1 for true, 0 for false
i have used this:
Status = IF ([EndDate].[Date] <= [Month].[Date] , 1, 0)
Date Formatting for [EndDate] and [Month]: March 2001 (MMMM yyyy)
The problem : if End Date = May 2019 and Month = May 2019 its returned 0 ,but its should returned 1
Actual Result
Expected Result
That is the correct logic/syntax, is the date you are using just visually formatted as MMMM yyyy, is the actual date in the dd/mm/yyyy format? as you may be trying to compare the following
[EndDate].[Date] = 01/05/2001 to [Month].[Date] 25/05/2001
What you can do is just pull out the month and year for example
MONTH([EndDate].[Date]) and YEAR([EndDate].[Date]) and MONTH([Month].[Date]) and YEAR( [Month].[Date])
So it would be IF(YEAR([EndDate].[Date]) = YEAR( [Month].[Date]) && MONTH([EndDate].[Date]) = MONTH([Month].[Date]), 1, 0)
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]))
I would like to show the card values:
if current month value is not available then it should compare with the previous month value automatically.
Like
In above image Apr-2017 value is not available then it should compare with the Mar-2017 value.
If Mar-2017 value also not available then the previous month value.Keep on goes.
And one more thing is I don't want to use the slicer anymore. Actually I am not going to use the slicer.
I just want to show the month comparison value in a card visuals.
I would like to give info of what kind of data that I have. I have table's plan rev, actual rev, and MonthYear
I have created a relationship using Date column from MonthYear table to plan rev and actual rev Date columns.
MonthYear table
And in plan rev, month year, plan rev, date, and in actual rev actual rev, month year, and date columns.
Then written measures for
For Sumof Plan rev-->
PlanSum = SUM('Revenue Report'[Planned Rev])
For Prev month value-->
PlanPrevMon = CALCULATE([PlanSum],PREVIOUSMONTH('Month Year'[Date]))
For Start and End Date of the months
FILTERDATESTART= FIRSTDATE('MonthYear'[Date])
FILTERDATEEND= LASTDATE('MonthYear'[Date])
Then for current month PlanArrows measure is.
PlanArrows = CALCULATE(
IF(
ISBLANK([PlanSum]),"No data Available",[PlanSum]
)& " "& IF(
[PlanSum]=[PlanPrevMon],
"",
IF([PlanSum]>[PlanPrevMon],
UNICHAR(8679),
UNICHAR(8681)
) & IF([PlanSum]<=0,
"",""
)
),
'Month Year'[Date]>=FILTERDATESTART,
'Month Year'[Date]<=FILTERDATEEND
)
But it is not working. I'm getting an error:
A function 'CALCULATE' has been used in a True/False expression that is used as a table filter expression. This is not allowed.
Any Suggestions please,
Thanks
Mohan V
I think i got the solution on my own.
I have written measure which solved this issue.
PlanArrows =
VAR s = [FILTERDATESTART]
VAR e = [FILTERDATEEND]
RETURN
CALCULATE (
IF ( ISBLANK ( [PlanSum] ), "No data Available", [PlanSum] ) & " "
& IF (
[PlanSum] = [PlanPrevMon],
"",
IF ( [PlanSum] > [PlanPrevMon], UNICHAR(8679), UNICHAR(8681) )
& IF ( [PlanSum] <= 0, "", "" )
),
'Month Year'[Date] >= s
&& 'Month Year'[Date] <= e
)
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