Checking Multiple condition in case statement using MDX query - ssas

How to write the below case statement in mdx.I moved the below mentioned columns in the fact table.
sum(case when IsSummary = 1 and IsAudited = 1 and FinalAuditFlag = 1 then 1
else 0 end) AuditsCompleted,--Count - Completed Audits
i tried the below MDX query.
WITH
MEMBER [Measures].[count]
AS
(
case ([Measures].currentmember )
when 0 then ([Measures].[Is Summary] )
else 1
end
)
Select
{[Measures].[count]} on 0,
[Dim_table1].[TableId].members on 1
from [Dsv]

What you have done is almost correct. Just change .currentmember to an actual measure in your cube. Currently when you have the following it is referring to itself i.e. currentmember by the black arrow it referring to the measure count by the black arrow...
This is in AdvWrks:
SELECT
{[Measures].[Internet Sales Amount]} ON 0
,{[Product].[Product Categories].[Category]} ON 1
FROM [Adventure Works];
It returns this:
If I want to replace the empty cell for Components the we can use CASE:
WITH
MEMBER [Measures].[count] AS
CASE [Measures].[Internet Sales Amount]
WHEN 0 THEN "XXX"
ELSE [Measures].[Internet Sales Amount]
END ,format_string = "#,###,##0"
SELECT
{
[Measures].[count]
} ON 0
,{[Product].[Product Categories].[Category]} ON 1
FROM [Adventure Works];
This now returns this:
IIF is used a lot more in MDX than CASE - IIF is nearly always faster. So the above equivalent using IIF is the following:
WITH
MEMBER [Measures].[count] AS
IIF
(
[Measures].[Internet Sales Amount] = 0
,"XXX"
,[Measures].[Internet Sales Amount]
)
,format_string = "#,###,##0"
SELECT
{[Measures].[count]} ON 0
,{[Product].[Product Categories].[Category]} ON 1
FROM [Adventure Works];

I am assuming that your requirement is to find the sum over all the members of [Dim Table1].[TableID] hierarchy.
If so, then the below should work for you:-
WITH MEMBER Measures.[Count] AS
SUM(
[Dim_table1].[TableId].CHILDREN,
[Measures].[Is Summary] * [Measures].[Is Audited] * [Measures].[Final Audited Flag] //If any is 0, it's 0, else 1
)
SELECT Measures.[Count]
FROM [Dsv]
Let me know if further slicers are needed to be added or if my understanding is incorrect.

Related

Subselects only support the COLUMNS axis

Subselects only support the COLUMNS axis.
SELECT NON EMPTY { [Measures].[Total Due] } ON COLUMNS,
TopCount ({[Store].[Name].Members *[Customer].[Store ID 1].Members }
,5,
[Measures].[Total Due]) ON ROWS
FROM [TOP_5]
Subselects support as many axes as you like - in fact the subselect is one of the occasions where you can practically use more than two axes.
SELECT
[Sales Territory].[Sales Territory Region].MEMBERS ON 0
,[Date].[Calendar].[Calendar Year].MEMBERS ON 1
FROM
( //<< subselect starts here
SELECT
[Sales Territory].[Sales Territory Region].[Canada] ON 0
,[Product].[Product].[Mountain-200 Black, 42] ON 1
,[Promotion].[Promotion Type].[No Discount] ON 2
,[Date].[Calendar].[Calendar Year].[CY 2008] ON 3
FROM [Adventure Works]
) //<< subselect ends here
WHERE
[Measures].[Sales Amount];
Did you try the WITH clause
WITH SET [X] AS
TopCount (
{[Store].[Name].Members *[Customer].[Store ID 1].Members }
,5
,[Measures].[Total Due]
)
SELECT
NON EMPTY { [Measures].[Total Due] } ON COLUMNS,
[X] ON ROWS
FROM [TOP_5];

Error with MDX expression

I have the following expression MDX :
isnull([Measures].[Available Hours],0)
After processing cube I got the following error in reporting :
No appropriate overload function was found for the stored procedure
.The parameters are incorrect .MdxScript Execution of the managed
stored procedure isnull failed with the following error :Microsoft
AnalysisServices AdomdServer AdomdException
How to resolve the error ?
isnull is not an mdx function.
To test for null in mdx try using iif
iif(
[Measures].[Available Hours] = 0,
0,
[Measures].[Available Hours]
)
It looks a little strange as we look for 0 and then if it is 0 change it to 0!!
But here is an example:
WITH
MEMBER [Measures].[Internet Sales Amount 2] AS
IIF
(
[Measures].[Internet Sales Amount] = 0
,0
,[Measures].[Internet Sales Amount]
)
SELECT
{
[Measures].[Internet Sales Amount]
,[Measures].[Internet Sales Amount 2]
} ON 0
,
[Customer].[Customer Geography].[Country].MEMBERS
*
[Product].[Category].MEMBERS ON 1
FROM [Adventure Works];
This is what happens:
Edit
Alternative by #MarcPolizzi does the same thing and his code is more compact:
WITH
MEMBER [Measures].[Internet Sales Amount 2] AS
IIF
(
[Measures].[Internet Sales Amount] = 0
,0
,[Measures].[Internet Sales Amount]
)
MEMBER [Measures].[Internet Sales Amount 3] AS
CoalesceEmpty
(
[Measures].[Internet Sales Amount]
,0
)
SELECT
{
[Measures].[Internet Sales Amount]
,[Measures].[Internet Sales Amount 2]
,[Measures].[Internet Sales Amount 3]
} ON 0
,
[Customer].[Customer Geography].[Country].MEMBERS
*
[Product].[Category].MEMBERS ON 1
FROM [Adventure Works];
You could use the CoalesceEmpty function to replace null/empty values. To replace them with a 0 value you can do the following:
CoalesceEmpty([Measures].[Available Hours], 0)
Hope that helps.

Using GENERATE to concatenate Rank and Member Name

The following is from MSDN: http://msdn.microsoft.com/en-us/library/ms144726.aspx
WITH
SET OrderedCities AS Order
([Geography].[City].[City].members
, [Measures].[Reseller Sales Amount], BDESC
)
MEMBER [Measures].[City Rank] AS Rank
([Geography].[City].CurrentMember, OrderedCities)
SELECT {[Measures].[City Rank],[Measures].[Reseller Sales Amount]} ON 0
,Order
([Geography].[City].[City].MEMBERS
,[City Rank], ASC)
ON 1
FROM [Adventure Works]
Would it be possible to use the GENERATE function to concatenate the rank and the city into a single column. So results would look like the following:
EDIT
So the following must be getting closer:
WITH
SET OrderedCities AS Order
([Geography].[City].[City].members
, [Measures].[Reseller Sales Amount], BDESC
)
MEMBER [Measures].[City Rank] AS Rank
([Geography].[City].CurrentMember, OrderedCities)
MEMBER [Measures].[memberName] AS
'[Geography].[City].CurrentMember.name'
MEMBER [Measures].[memberValue] AS
'[Measures].[City Rank].value'
MEMBER [Measures].[concat] AS
// [Measures].[memberName] + [Measures].[memberValue]//<<this errors
// '[Measures].[memberName] & [Measures].[memberValue]'//<<this errors
'[Measures].[memberName] + [Measures].[memberValue]'//<<this errors
SELECT
{[Measures].[City Rank],
[Measures].[memberName],
[Measures].[memberValue],
[Measures].[concat],
[Measures].[Reseller Sales Amount]} ON 0
,Order
([Geography].[City].[City].MEMBERS
,[City Rank], ASC)
ON 1
FROM [Adventure Works]
the following should work:
MEMBER [Measures].[concat] AS
cstr(RANK( [Geography].[City].currentmember, orderedcities)) +'.'+
[Geography].[City].currentmember.name
You will still have to use the presentation programm to get rid of the first redundant column.
Philip,

How to get an "(all)" value for a level in a user defined hierarchy in SSAS/MDX

I am trying to work out how I can get a total for a level in a user defined hierarchy. For example if I do (using Adventure works here):
SELECT [Measures].[Internet Sales Amount] ON 0,
([Date].[Calendar Year].ALLMEMBERS * [Date].[Month of Year].ALLMEMBERS) ON 1
FROM [Adventure Works]
I get totals for each pair of year/month values.
How can I rewrite this query using the Calendar user hierarchy in the Date dimensions? Eg something like:
SELECT [Measures].[Internet Sales Amount] ON 0,
([Date].[Calendar].[Year] * [Date].[Calendar].[Month]) ON 1
FROM [Adventure Works]
You can use
SELECT [Measures].[Internet Sales Amount] ON 0,
Hierarchize([Date].[Calendar].[Calendar Year] + [Date].[Calendar].[Month]) ON 1
FROM [Adventure Works]
This will show you each year containing its value (which is the sum of the months contained in it) before the months.
The + is the short form of Union in MDX, and Hierarchize sorts a set in hierarchical order, which means parents before children.
And if you want something similar to your first result, the closest thing I can imagine would be via calculated measures like this:
WITH Member Measures.Year AS
IIf([Date].[Calendar].CurrentMember.Level IS [Date].[Calendar].[Calendar Year],
[Date].[Calendar].CurrentMember.Name,
Ancestor([Date].[Calendar].CurrentMember, [Date].[Calendar].[Calendar Year]).Name
)
Member Measures.Month AS
IIf([Date].[Calendar].CurrentMember.Level IS [Date].[Calendar].[Month],
[Date].[Calendar].CurrentMember.Name,
'All Months'
)
SELECT {
Measures.Year,
Measures.Month,
[Measures].[Internet Sales Amount]
}
ON 0,
Hierarchize([Date].[Calendar].[Calendar Year] + [Date].[Calendar].[Month]) ON 1
FROM [Adventure Works]
Note that calculated members need not return numbers, they can as well return strings.
Using allmembers as you have it should return you the ALL line and then each year and each month. If you are interested in only the all line you can change you query to:
SELECT [Measures].[Internet Sales Amount] ON 0,
([Date].[Calendar Year] * [Date].[Month of Year]) ON 1
FROM [Adventure Works]
Notice how it is specifying only the Dimension and the hierarchy.
If you wanted to get the data without the all line use the following:
SELECT [Measures].[Internet Sales Amount] ON 0,
([Date].[Calendar Year].children * [Date].[Month of Year].children) ON 1
FROM [Adventure Works]

Comparison in Where clause in MDX Queries

In MDX Queries, How can i compare whether a level value is less than a certain value or not.
e.g.
SELECT NON EMPTY
[Sales Territory].[Sales Territory Country].Members ON 0,
[Product].[Category].[Clothing] ON 1
FROM
[Adventure Works]
WHERE
([Measures].[LowGPM] > 120)
This MDX query didn't work.
Also, i tried WITH Clause also.
WITH Member [Measures].[Calculated Measures] as
IIF ([Measures].[LowGPM] < 120, [Measures].[LowGPM], null)
SELECT NON EMPTY
[Sales Territory].[Sales Territory Country].Members ON 0,
[Product].[Category].[Clothing] ON 1
FROM
[Adventure Works]
WHERE
([Measures].[LowGPM] > 120)
It shows Mondrian Error:MDX object '[Measures].[Calculated Measures]' not found in cube.
You can filter the axis like this:
SELECT
[Measures].[Internet Sales Amount] ON 0,
Filter(
[Customer].[Country].Members,
([Measures].[Internet Sales Amount] > 2000000)
AND ([Measures].[Internet Sales Amount] < 5000000)
) ON 1
FROM [Adventure Works]