How to filter using calculated date value based on parameter in MDX - sharepoint

I'm trying to fetch the values based on a Date value passed as parameter (named AsOnDate) into a Performance Point report's MDX from a Webpart Date Filter. It's an Accounts database and the scenario is to fetch the Dormant accounts data. The problem I'm facing is that I need to fetch the Accounts having Dormant Date till 6 months before the AsOnDate value, i.e. AsOnDate -6 months, I managed to calculate the 6 month old date using DateAdd() function, like following:
DateAdd('m', -6, DateValue('<<AsOnDate>>'))
Now, I cann't figure out how to pass this value in place of the paramter in WHERE clause, MDX query looks like:
WITH
SET [Products] AS { <<Product>> }
MEMBER [Measures].[Dormant A/C Count] AS ([Measures].[Account Head Count])
MEMBER [Measures].[Dormant A/C Vol.] AS ([Measures].[LC Balance])
MEMBER [AH].[Subhead - Description].[Totals] AS Aggregate([Products])
SELECT
{ [Measures].[Dormant A/C Count], [Measures].[Dormant A/C Vol.]}
ON COLUMNS,
{[Products], [AH].[Subhead - Description].[Totals]}
ON ROWS
FROM
[MyCUbe]
WHERE (
[AH].[Is Dormant].&[1],
[AH].[Is Inoperative].&[0],
{NULL : [AH].[Dormant Date].&[ DateAdd('m', -6, DateValue('<<AsOnDate>>')) ]}
)
I get this error:
Error running data source query.
The 'DateAdd('m', -6, DateValue('2012-12-03'))'
string cannot be converted to the date type.
I have tried StrToMember() like:
WHERE (
[AH].[Is Dormant].&[1], [AH].[Is Inoperative].&[0],
{ NULL : StrToMember("[AH].[Dormant Date].&["+DateAdd('m', -6, DateValue('<<AsOnDate>>'))+"]")}
)
However, passing the Date Filter value directly as following works fine:
WHERE ( [AH].[Is Dormant].&[1], [AH].[Is Inoperative].&[0],
{ NULL : [AH].[Dormant Date].&[<<AsOnDate>>T00:00:00]} )

Figured it out, after some hit n trials.. managed to hack a combination of StrToMember() and Format() to convert calculated Date into string and feed it into MDX as Member, here's the working WHERE clause:
WHERE ( [AH].[Is Dormant].&[1], [AH].[Is Inoperative].&[0],
{ NULL : StrToMember("[AH].[Dormant Date].&[" + Format(DateAdd('m', -6, DateValue('<<AsOnDate>>')), 'yyyy-MM-dd') + "T00:00:00]") } )
Hope it will prove helpful for someone out there..

Related

Hybris ImpEx - how to export all Orders, which contain a specific coupon code

I want to export all items of type Order, which contain a coupon code named 'TESTCOUPON'. However, when I try to do it, I get this error:
ERROR line 4 at main script: error executing code line at 4 : SQL search error - ORA-00932: inconsistent datatypes: expected - got BLOB
query = 'SELECT item_t0.PK FROM orders item_t0 WHERE ( item_t0.p_appliedcouponcodes = 'TESTCOUPON') AND (item_t0.TypePkString IN (?,?,?) )', values = [8796099149906, 8796099215442, 8796098756690]
I assume from the error that the coupon is stored in a Collection/List - how would I filter by it in this case?
The FlexibleSearch line looks like this:
SELECT {C:PK} FROM {Order as C} WHERE {C:appliedCouponCodes} = 'TESTCOUPON'
I tried using the PK of the coupon code, but it doesn't work either and it presents the same error. I tried using LIKE '%TESTCOUPON%' but then it said 'expected CHAR got BLOB'.
As per the Table structure CouponRedemption having reference of Order
.
so correct query will be as below.
select { o.code },{o.pk},{cr.couponCode} from {Order as o Join CouponRedemption as cr on {cr.order}={o.pk}} where {code}='TESTCOUPON'
Output:
code PK p_couponcode
1050156308 9499773075501 BUY4
1044303645 9499775172653 BUY4
1042057811 9499796897837 BUY4
1049853832 9499798863917 BUY4

How truncate time while querying documents for date comparison in Cosmos Db

I have document contains properties like this
{
"id":"1bd13f8f-b56a-48cb-9b49-7fc4d88beeac",
"name":"Sam",
"createdOnDateTime": "2018-07-23T12:47:42.6407069Z"
}
I want to query a document on basis of createdOnDateTime which is stored as string.
query e.g. -
SELECT * FROM c where c.createdOnDateTime>='2018-07-23' AND c.createdOnDateTime<='2018-07-23'
This will return all documents which are created on that day.
I am providing date value from date selector which gives only date without time so, it gives me problem while comparing date.
Is there any way to remove time from createdOnDateTime property or is there any other way to achieve this?
CosmosDB clients are storing timestamps in ISO8601 format and one of the good reasons to do so is that its lexicographical order matches the flow of time. Meaning - you can sort and compare those strings and get them ordered by time they represent.
So in this case you don't need to remove time components just modify the passed in parameters to get the result you need. If you want all entries from entire date of 2018-07-23 then you can use query:
SELECT * FROM c
WHERE c.createdOnDateTime >= '2018-07-23'
AND c.createdOnDateTime < '2018-07-24'
Please note that this query can use a RANGE index on createdOnDateTime.
Please use User Defined Function to implement your requirement, no need to update createdOnDateTime property.
UDF:
function con(date){
var myDate = new Date(date);
var month = myDate.getMonth()+1;
if(month<10){
month = "0"+month;
}
return myDate.getFullYear()+"-"+month+"-"+myDate.getDate();
}
SQL:
SELECT c.id,c.createdOnDateTime FROM c where udf.con(c.createdOnDateTime)>='2018-07-23' AND udf.con(c.createdOnDateTime)<='2018-07-23'
Output :
Hope it helps you.

MDX Calculated Member displays no value in Excel but same query works in SSMS

I have a calculated member (not measure) of a dimension, which works in SSMS, but doesn't work in Excel 2013.
I've followed the advice described in this question on how to create calculated members.
So I have a new attribute in my lottery dimension called 'Relative Lotteries' with a single member 'All Lotteries in Scope' defined through the DSV.
I have a calculated member like this:
CREATE MEMBER CURRENTCUBE.[Lottery].[Relative Lotteries].[All].[This Lottery]
AS NULL, VISIBLE = 1;
Then a SCOPE Statement like this:
SCOPE ([Lottery].[Relative Lotteries].[All].[This Lottery]
,[Lottery].[Lottery Number].members);
THIS = 10000;--([Lottery].[Lottery Number].currentmember,measures.currentMember);
END SCOPE;
I've tried loads of combinations of SCOPE and THIS = , most of which work through SSMS but none work through Excel 2013.
I used OLAP extensions to get the MDX script generated by Excel 2013, The MDX is:
SELECT
{ [Measures].[Response - Sale Amt], [Measures].[Response - Qty of Orders] } DIMENSION PROPERTIES PARENT_UNIQUE_NAME
, HIERARCHY_UNIQUE_NAME ON COLUMNS
, NON EMPTY Hierarchize (
{
DrilldownLevel (
{ [Lottery].[Relative Lotteries].[All] }
,
,
, INCLUDE_CALC_MEMBERS
)
}
) DIMENSION PROPERTIES PARENT_UNIQUE_NAME
, HIERARCHY_UNIQUE_NAME ON ROWS
FROM [Lottery]
WHERE ( [Lottery].[Lottery Number].&[141] ) CELL PROPERTIES VALUE
, FORMAT_STRING
, LANGUAGE
, BACK_COLOR
, FORE_COLOR
Through SSMS this query yields the expected result:
But through Excel 2013, I get this result:
The only thing I can think of is that its something to do with the Excel connection string. However its difficult to test. Excel won't let me remove or add any properties, it just returns the connection string back to how it was before editing. My Excel Connection string is:
Provider=MSOLAP.6;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=Cube_Name;Data Source=ServerName;MDX Compatibility=1;Safety Options=2;MDX Missing Member Mode=Error
I've been banging my head against a brick wall trying to work this out. Any ideas ???

MDX return fiscal PriorMTD date as value

I am trying to create MDX Calculated member which returns prior mtd date.
This is Calculated member I've created:
CREATE MEMBER CURRENTCUBE.[Measures].PriorMTDDate
AS cousin(
[Date].[Fiscal].CurrentMember,
[Date].[Fiscal].CurrentMember.parent.parent.lag(1)
),
VISIBLE = 1 ;
And this is query, but it returns just null:
select {[Measures].[PriorMTDDate]} on 0
from [WH_Cube]
WHERE ( [Date].[Fiscal].[Date].&[2014-09-12T00:00:00] )
Any idea what am I doing wrong?
EDIT: Another example returning null:
WITH MEMBER Measures.x AS
[Date].[Fiscal].CurrentMember
SELECT Measures.x ON 0
FROM [WH_Cube]
WHERE ( [Date].[Fiscal].[Date].&[2014-09-30T00:00:00] )
Does a measure need to be a numeric value?:
CREATE MEMBER CURRENTCUBE.[Measures].PriorMTDDate
AS cousin(
[Date].[Fiscal].CurrentMember,
[Date].[Fiscal].CurrentMember.parent.parent.lag(1)
).MemberValue ,
VISIBLE = 1 ;
.CurrentMember is evaluated at the row level, doesn't look into the slicer. The slicer is a global restriction on the cube, providing a sub-cube domain for your query.
In your query, [Date].[Fiscal].CurrentMember is underfined, as there is nothing on the Rows clause.
Try
select {[Measures].[PriorMTDDate]} on 0,
[Date].[Fiscal].[Date].&[2014-09-12T00:00:00] on 1
from [WH_Cube]

Using TopCount in MDX query

I'm trying to select the top 10 of a specified data set for use in a report. However, I'm not sure where it would go in the query. My current MDX query is below and mostly resembles what is automatically generated by the designer. What I'm trying to get is the top 10 Subcontractors by the value of Revised Value. Currently when I try to run this I get the error "Error running the data source query"
SELECT
TopCount([Dim Subcontractor].[Subcontractor Name].[Subcontractor Name].ALLMEMBERS, 10, [Measures].[Revised Value]),
{ [Dim Subcontractor].[Subcontractor Name].[Subcontractor Name].ALLMEMBERS }
ON COLUMNS,
{ [Measures].[Revised Value] }
ON ROWS
FROM [BGDEMO]
WHERE ( [Dim Project].[Project Name].DEFAULTMEMBER, [Dim Date].[Full Date].DEFAULTMEMBER )
CELL PROPERTIES VALUE, FORMATTED_VALUE, CELL_ORDINAL, FONT_FLAGS, FORE_COLOR, BACK_COLOR
Any help with this is appreciated.
Figured it out. I'll post it here in case others might find it useful:
SELECT
{ TOPCOUNT([Dim Subcontractor].[Subcontractor Name].[Subcontractor Name],10, [Measures].[Revised Value]) }
ON COLUMNS,
{ [Measures].[Revised Value] }
ON ROWS
FROM [BGDEMO]
WHERE ( [Dim Project].[Project Name].DEFAULTMEMBER, [Dim Date].[Full Date].DEFAULTMEMBER )
CELL PROPERTIES VALUE, FORMATTED_VALUE, CELL_ORDINAL, FONT_FLAGS, FORE_COLOR, BACK_COLOR

Resources