Unexpected error using the sum function in a QoQ - coldfusion

While attempting to run the following code
<cfquery name="Lev1CatTotal" dbtype="query">
SELECT
SUM(AMOUNT) AS TOTAL
FROM
ChartData
</cfquery>
This is the error message that's generated:
Query Of Queries runtime error. The aggregate function [SUM(expression)] cannot operate on an operand of type [JAVA_OBJECT]
This code works fine when aggregating smaller amounts. However, these are the amount in the table I'm aggregating. This particular query sums to over $5.7B.
AMOUNT
FISCAL_YR
GOV_LEVEL1_CAT
979241575.14
2019
Charges for Services
97218277.18
2019
Charges to Other Governments
233197655.52
2019
Federal Aid
329567996.81
2019
Other Local Revenues
86957092.75
2019
Other Non-Property Taxes
158997846.75
2019
Other Real Property Tax Items
371012673.89
2019
Other Sources
346575244.01
2019
Proceeds of Debt
1145011131.99
2019
Real Property Taxes and Assessments
945308275.55
2019
Sales and Use Tax
921087680.04
2019
State Aid
107357596.20
2019
Use and Sale of Property
Just to move forward, as a workaround, I recoded this as follows:
<cfset TOTAL = 0>
<cfloop query="ChartData">
<cfset TOTAL = precisionEvaluate(TOTAL + AMOUNT)>
</cfloop>
Using precisionEvaluate(), it casts the TOTAL to BigDecimal precision and avoids the error. Does someone know of a QoQ solution using the sum() function to cast this to a big decimal and avoid using this workaround? Thanks.

Big thanks to #BernhardDöbler for getting me to look into where ChartData comes from. Since this was inherited code, I had to look into this. It turned out that ChartData was created with the following line of code.
<cfset ChartData = QueryNew("FISCAL_YR, GOV_LEVEL1_CAT, AMOUNT")>
I noticed, the original coder didn't specify any data types for his QueryNew() statement, so I modified the line of code to
<cfset ChartData = QueryNew("FISCAL_YR, GOV_LEVEL1_CAT, AMOUNT", "VarChar, VarChar, Double")>
Once I added the Double data type to the AMOUNT column, it corrected the error when I restored back to the original code of
<cfquery name="Lev1CatTotal" dbtype="query">
SELECT
SUM(AMOUNT) AS TOTAL
FROM
ChartData
</cfquery>
and I was able to remove my workaround code which performed the aggregation using a <cfloop>.

Related

DAX Doesnt Support Comparison

I have a table of Employees. The Table has both current and budgeted employees. Each employees has a salary, start date and if applicable a term date associated with them.
I am attempting for project the salary spend for each month for the next year based on the following rules:
Hire_Date <= Projected_Month
Term_Date > Projected_Month
Measure:
CALCULATE(sum(INPUT_TECH[Salary_USD]),filter(INPUT_TECH,INPUT_TECH[Hire_Date]<=MEASURE_SWB_SPEND[MEASURE_SWB_SPEND_DATE]&&INPUT_TECH[Term_Date]>MEASURE_SWB_SPEND[MEASURE_SWB_SPEND_DATE]))
The issue here is that if ther is no Term_date, the cell will be blank, and if the cell is blank, the term_date will always be less than the current projected month and therefore the answer will always be 0.
I attempted to fix this by changing the format of the termdate to always either have the number 100,000 formated to read "ACTIVE" or the date they were actually terminated
Format: [=100000]"ACTIVE";[<>100000]mm/dd/yyyy
However, I still receive the following error:
DAX comparison operations do not support comparing values of type Text with values of type Date. Consider using the VALUE or FORMAT function to convert one of the values.
I would suggest not formatting the term date to the string "Active" and then using it for comparisons in your measures. This is why you may be getting that specific error. DAX is finding a data-type issue when making the comparisons. If you want the term to show up as "Active" where term dates are blank for visualization reasons, that may be okay. But I would establish a new calculated column that determines if a term date is blank and replaces it with a date type far into the future so that dates can be compared among dates—something like:
Term Date Blank Fix =
IF(
ISBLANK( INPUT_TECH[Term_Date] ),
// The third argument below could be replaced with a hard year, ex. 2099
DATE( 12, 31, YEAR( TODAY() ) + 10 ),
INPUT_TECH[Term_Date]
)
Then you could use the above column to make your comparisons in your measures. Keep in mind that if you are using a date table with AUTOCALENDAR() that making a date with the year 2099 may wildly expand your date table.

Subtracting current date from column to show in oracle apex classic report

I have a very simple question but since i am not familiar with SQL or PL/SQL, i got no idea to do that.
In my Oracle APEX Application, I am loading data from a table into a CLASSIC REPORT through setting Local Database/SQL Query as source.
I have to make 4 columns from data of 2 columns stored in a table. I can load 3 without any issue using the below simple statement:
Select TaskName, DueDate, DueDate - 3 as ReminderDate
from table_name
Fourth column should be "RemainingDays" which equals to DueDate-current date, I have tried writing DueDate - Sys_date and DueDate - current_date in the above statement to get the fourth column but probably its not the correct way as i get error instead of all 4 columns. (I am doing in it basic excel/dax way). Any Help here?
When you subtract a date from another date, Oracle returns a number which is the number of days between the two dates.
One thing to note when using SYSDATE or CURRENT_DATE is that you may get different results if your user is not in the same timezone as the database. SYSDATE returns the current time of the database. CURRENT_DATE returns the current time of the user whatever timezone they may be in.
If possible, try building the query in a tool such as SQL Developer, get it working there, then build your Classic Report in APEX. If you are still receiving an error, please share the error you are receiving as well as the query you are using.
Example
--Start of sample data
WITH
t (task_name, due_date)
AS
(SELECT 'task1', DATE '2020-9-30' FROM DUAL
UNION ALL
SELECT 'task2', DATE '2020-9-28' FROM DUAL)
--End of sample data
SELECT task_name,
due_date,
due_date - 3 AS reminder_date,
ROUND (due_date - SYSDATE,2) AS days_remaining
FROM t;
Result
TASK_NAME DUE_DATE REMINDER_DATE DAYS_REMAINING
____________ ____________ ________________ _________________
task1 30-SEP-20 27-SEP-20 13.66
task2 28-SEP-20 25-SEP-20 11.66

Measure in DAX to calculate YTD for chosen month only for Power BI

How to construct DAX measure to calculate sum of YTD value for specific month?
Here we have FactTable grouped by months. FactTable is filled with both Actual data and Forecast data. The only way to know when Actual end is information in table [Cut of date] in column [End of YTD]. In table [Cut of date] in column [End of YTD] – it is a single value table – we have the interesting chosen month, for which we want to see the calculation of YTD. In our case it is March. FactTable is updated irregularly every month with usually one month delay. There is no way of linking it to time functions like TODAY because of irregular update.
We would like to have a correct value of YTD displayed in yellow Card Visual for the month [End of YTD]. When we click on the slicer on "2018-03" we get almost what we want – correct value of 66 in the yellow Card. However this solution is not automatic. I want to see correct value automatically when the [End of YTD] month changes, in our case to April or then to May. I do not want it done by user.
My desperate effort can be downloaded from file: DAX YTD.pbix
I pursued the deer in various ways:
By using FILTER function in DAX measures. But it seems that the
FILTER function is to harsh. It is applied to fact table first,
selecting only one month, and then calculating YTD value wrongly. So if
there would be any option for forcing order of calculation and filtering, there would
be hope.
I tried SWITCH function to display proper result
for specific month and 0 or null for other months. Although I
succeed in this, I was not able to take advantage of it. When it
came to filtering I was as hopeless as before. BTW I would be able
to make it if SWITCH produced totals at the end of the table, but it
does not. Surprisingly.
I put some hopes in RELATED function to display proper results in the [Cut off date] table. I have not walk out of the fog so far.
I would appreciate your help.
Update before bounty.
Going to higher level. I have introduced a Category column to FactTable. Please download DAX YTD by category.pbix. So filtering gets more complex now. I would like to have correct YTD figures for Apples category.
Did you use the Date column from the Calendar table, instead of the one from FactTable?
If you use the date column from FactTable, when you apply a filter on the date, it will filter on the fact records which is in March, and then do the calculation afterwards, hence the result 33.
If you use the one from Calendar, when you apply a filter on it, it filters the records on Calendar (which you want to show in the chart), so the underlying calculation will still remain intact.
A working example:
Calendar = CALENDAR(DATE(2010, 1, 1), DATE(2020, 12, 31))
I suggest you to change the calculations of the measures to avoid missing values in some cases:
Total = SUM(FactTable[Value])
MTD = TOTALMTD([Total], 'Calendar'[Date])
YTD = TOTALYTD([Total], 'Calendar'[Date])
UPDATE:
It's much clearer to me what you want to achieve now but it still seems an XY problem to me.
I understand that you want to show the dashboard as is so that users do not need to click/input every time to see what they are supposed to see. That's why I don't get why you need to create a new table to store the Cut off date (End of YTD). How is it going to be maintained automatically?
The relative date filtering solution above actually still works in the .pbix file you've shared. If you drag the Date column from the Calendar table to visual level filters for the yellow card and add the relative date filtering, it should work as below:
For the End of YTD visual, you can use the following measure to get the first day of last calendar month, so you don't need to create another table for it:
End of YTD = EOMONTH(TODAY(), -2) + 1
And hopefully this is what you want to achieve:
Updated file for your reference.
UPDATE again:
I think you'll have to write your own YTD calculation instead of using the built-in one, so that you can make use of the cut off date you defined in another table. Here I assume that you have one and only one row in 'Cut off date'[End of YTD]. Note that I've added ALL() to the filter, so that the yellow card remains the same (66) instead of showing blank when some other rows/filters are clicked:
YTD_Special =
CALCULATE(
[Total],
FILTER(
ALL(FactTable),
FactTable[Date] >= DATE(YEAR(VALUES('Cut off date'[End of YTD])), 1, 1) &&
FactTable[Date] <= VALUES('Cut off date'[End of YTD])
)
)
I would resolve this by adding a calculated column to your Calendar table to categorise each row into either "YTD" or "Other", e.g.
Is YTD =
IF (
[Date] >= DATE ( YEAR ( DISTINCT ( 'Cut off date'[End of YTD] ) ), 1, 1 )
&& [Date] <= DISTINCT ( 'Cut off date'[End of YTD] ),
"YTD",
"Other"
)
I would then add the new Is YTD field to the Visual level filters of your Card visual, and choose YTD from the Basic filtering list. The measure shown can be your simple Total measure: SUM(FactTable[Value]).
This is a far more flexible and resuable solution than any specific measure gymnastics. You will not need an explosion of measures to apply the required logic on top of every base measure - they will all just work naturally. You can apply the filter at any level: Visual, Page, Report, or put it in a Slicer for control by the end user.
I prefer to return text results e.g. "YTD" / "Other" (rather than 1/0, True/False or Yes/No), as this allows for easy extension to other requirements e.g. "Prior YTD" (1 Jan 2017 to 1 Mar 2017). It also is clearer when used in visuals.
Actually I shouldn't claim the credit for this design - this roughly follows how Cognos Transformer's Relative Time functionality worked back in the 90s.
I did something like this in my Periodic/YTD report (last sheet): http://ciprianbusila.ro/
I have used the index value of the month selected (range 1-12) and based on this I have created a measure using max function please see the code below:
ACT = var
ACT_periodic=calculate([Value],Scenarios[Scenario]=values(Scenarios[Scenario]))
var max_month=max(Periods[Period Order])
var ACT_YTD=CALCULATE([Value],Scenarios[Scenario]=VALUES(Scenarios[Scenario]),all(Periods[Month]),Periods[Period Order]<=max_month)
var myselection=if(HASONEVALUE(MRD_view[.]),values(MRD_view[.]),"PERIODIC")
return
switch(
true(),
myselection="PERIODIC",ACT_periodic,
myselection="YEAR TO DATE",ACT_YTD,
ACT_periodic
)

Sharepoint 2010 Task list: Due date = Start date + 1 month

I want to auto-fill the Due date column by taking the Start date column and add 1 month to it.
This formula I've tried in different ways:
=DATE([Start Date]+[Gap],"dd-mm-yyyy") (I created Gap column as number type and filled it with 28 days; in our language, dd-mm-yyyy seems to be the usual way of showing dates in Sharepoint).
Sharepoint keeps on giving back errors.
Try this formula:
=DATE(YEAR([Start Date]);MONTH([Start Date])+[Gap];DAY([Start Date]))
where [Gap] value is integer.
Note: pay attetion to delimiters - they are locale-dependent, most probably you need to replace , with ;

ColdFusion: how to check 15 days past the submitted date?

I saved several accounts in the database. I want to email the accountsdd that have been expired for 15 days. In another word, if the submitted day (date format: mm-dd-yyyy) is 15 days older from today's date then send an email. How do I do it? Any info is greatly appreciated. Thank you.
you would want to use the dateDiff function
<cfif dateDiff('d',submittedDate,now()) GT 15>
If your date is really stored as mm-dd-yyyy instead of a date/time object then you would need to use the createODBCDate function
<cfif dateDiff('d',CreateODBCDate(submittedDate),now()) GT 15>
If you're looking to pull all accounts via a query this would work. This would work on MSSQL
SELECT relevant, columns
FROM myTable
WHERE dateDiff(d,submittedDate,getDate()) > 15
Without more details (database type, data types, etectera) this is just an educated guess. But it sounds like you are asking how to find all records that were submitted exactly fifteen days ago, regardless of the time. For example if the date and time now is July 29, 2013 08:49 AM, you want to retrieve all records submitted on July 14, 2013 (any time between 12 midnight and 11:59:59 PM).
A query with a simple range comparison should do the trick. Note: While Matt's suggestion of dateDiff (or your database's version of it) would also work, using functions in that manner often prevent the database from leveraging indexes. So it is preferable to use a more index friendly expression, like:
<cfset fifteenDaysAgo = dateAdd("d", -15, now())>
<cfquery ...>
SELECT emailAddress
FROM yourTable
WHERE submittedDate >= <cfqueryparam value="#fifteenDaysAgo#" cfsqltype="cf_sql_date">
AND submittedDate < <cfqueryparam value="#dateAdd('d', 1, fifteenDaysAgo)#" cfsqltype="cf_sql_date">
</cfquery>
Then it is just a matter of using the query results to send out your emails.