ApplySimple Formula for Current year in Microstrategy - oracle

I'd like to nkow how to extract the current year using an ApplySimple formula for an Oracle DB being used on microstrategy.
The formula I tried is :
ApplySimple("to_char(SYSTIMESTAMP,'Year')")
Even though this formula is a valid one - when I try using this formula to create an attribute , and display it in a report , I get no results( blank column )
What I'm essentially trying to do is compare this current year attribute to another year attribute and create afilter based on this.
Any help wll be much appreciated!

I wouldn't bother with ApplySimple at all, it can be done directly in a filter.
Qualify on the attribute form that you want to compare (presumably the Year ID), and then in the Operator section, change the dropdown from its default 'Value' to 'Custom'.
This allows you to use MicroStrategy's built-in functions in your qualification. The current year can be returned by putting:
Year(CurrentDate())
for your comparison.

Are you sure you want to compare the string "twenty fourteen"? Because, TO_CHAR(SYSTIMESTAMP,'year') would return that. Instead, you might need the YYYY format :
TO_CHAR(SYSTIMESTAMP,'YYYY')
But that is still a string.
You probably need NUMBER :
So, I would prefer, EXTRACT(YEAR FROM SYSTIMESTAMP) Because, this will return 2014 as NUMBER.
SQL> SELECT EXTRACT(YEAR FROM SYSTIMESTAMP) FROM DUAL;
EXTRACT(YEARFROMSYSTIMESTAMP)
-----------------------------
2014
Formula
If the attribute is NUMBER data type, you might need this formula :
ApplySimple("EXTRACT(YEAR FROM SYSTIMESTAMP)")

Related

Oracle : Want to convert Substring to a useable, sortable date

1st Post go easy on me.
I'm using this Substring to pull part of a Field, this date I assume is probably non Standard (ddmmmyy) - how can I enhance this command so that I can use this a sortable Date Field, I'm guessing Cast but have no idea of Syntax etc ??
SELECT SUBSTR(Host_Name,-9) as Decom_Date
Output
DECOM_DATE
31Oct2018
31May2018
31May2018
31Mar2017
31Jul2018
TIA
This is exactly what the TO_DATE function is designed for:
SELECT TO_DATE(SUBSTR(Host_Name,-9), 'DDMonYYYY') as Decom_Date
It doesn't affect you here but bear in mind that oracle dates can only store down to a second precision. Also, if you have any rogue data in the table that can't be cant be parsed as a date you'll get "not a valid..." or "a nonnumeric was found where a numeric was expected".
Be mindful that your strings here are in English but parsing MON (3 letter month name) can be regionally contextual so this code might not work on a server with a different NLS; for example consider passing 'NLS_DATE_LANGUAGE = American' as the third argument to TO_DATE if you know your strings will always be English month names

Use date picker range as a filter in a MDX query

I created a report against the demo Sales cube.
It includes a date picker with range (from & to) where the "on selection" event is named date window.
& a pivot table with a MDX query similar to this:
SELECT
[Time].[Calendar].[Day].[7 Jan 2005]
:
[Time].[Calendar].[Day].[10 Jan 2005] ON 0
FROM [Sales];
I would like to replace the fixed dates in the query by the from and to in the date picker. How to do that?
In many dialects of mdx if you have a parameter called #aDate and let us assume its current value is 7 Jan 2005 then you can add to mdx via the strToMember function like this:
strToMember('[Time].[Calendar].[Day].[' + #aDate + ']')
Or the strToSet function like this:
strToSet('[Time].[Calendar].[Day].[' + #aDate + ']:[Time].[Calendar].[Day].[' + #aSECONDDate + ']')
In icCube I'd rather use MDX function LookupByKey instead of strToMember. Besides the better typing the compiler can easily guess the hierarchy that is handy in a few scenarios. IMHO, try never using StrToMember. Something like :
[Calendar].[Day].lookupByKey( StringToDate(#date,"d/M/yyyy") )
To parse strings into dates you've a few functions available ( here and here ).
I think the range filter directly returns the MDX range expression in the event directly (contact us directly if it's not the case).
As it is mentioned above, IcCube Date Picker widget returns valid MDX value for range. Here is demo report with described configuration.
For more details check Date Picker's settings and Pivot Table's mdx settings.

Sorting behavior incorrect with FormatDate

I am editing a Webi report using SAP Business Objects BI4.
I have a report with a table in which I display a date formatted like this :
=FormatDate([Sales Date];"dd-MMM-yyyy")
Originally, Sales Date dimension has the format "mm/dd/yyyy".
I want to sort my table by this formatted date dimension in ascending order, so I just select my table > right click > select Sort and then I select the =FormatDate([Sales Date];"dd-MMM-yyyy") in ascending order.
My problem is that, the sorting behavior is incorrect as the result is as follows:
Sales Date
----------
01-AUG-2006
----------
01-JUL-2010
----------
02-FEB-2006
----------
03-AUG-2005
As you can see above, it seems that the sorting is only done by the "day" value and it completely ignores the "month" and "year".
My object is correctly defined as "Date" in the universe.
and the sorting is correctly done when I don't use the formatting "dd-MMM-yyyy" and keep the original format of Sales Date.
Any suggestions please?
Thanks!
The return data type of the FormatDate is a string. This is why the sorting will be out of order as it will sort the string value instead of the date value.
You have two options:
Format the date using a (custom) date format (In the Formatting section when you have the document in Edit mode). You'll need to use the RIA (Java applet) in order to define your own custom date format. Alternatively you can use one of the formats available. This will not change the data type but only the presentation, thus the sorting should be fine.
If you still want to use the FormatDate option, you could add the [Sales Date] in a second column, use it to sort your data and then hide the column.

OBIEE: Casting String to date then date to string

FILTER("source"."recordCount" USING "source"."snapshot_date" =
EVALUATE('TO_CHAR(%1, ''YYYYMMDD'')', TIMESTAMPADD(SQL_TSI_DAY, -7, EVALUATE('TO_DATE(%1, %2)', "source"."snapshot_date" , 'YYYYMMDD'))))
So i have this piece of code here. I know some will say "Just use the AGO function" But somehow it's causing problems because of it's connection with other tables so what I'm trying to achieve here is like a remake. The process goes this way:
The snapshot_date there is actually in varchar format and not date. So it's like "20131016" and I'm trying to change it to a date then subtract 7 days from it using the TIMESTAMPADD function and then finally returning it back to varchar to use it with FILTER.
This snippet somehow works when testing the FILTER using hardcoded values like "20131016" for example but when tested out with the code above all the row are blank. On paper, the process i assumed would happen goes lke this. "20131016" turns to a date with a format of 20131016 (yyyymmdd) and then less 7 days: 20131009 and then turned into char again "20131009" to be used in the filter.
But somehow that doesn't happen. I think the data format is not applying either to the string->date or the date->string conversion. which results to the values not getting a match at all.
Anyone have any idea what's wrong with my code?
By the way I've already tried to use CAST instead of EVALUATE or TO_TIMEDATE with the same result. Oh and this goes to the formula of the column in BMM.
Thanks
You might get some clues by looking at the SQL generated by the BI Server. I can't see any issues with your column expression, so I wouldn't limit your debugging to that alone.
A query returning nulls is often caused by incorrect levels being set (especially on logical table sources, but potentially on a measure column too). This will often result in some form of SELECT NULL FROM ... in the physical SQL.
Try this :
FILTER("source"."recordCount" USING "source"."snapshot_date" =
EVALUATE('TO_CHAR(%1, %2)', TIMESTAMPADD(SQL_TSI_DAY, -7, EVALUATE('TO_DATE(%1, %2)', TO_CHAR("source"."snapshot_date" , 'YYYYMMDD') , 'YYYYMMDD')) , 'YYYYMMDD'))

Simple Date Arithmetic in Oracle APEX

What I need to do is, in my mind, incredibly simple. So simple in fact that it is probably obvious and therefore I cannot seem to find any place that documents how to do this.
What I need is just to take the value of a Date Picker page Item (:P1_DATE_1) and through a dynamic action set the value of a second Date Picker page Item (:P1_DATE_2). The value of (:P1_DATE_2) should be 2 years, or 730 days, greater than the value of (:P1_DATE_1). So all I need is a simple '+730' expression right?
I had this working using sysdate as the start date for this calculation. The below PL/SQL expression gave the appropriate output:
to_char(sysdate + 730,'dd-MON-rr')
But then I could not get it to translate to accepting the value of the page item. I already have the actions and everything set up I just can't get it to function when I try substituting :P1_DATE_1 for sysdate. I have tried as many different manifestations of that expression using the page item but it does not populate the 2nd page item.
Sorry for the probably stupid question but if anyone could help out I would appreciate it. Thanks!
#Justin Cave is correct: all values in page items are treated as varchar2
As for your dynamic action, this is what i've set up:
Region:
Dynamic action (date 2 add):
True action:
Are you getting an error? If so, what error? Since page items in APEX are always strings, you need to convert them to dates before doing date arithmetic on them. Something like this should work assuming that P1_DATE_1 is in the DD-MON-RR format as well.
to_char( add_months( to_date( :P1_DATE_1, 'DD-MON-RR' ),
24 ),
'DD-MON-RR' )
If you're not getting an error and the second item just isn't defaulting, my guess is that you have an order of operations problem. Are you certain that the first item is actually set to a non-NULL value when the initialization code for the second item is run? If you're trying to set the value in the second item based on a value that the human selects at runtime in the first date picker, you'd either need to submit the page or you'd need a bit of Javascript.

Resources