MDX: Create a set excluding entries from a different hierarcy - ssas

I am trying to create a custom set which excludes entries from another hierarchy, and I can't use the except function because of this. I have tried this, but I get an error message saying that the hierarchy is shown more than once in the query. Could someone please advise me on how to do this in the best way?
WITH
SET [Market1] AS {
[Market].[Market].[Market].&[103],
-([Agency].[Nationality - Consortium - Agency].[Nationality].&[111],
[Agency].[Nationality - Consortium - Agency].[Nationality].&[116],
[Agency].[Nationality - Consortium - Agency].[Nationality].&[242],
[Agency].[Nationality - Consortium - Agency].[Nationality].&[134])
}
EDIT: Here is my query (most of it is built from a query builder)
WITH
SET [Market1] AS SUM((
[Market].[Market].[Market].&[103],
EXCEPT({[Agency].[Nationality - Consortium - Agency].[Nationality].ALLMEMBERS},
{[Agency].[Nationality - Consortium - Agency].[Nationality].&[111],
[Agency].[Nationality - Consortium - Agency].[Nationality].&[116],
[Agency].[Nationality - Consortium - Agency].[Nationality].&[242],
[Agency].[Nationality - Consortium - Agency].[Nationality].&[134]}
)), [Measures].[Price])
SELECT NON EMPTY [Market1] ON ROWS
FROM ( SELECT ( { StrToMember("[Report Date].[Report Date].&[" + Str(Int(CDbl(Now()) - 38718)) + "]") } ) ON COLUMNS
FROM ( SELECT ( { [Travel Type].[Travel Type].&[101],
[Travel Type].[Travel Type].&[102],
[Travel Type].[Travel Type].&[103] } ) ON COLUMNS
FROM ( SELECT ( { [Departure Date].[Year].&[2017] } ) ON COLUMNS
FROM [Booking])))
WHERE ( [Departure Date].[Year].&[2017],
[Travel Type].[Travel Type].CurrentMember,
StrToMember("[Report Date].[Report Date].&[" + Str(Int(CDbl(Now()) - 38718)) + "]") )
EDIT2: Managed to hack it together
WITH
SET [Market1] AS (
[Market].[Market].[Market].&[103],
EXCEPT({[Agency].[Nationality - Consortium - Agency].[Nationality].ALLMEMBERS},
{[Agency].[Nationality - Consortium - Agency].[Nationality].&[111],
[Agency].[Nationality - Consortium - Agency].[Nationality].&[116],
[Agency].[Nationality - Consortium - Agency].[Nationality].&[242],
[Agency].[Nationality - Consortium - Agency].[Nationality].&[134]}
))
MEMBER Agg AS SUM
( [Market1], [Measures].[Price]
)
SELECT NON EMPTY Agg ON COLUMNS
FROM ( SELECT ( { StrToMember("[Report Date].[Report Date].&[" + Str(Int(CDbl(Now()) - 38718)) + "]") } ) ON COLUMNS
FROM ( SELECT ( { [Travel Type].[Travel Type].&[101],
[Travel Type].[Travel Type].&[102],
[Travel Type].[Travel Type].&[103] } ) ON COLUMNS
FROM ( SELECT ( { [Departure Date].[Year].&[2017] } ) ON COLUMNS
FROM [Booking])))
WHERE ( [Departure Date].[Year].&[2017],
[Travel Type].[Travel Type].CurrentMember,
StrToMember("[Report Date].[Report Date].&[" + Str(Int(CDbl(Now()) - 38718)) + "]") )

You cannot do it this way. You want to limit data from different dimensions, so you have to use tuple:
WITH
SET [Market1] AS (
[Market].[Market].[Market].&[103],
EXCEPT([Agency].[Nationality - Consortium - Agency].[Nationality].ALLMEMBERS,
{([Agency].[Nationality - Consortium - Agency].[Nationality].&[111],
[Agency].[Nationality - Consortium - Agency].[Nationality].&[116],
[Agency].[Nationality - Consortium - Agency].[Nationality].&[242],
[Agency].[Nationality - Consortium - Agency].[Nationality].&[134]}
)

Related

Sequelize complex aggregate attribute in where and order

Hello I'm trying to use Sequelize to perform location based queries, and I'm having a nightmare!
I'm trying to generate the SQL:
SELECT *, (3959 * acos(cos(radians([user-latitude])) * cos(radians(latitude)) * cos(radians(longitude) - radians([user-longitude])) + sin(radians([user-latitude])) * sin(radians(latitude)))) AS distance FROM myModel HAVING distance <= 25 ORDER BY distance ASC LIMIT 0 , 10;
where [user-latitude] and [user-longitude] are variables. So far I've got this:
myModel.findAll({
attributes: [
'*',
[`(3959 * acos(cos(radians(${user-latitude})) * cos(radians(latitude)) * cos(radians(longitude) - radians(${user-longitude})) + sin(radians(${user-latitude})) * sin(radians(latitude))))`, 'distance'],
],
where: {
distance: {
$lte: 25,
},
},
order: [
['distance', 'ASC'],
],
limit: 10,
offset: 0,
});
which generates:
SELECT *, (3959 * acos(cos(radians([user-latitude])) * cos(radians(latitude)) * cos(radians(longitude) - radians([user-longitude])) + sin(radians([user-latitude)) * sin(radians(latitude)))) AS `distance` FROM `myModels` AS `myModel` WHERE `myModel`.`distance` <= 15 ORDER BY `myModel`.`distance` ASC LIMIT 0, 10;
which doesn't work because `myModel`.`distance` is not a field. Is there a way to make this work without using raw queries?
Unfortunately you cannot use alias field in the WHERE or HAVING statement, only in the ORDER BY. You must repeat your statement in the WHERE clause instead using alias (just as it is explained in SQL Use alias in Where statement).
What is more, the error you obtained happens because when you used field distance in the where and order attributes, Sequelize automatically treated it as a field of myModel instead your alias, so you need to write it literally so it won't be treated as a column of table you select.
myModel.findAll({
attributes: {
include: [[`(3959 * acos(cos(radians(${user-latitude})) * cos(radians(latitude)) * cos(radians(longitude) - radians(${user-longitude})) + sin(radians(${user-latitude})) * sin(radians(latitude))))`, 'distance']]
},
where: sequelize.where(
sequelize.literal(`(3959 * acos(cos(radians(${user-latitude})) * cos(radians(latitude)) * cos(radians(longitude) - radians(${user-longitude})) + sin(radians(${user-latitude})) * sin(radians(latitude))))`),
'<=',
25
),
order: 'distance ASC',
limit: 10,
offset: 0
});
sequelize in this case is your instance of Sequelize.

With MDX how to sort by month

How do I sort by month. Now the sorting is alfabetical. And that is not correct. I want january before april - not the other way run :)
Im working in report builder 3.0 and have tried this (=format(Fields!Måned.Value,"MM") as a calculated member called monthsort, but it dosent Work. (Måned = month in Danish).
I have the following MDX-code:
WITH
SET [prodSet] AS
[Borger].[Anonym Borgernøgle DPR].[Anonym Borgernøgle DPR].ALLMEMBERS
SET [prodDtSet] AS
NonEmpty
(
[prodSet]
*
{
( [SundhedOgOmsorg - Ydelse].[Ydelse].[Ydelse].ALLMEMBERS *
[Kalender].[År].[År].ALLMEMBERS *
[Kalender].[Måned].[Måned].ALLMEMBERS )
}
,[Measures].[Antal unikke brugere - Visiteret Tid]
)
SET [prodDtSetFiltered] AS
Filter
(
[prodDtSet]
,
(NOT
[prodDtSet].Item(
[prodDtSet].CurrentOrdinal - 1).Item(0)
IS
[prodDtSet].Item(
[prodDtSet].CurrentOrdinal - 2).Item(0))
OR
[prodDtSet].CurrentOrdinal = 1
)
SELECT
{ [Measures].[Antal unikke brugere - Visiteret Tid] } ON 0
,[prodDtSetFiltered] ON 1
FROM
(
SELECT
{
[SundhedOgOmsorg - Ydelse].[Ydelse].&[12.1 Hjemmetrænerforløb]
,[SundhedOgOmsorg - Ydelse].[Ydelse].&[12.2 Komb. Hjemmetræner & Terapeutforløb]
,[SundhedOgOmsorg - Ydelse].[Ydelse].&[12.3 Komplekse Rehabiliteringsforløb]
,[SundhedOgOmsorg - Ydelse].[Ydelse].&[12.7. hverdagsrehab. revis.gr. m. tp.]
} ON COLUMNS
FROM
(
SELECT
{[SundhedOgOmsorg - Modul].[Modul].&[Hjemmehjælp]} ON COLUMNS
FROM [FrbLis]
)
)
WHERE
[SundhedOgOmsorg - Modul].[Modul].&[Hjemmehjælp]
You can try and pull the
MEMBER [Measures].[MånedSortKey] AS [Kalender].[Måned].CURRENTMEMBER.PROPERTIES('KEY')
You can also use MEMBER_KEY instead of PROPERTIES('KEY')
And Sort by it in the report.
(I'm assuming your Key is a Date or a Int which should be more sortable than a string)
(...)
MEMBER [Measures].[MånedSortKey] AS [Kalender].[Måned].CURRENTMEMBER.PROPERTIES('KEY')
SELECT
{
[Measures].[Antal unikke brugere - Visiteret Tid],
[Measures].[MånedSortKey]
} ON 0,
[prodDtSetFiltered] ON 1
FROM (
(...)
You will probably need to use the following function in your script:
Order : https://msdn.microsoft.com/en-us/library/ms145587.aspx
The <numeric expression> to use as an argument will be the Month member's .membervalue property.
Taking mxix measure (I've used membervalue rather than key - although both should work) you should be able to enforce an order inside the mdx:
WITH
MEMBER [Measures].[MånedSortKey] AS
[Kalender].[Måned].CURRENTMEMBER.membervalue
SET [prodSet] AS
[Borger].[Anonym Borgernøgle DPR].[Anonym Borgernøgle DPR].ALLMEMBERS
SET [prodDtSet] AS
NonEmpty
(
[prodSet]
*
{
( [SundhedOgOmsorg - Ydelse].[Ydelse].[Ydelse].ALLMEMBERS *
[Kalender].[År].[År].ALLMEMBERS *
[Kalender].[Måned].[Måned].ALLMEMBERS )
}
,[Measures].[Antal unikke brugere - Visiteret Tid]
)
SET [prodDtSetFiltered] AS
Filter
(
[prodDtSet]
,
(NOT
[prodDtSet].Item(
[prodDtSet].CurrentOrdinal - 1).Item(0)
IS
[prodDtSet].Item(
[prodDtSet].CurrentOrdinal - 2).Item(0))
OR
[prodDtSet].CurrentOrdinal = 1
)
SELECT
{ [Measures].[Antal unikke brugere - Visiteret Tid] } ON 0
,ORDER(
[prodDtSetFiltered],
[Measures].[MånedSortKey],
BDESC
) ON 1
FROM
...
...

SQL Server CASE then statement error

I have a pretty simple Stored Procedure that I'm trying to add a case then statement in the Where Statement. I keep getting an error message:
Msg 156, Level 15, State 1, Procedure Proc_AssuranceBilling_rpt, Line 36
Incorrect syntax near the keyword 'Case'.
I don't know why. Can someone please look and tell me what I've done wrong?
WHERE
e.[DDEventDesc] IN (SELECT #rptType
Case WHEN 'Payroll - Audit' THEN ('Payroll - Audit')
WHEN 'Audits' Then ('Audit - Aup', 'Audit - EBP', 'Audit- Financial Institutions','Audit - Governmental','Audit - HUD','Audit - Not-for-Profit','Audit - Personal Property Tax','Audit - Single Audit','Audit - Small Business')
WHEN 'Review & Comps' Then ('Audit - Review', 'Audit -Comp/Disc','Audit - Comp w/o Disc')
WHEN 'Assur Tax Returns' THEN ('5500','720-PCORI','8955-SSA','Campaign Report','Corporate (1120-POL)','LM-1','LM-2','LM-3','LM-4','LM-10','LM-30','Non-Profit (990)','Non-Profit (990 EZ)','Non-Profit (990-N)','Non-Profit (990-T)','Schedule C Letters','Section 104D Letter')
END)
AND AR.[ARType] = 1
AND (CLT.[cmaster] = 1 OR CLT.[cinvIndivEng] = 0)
I think you need to rewrite your WHERE clause as:
WHERE ((#rptType = 'Payroll - Audit' AND e.[DDEventDesc] = 'Payroll - Audit') OR
(#rptType = 'Audits' AND e.[DDEventDesc] IN ('Audit - Aup',
'Audit - EBP',
'Audit- Financial Institutions',
'Audit - Governmental',
'Audit - HUD',
'Audit - Not-for-Profit',
'Audit - Personal Property Tax',
'Audit - Single Audit',
'Audit - Small Business') OR
(#rptType = 'Review & Comps' AND e.[DDEventDesc] IN ('Audit - Review', 'Audit -Comp/Disc','Audit - Comp w/o Disc')) OR
(#rptType = 'Assur Tax Returns' AND e.[DDEventDesc] IN ('5500','720-PCORI','8955-SSA','Campaign Report','Corporate (1120-POL)','LM-1','LM-2','LM-3','LM-4','LM-10','LM-30','Non-Profit (990)','Non-Profit (990 EZ)','Non-Profit (990-N)','Non-Profit (990-T)','Schedule C Letters','Section 104D Letter') )
AND AR.[ARType] = 1
AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0)
CASE cannot be used to return multiple values. From MSDN on CASE:
Syntax:
Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
THEN result_expression Is the expression returned when
input_expression equals when_expression evaluates to TRUE, or
Boolean_expression evaluates to TRUE. result expression is any valid
expression.
And what can any valid expression be? According to Expressions:
Is a combination of symbols and operators that the SQL Server Database
Engine evaluates to obtain a single data value.
the variable #rptType should be the case variable .. u placed it out side the case
try this..
WHERE e.[DDEventDesc] IN ( SELECT Case #rptType
WHEN 'Payroll - Audit' THEN ('''Payroll - Audit''')
WHEN 'Audits' Then ('''Audit - Aup, Audit - EBP'', ''Audit- Financial Institutions'',''Audit - Governmental'',''Audit - HUD'',''Audit - Not-for-Profit'',''Audit - Personal Property Tax'',''Audit - Single Audit,Audit - Small Business''')
WHEN 'Review & Comps' Then ('''Audit - Review'', ''Audit -Comp/Disc'',''Audit - Comp w/o Disc''')
WHEN 'Assur Tax Returns' THEN ('''5500'',''720-PCORI'',''8955-SSA'',''Campaign Report'',''Corporate (1120-POL)'',''LM-1'',''LM-2'',''LM-3,''LM-4,''LM-10'',''LM-30'',''Non-Profit (990)'',''Non-Profit (990 EZ)'',''Non-Profit (990-N)'',''Non-Profit (990-T)'',''Schedule C Letters'',''Section 104D Letter''')
END
)
AND AR.[ARType] = 1
AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0)

Fast MDX query on SSAS 2005 performs very badly on SSAS 2012

I have very little experience with MDX and SSAS so have no idea where to start with this:
We are moving from a SQL 2005 environment to SQL 2012. On SSAS 2005 the query below runs in about 3 to 4 seconds. When running the same query on SSAS 2012 it runs for a whopping 1 hour 58 minutes before completing. Could anyone shed some light on why the performance is so terrible, and how it could be improved? We're on SQL 2012 11.0.5532.0 (X64).
Thanks a lot in advance.
with member [Measures].[Calculation] as
Format(
IIF(isempty([Measures].[average complience to requirements]), null,
[Measures].[average complience to requirements]),
"#,0.00"
)
select
non empty
{
[Measures].[average complience to requirements],
[Measures].[Calculation]
} on 0,
nonempty
(
[Customer].[App Key Company Id].children *
[Location].[App Key Region Id].children *
[Category].[App Key Category Id].children *
[Vendor].[App Key Vendor Id].children,
[average complience to requirements]
) on 1
from
[BSC]
where
(
strtomember(
"[Date Submitted Date].[YYYY-MMMM-DD].[Month Of Year].&[" + Format( Now(), "yyyyMM") + "]"
):
strtomember(
"[Date Submitted Date].[YYYY-MMMM-DD].[Month Of Year].&[" + Format( Now(), "yyyyMM") + "]").Lag(2)
)
If current month is the last member of [Date Submitted Date].[YYYY-MMMM-DD] hierarchy, query could be simplified to this one:
with member [Measures].[Calculation] as
Format([Measures].[average complience to requirements],"#,0.00")
select
{
[Measures].[average complience to requirements],
[Measures].[Calculation]
} on 0,
nonempty
(
[Customer].[App Key Company Id].children *
[Location].[App Key Region Id].children *
[Category].[App Key Category Id].children *
[Vendor].[App Key Vendor Id].children,
[average complience to requirements]
) on 1
from
[BSC]
where
Tail([Date Submitted Date].[YYYY-MMMM-DD].[Month Of Year].Members,3)
Unnecessary Non Empty checks were removed from axis 0 and [Measures].[Calculation].
TAIL function is used instead of dynamic members to let server use block mode instead of cell-by-cell computation.
Please also answer questions under your message to have better understanding of possible issues.
Not sure if this would work, but try this one out too:
Here replaced WITH clause with subselect
with member [Measures].[Calculation] as
Format(
IIF(isempty([Measures].[average complience to requirements]), null,
[Measures].[average complience to requirements]),
"#,0.00"
)
select
non empty
{
[Measures].[average complience to requirements],
[Measures].[Calculation]
} on 0,
nonempty
(
[Customer].[App Key Company Id].children *
[Location].[App Key Region Id].children *
[Category].[App Key Category Id].children *
[Vendor].[App Key Vendor Id].children
//[average complience to requirements] //removed
) on 1
from
(
SELECT
{
strtomember(
"[Date Submitted Date].[YYYY-MMMM-DD].[Month Of Year].&[" + Format( Now(), "yyyyMM") + "]"
):
strtomember(
"[Date Submitted Date].[YYYY-MMMM-DD].[Month Of Year].&[" + Format( Now(), "yyyyMM") + "]").Lag(2)
} ON 0
FROM [BSC]
)
How much time does the below queries take?
1) No calculated member
//with member [Measures].[Calculation] as
// Format(
// IIF(isempty([Measures].[average complience to requirements]), null,
// [Measures].[average complience to requirements]),
// "#,0.00"
//)
select
non empty
{
// [Measures].[average complience to requirements], /////This measure removed
[Measures].[Calculation]
} on 0,
nonempty
(
[Customer].[App Key Company Id].children *
[Location].[App Key Region Id].children *
[Category].[App Key Category Id].children *
[Vendor].[App Key Vendor Id].children
//[average complience to requirements] //removed
) on 1
FROM [BSC]
where (
{
strtomember(
"[Date Submitted Date].[YYYY-MMMM-DD].[Month Of Year].&[" + Format( Now(), "yyyyMM") + "]"
):
strtomember(
"[Date Submitted Date].[YYYY-MMMM-DD].[Month Of Year].&[" + Format( Now(), "yyyyMM") + "]").Lag(2)
}
)
2) Nothing on ROWS
with member [Measures].[Calculation] as
Format(
IIF(isempty([Measures].[average complience to requirements]), null,
[Measures].[average complience to requirements]),
"#,0.00"
)
select
non empty
{
[Measures].[average complience to requirements], /////This measure removed
[Measures].[Calculation]
} on 0,
{} on 1
FROM [BSC]
where (
{
strtomember(
"[Date Submitted Date].[YYYY-MMMM-DD].[Month Of Year].&[" + Format( Now(), "yyyyMM") + "]"
):
strtomember(
"[Date Submitted Date].[YYYY-MMMM-DD].[Month Of Year].&[" + Format( Now(), "yyyyMM") + "]").Lag(2)
}
)
3) NO SLICER
select
non empty
{
// [Measures].[average complience to requirements], /////This measure removed
[Measures].[Calculation]
} on 0,
nonempty
(
[Customer].[App Key Company Id].children *
[Location].[App Key Region Id].children *
[Category].[App Key Category Id].children *
[Vendor].[App Key Vendor Id].children
//[average complience to requirements] //removed
) on 1
FROM [BSC]
If nothing is conclusive out of them, just do some more similar troubleshooting to zero in on the actual cause of issue. Once you figure out, it will be easy to work on it. For us, it is obviously pointless to do armchair analysis without having any idea on the inside cube, when the actual query looks quite okay.

MDX - Extract Last 24 hours data

I need to extract last 24 hours data from a cube. Below is the MDX I have written but it doesn't return the data according to current time (system time).
SELECT
[Date].[Calender-Year_Quarter_Month_Date].[Date] ON ROWS
,{[Measures].[Delay In Mintues]} ON COLUMNS
FROM
(
SELECT
{
StrToMember
("[Date].[Calender-Year_Quarter_Month_Date].[Date].&["
+
Format
(
Now() - 1
,'yyyyMMdd'
)
+ "]"
)
*
(
[Time].[Time-Hour_Time].[Hour].&["+FORMAT(NOW(),"HH")+"]
:
[Time].[Time-Hour_Time].[Hour].&[23].&[23:59]
)
}
+
{
StrToMember
("[Date].[Calender-Year_Quarter_Month_Date].[Date].&["
+
Format
(
Now()
,'yyyyMMdd'
)
+ "]"
)
*
(
[Time].[Time-Hour_Time].[Hour].&[0].&[00:00]
:
[Time].[Time-Hour_Time].[Hour].&["+FORMAT(NOW(),"HH")+"]
)
} ON COLUMNS
FROM [Delay Reasons]
);
(note: not tested)
Does this work?
SELECT
[Date].[Calender-Year_Quarter_Month_Date].[Date] ON ROWS
,{[Measures].[Delay In Mintues]} ON COLUMNS
FROM [Delay Reasons]
WHERE
{
StrToMember
("[Date].[Calender-Year_Quarter_Month_Date].[Date].&["
+
Format
(
Now() - 1
,'yyyyMMdd'
)
+ "]"
)
*
(
[Time].[Time-Hour_Time].[Hour].&["+FORMAT(NOW(),"HH")+"]
:
[Time].[Time-Hour_Time].[Hour].&[23].&[23:59]
)
}
+
{
StrToMember
("[Date].[Calender-Year_Quarter_Month_Date].[Date].&["
+
Format
(
Now()
,'yyyyMMdd'
)
+ "]"
)
*
(
[Time].[Time-Hour_Time].[Hour].&[0].&[00:00]
:
StrToMember
(
"[Time].[Time-Hour_Time].[Hour].&[" + Format(Now(),"HH") + "].&[00:00]"
)
)
};