MDX - filter measure by dimension - mdx

I'm new to MDX and I'm currently trying to filter [Measures].[Sales Invoice Line Amount] by
{
[NAV CINNOST].[DIM_Code_Cinnost].&[AAA],
[NAV CINNOST].[DIM_Code_Cinnost].&[BBB],
[NAV CINNOST].[DIM_Code_Cinnost].&[CCC]
}
I only need to filter THIS measure (Sales Invoice Line Amount). Not the others. Can you guide me how to rewrite the query?
SELECT
NON EMPTY [Customer].[Customer No].[Customer No] DIMENSION PROPERTIES
member_name, parent_unique_name ON ROWS,
{
[Measures].[Value Entry Item Ledger Entry Quantity],
[Measures].[Unit Margin],
[Measures].[Sales Invoice Line Amount]
} ON COLUMNS
FROM [QTY Margin]
WHERE (
{
[Date].[Calendar Month].&[201407]
},
{
[NAV CINNOST].[DIM_Code_Cinnost].&[AAA],
[NAV CINNOST].[DIM_Code_Cinnost].&[BBB],
[NAV CINNOST].[DIM_Code_Cinnost].&[CCC]
}
) CELL PROPERTIES value, formatted_value, fore_color, back_color

The quickest solution is using a calculated member that does the job you're looking for. Assuming you add over this dimension :
WITH
MEMBER [My Sales Invoice Line Amount] AS Sum( {[NAV CINNOST].[DIM_Code_Cinnost].&[AAA],[NAV CINNOST].[DIM_Code_Cinnost].&[BBB], [NAV CINNOST].[DIM_Code_Cinnost].&[CCC]},
[Measures].[Sales Invoice Line Amount])
SELECT
NON EMPTY [Customer].[Customer No].[Customer No] ON ROWS,
{[Measures].[Value Entry Item Ledger Entry Quantity],
[Measures].[Unit Margin],
[Measures].[My Sales Invoice Line Amount]
} ON COLUMNS
FROM
[QTY Margin]
WHERE
[Date].[Calendar Month].&[201407]

Related

Aggregate values based on multiple dimension members to create a Profit & Loss report via SCOPE statement

I am relatively new to SSAS MDX and am facing the problem to create aggregates over all measures of different dimension members.
The following picture best describes what I'm trying to achieve:
Problem and What the solution should be
This is a draft and a simplification of a more complex scenario, so don't bother ;-).
After running a series of experiments I figured out to use a simple dimension without connection to other dimensions to be able to use the SCOPE statement to override the produced results in the report.
The simple dimension "Kurz PuL" contains the preceding members (Umsatz .. Periodenergebnis) and shall produce the Profits + Loss report.
Kurz PuL Dimension
What currently works is to show results of single members (like Umsatz, Wareneinsatz, SbA, Personal).
But I am not able to aggregate values of more than one member to obtain an aggregate, e.g.: of Umsatz + Wareneinsatz = Rohertrag. I tried different approaches but none worked.
See the following SCOPE statements which are used to "override" the resulting values that are displayed in Excel (first image ^^):
SCOPE Statements:
/*
The CALCULATE command controls the aggregation of leaf cells in the cube.
If the CALCULATE command is deleted or modified, the data within the cube is affected.
You should edit this command only if you manually specify how the cube is aggregated.
*/
CALCULATE;
SCOPE ([Kurz PuL].[Calculated].[Umsatz]);
THIS = AGGREGATE([EinfacheBwaZeile].[Hierarchy].&[U]);
END SCOPE;
SCOPE ([Kurz PuL].[Calculated].[Wareneinsatz]);
THIS = AGGREGATE([EinfacheBwaZeile].[Hierarchy].&[W]);
END SCOPE;
/* DOES NOT WORK: */
SCOPE ([Kurz PuL].[Calculated].[Rohertrag]);
THIS = AGGREGATE(FILTER([EinfacheBwaZeile].[Hierarchy].CurrentMember, { [EinfacheBwaZeile].[Hierarchy].&[U], [EinfacheBwaZeile].[Hierarchy].&[W] }));
END SCOPE;
SCOPE ([Kurz PuL].[Calculated].[Personal]);
THIS = AGGREGATE( { [EinfacheBwaZeile].[Hierarchy].&[P] });
END SCOPE;
SCOPE ([Kurz PuL].[Calculated].[Marketing]);
THIS = AGGREGATE( { [EinfacheBwaZeile].[Hierarchy].&[M] });
END SCOPE;
/* DOES NOT WORK: */
SCOPE ([Kurz PuL].[Calculated].[Deckungsbeitrag]);
THIS = AGGREGATE( { [EinfacheBwaZeile].[Hierarchy].&[U], [EinfacheBwaZeile].[Hierarchy].&[W], [EinfacheBwaZeile].[Hierarchy].&[R], [EinfacheBwaZeile].[Hierarchy].&[P]
, [EinfacheBwaZeile].[Hierarchy].&[M], [EinfacheBwaZeile].[Hierarchy].&[D] });
END SCOPE;
SCOPE ([Kurz PuL].[Calculated].[SbA]);
THIS = AGGREGATE( { [EinfacheBwaZeile].[Hierarchy].&[S] });
END SCOPE;
/* DOES NOT WORK: */
SCOPE ([Kurz PuL].[Calculated].[EBITDA]);
THIS = AGGREGATE( { [EinfacheBwaZeile].[Hierarchy].&[U], [EinfacheBwaZeile].[Hierarchy].&[W], [EinfacheBwaZeile].[Hierarchy].&[R], [EinfacheBwaZeile].[Hierarchy].&[P]
, [EinfacheBwaZeile].[Hierarchy].&[M], [EinfacheBwaZeile].[Hierarchy].&[D]
, [EinfacheBwaZeile].[Hierarchy].&[S], [EinfacheBwaZeile].[Hierarchy].&[A] });
END SCOPE;
/* DOES NOT WORK: */
SCOPE ([Kurz PuL].[Calculated].[EBIT]);
THIS = AGGREGATE( { [EinfacheBwaZeile].[Hierarchy].&[U], [EinfacheBwaZeile].[Hierarchy].&[W], [EinfacheBwaZeile].[Hierarchy].&[R], [EinfacheBwaZeile].[Hierarchy].&[P]
, [EinfacheBwaZeile].[Hierarchy].&[M], [EinfacheBwaZeile].[Hierarchy].&[D]
, [EinfacheBwaZeile].[Hierarchy].&[S], [EinfacheBwaZeile].[Hierarchy].&[A]
, [EinfacheBwaZeile].[Hierarchy].&[I] });
END SCOPE;
/* DOES NOT WORK: */
SCOPE ([Kurz PuL].[Calculated].[EBT]);
THIS = AGGREGATE( { [EinfacheBwaZeile].[Hierarchy].&[U], [EinfacheBwaZeile].[Hierarchy].&[W], [EinfacheBwaZeile].[Hierarchy].&[R], [EinfacheBwaZeile].[Hierarchy].&[P]
, [EinfacheBwaZeile].[Hierarchy].&[M], [EinfacheBwaZeile].[Hierarchy].&[D]
, [EinfacheBwaZeile].[Hierarchy].&[S], [EinfacheBwaZeile].[Hierarchy].&[A]
, [EinfacheBwaZeile].[Hierarchy].&[I]
, [EinfacheBwaZeile].[Hierarchy].&[T] });
END SCOPE;
/* DOES NOT WORK: */
SCOPE ([Kurz PuL].[Calculated].[Periodenergebnis]);
THIS = AGGREGATE( { [EinfacheBwaZeile].[Hierarchy].&[U], [EinfacheBwaZeile].[Hierarchy].&[W], [EinfacheBwaZeile].[Hierarchy].&[R], [EinfacheBwaZeile].[Hierarchy].&[P]
, [EinfacheBwaZeile].[Hierarchy].&[M], [EinfacheBwaZeile].[Hierarchy].&[D]
, [EinfacheBwaZeile].[Hierarchy].&[S], [EinfacheBwaZeile].[Hierarchy].&[A]
, [EinfacheBwaZeile].[Hierarchy].&[I]
, [EinfacheBwaZeile].[Hierarchy].&[T]
, [EinfacheBwaZeile].[Hierarchy].&[E] });
END SCOPE;
I've been trying to find a resource that explains how to aggregate on more than one member but didn't find a solution.
How can I use the AGGREGATE() function to combine/sum values that are related to multiple dimension members of [EinfacheBwaZeile].[Hierarchy].A, … .B, … .C ?
I really appreciate your answers!
Thanks in advance,
Cordt
-- UPDATE 2019-05-28 as reply to Moaz as of 2019-05-27: --
Hi Moaz,
thank you for your suggestion.
Sadly, that solution does not meet my needs.
First thing to notice is, that I need a SCOPE-Statement, not a SELECT-MDX.
Second, I need all the measures of specific members of another dimension to be "summarized". That looks at first sight like a "running total" but it depends on the values of "previous" members where some may be skipped.
In words of the Adventure Works sample, the following shows what I need:
SCOPE (MountainBikeSales);
THIS = AGGREGATE(Product.&[Bikes]);
END SCOPE;
SCOPE (BikesAndAccessories);
THIS = AGGREGATE({ Product.&[Bikes], Product.&[Accessories] });
END SCOPE;
SCOPE (BikesAccsClothing);
THIS = AGGREGATE({ Product.&[Bikes], Product.&[Accessories], Product.&[Clothing] });
END SCOPE;
Is that more clear?
You could even think of a SCOPE that skips Accessories by summarizing Bikes and Clothing instead of Accessories.
Problem to me is, that the first statement would succeed (that summarizes a single member) but the others won't.
Thank you for your advice!
Cordt
I am not sure that i understand your problem exactly. But I think you want to know how to calculate running totals. It that is the case take a look at the below example
with
member
[Measures].[Internet Sales AmountRunningtotal]
as
case when [Measures].[Internet Sales Amount] = null then null
else
sum({[Product].[Subcategory].firstchild:[Product].[Subcategory].currentmember},[Measures].[Internet Sales Amount])
end
select {[Measures].[Internet Sales Amount],
[Measures].[Internet Sales AmountRunningtotal]
} on columns,
non empty
([Date].[Calendar Year].[Calendar Year],[Date].[Calendar Quarter of Year].[Calendar Quarter of Year],
[Product].[Category].[Category],[Product].[Subcategory].[Subcategory])
on
rows
from
[Adventure Works]
Result
Edit:Based on your edit
select
{
[Measures].[Internet Sales Amount],
[Measures].[Internet Order Quantity],
[Measures].[Internet Tax Amount],
[Measures].[Internet Gross Profit Margin]
}on 0 ,
[Product].[Category].[Category]
on 1
from
[Adventure Works]
Results
Notice that all measures for Components have null values. Now Lets equate Components with Bikes & Accessories
Scope ([Product].[Category].&[2]) ;
this = (aggregate({[Product].[Category].&[1],[Product].[Category].&[4]},[Measures].[Measures].currentmember));
end scope;
select
{
[Measures].[Internet Sales Amount],
[Measures].[Internet Order Quantity],
[Measures].[Internet Tax Amount],
[Measures].[Internet Gross Profit Margin]
}on 0 ,
[Product].[Category].[Category]
on 1
from
[Adventure Works]
Results

How to check, which hierarchy is being used

I have 1 dimension Data with 2 hierarchies: Gregorian Calendar, Planning Calendar.
[Gregorian Year] - [Gregorian Month] - [Gregorian Day]
[Planning Year] - [Planning Month] - [Planning Day]
I have 2 MDX measures:
[Planning Stock] = Sum(PeriodsToDate([Data].[Planning Calendar].[(All)]), [amount])
[Gregorian Stock] = Sum(PeriodsToDate([Data].[Gregorian Calendar].[(All)]), [amount])
How to create one measure that will recognize the hierarchy?
I wrote something like that, but it does not work :(
iif(([Data].currentmember.level is [Data].[Planning Calendar].[(All)]),
(Sum(PeriodsToDate([Data].[Planning Calendar].[(All)]), [amount])),
(Sum(PeriodsToDate([Data].[Gregorian Calendar].[(All)]), [amount]))
)
Do you have any ideas?
You can do this using the level.hierarchy. Take a look at the example.
with member
measures.t
as
axis(1).item(0).hierarchy.name
select
{
[Measures].[Internet Sales Amount],measures.t
}
on columns,
{
[Date].[Fiscal].[Month].&[2014]&[7].children
--[Date].[Calendar].[Month].&[2013]&[6].children
}
on rows
from [Adventure Works]
Result
Lets change the hierarchy
with member
measures.t
as
axis(1).item(0).hierarchy.name
select
{
[Measures].[Internet Sales Amount],measures.t
}
on columns,
{
--[Date].[Fiscal].[Month].&[2014]&[7].children
[Date].[Calendar].[Month].&[2013]&[6].children
}
on rows
from [Adventure Works]
Result

Add a column to the MDX Query with a tuple

I have an MDX Query running on the SSAS cube that returns lots of object codes and the period balances for them. I was able to add multiple period balances by using a crossjoin on the rows, however I would like to add one more row with the period end balance for the last fiscal period, and can't seem to figure out a way to do it.
The initial query is
select
non empty
{
[Object Code].[Object Code Number].[Object Code Number]
*
[Object Code].[Object Code Description].[Object Code Description]
*
[Object Code Pathing 1E 1R].[1E_R1 Value].[1E_R1 Value]
*
[Object Code Pathing 1E 1R].[1E_R2 Value].[1E_R2 Value]
*
[Object Code Pathing 1E 1R].[1E_R3 Value].[1E_R3 Value]
*
[Object Code Pathing 1E 1R].[1E_R4 Value].[1E_R4 Value]
}
on rows,
{
[Measures].[Current Period Balance]
}
*
{
[Date].[Fiscal].[Fiscal Period].&[2016]&[1]:[Date].[Fiscal].[Fiscal Period].&[2016]&[7]
}
on columns
from [Finance]
and when I am trying to add one more column
select
non empty
{
[Object Code].[Object Code Number].[Object Code Number]
*
[Object Code].[Object Code Description].[Object Code Description]
*
[Object Code Pathing 1E 1R].[1E_R1 Value].[1E_R1 Value]
*
[Object Code Pathing 1E 1R].[1E_R2 Value].[1E_R2 Value]
*
[Object Code Pathing 1E 1R].[1E_R3 Value].[1E_R3 Value]
*
[Object Code Pathing 1E 1R].[1E_R4 Value].[1E_R4 Value]
}
on rows,
{
[Measures].[Balance At Period End]
*
[Date].[Fiscal].[Fiscal Period]&[2016]&[7]
},
{
[Measures].[Current Period Balance]
}
*
{
[Date].[Fiscal].[Fiscal Period].&[2016]&[1]:[Date].[Fiscal].[Fiscal Period].&[2016]&[7]
}
on columns
from [Finance]
I get the
Parser: The statement dialect could not be resolved due to ambiguity. error
and if I add it like
{
[Measures].[Current Period Balance],
[Measures].[Balance At Period End]
}
*
{
[Date].[Fiscal].[Fiscal Period].&[2016]&[1]:[Date].[Fiscal].[Fiscal Period].&[2016]&[7]
}
on columns
I get Period end Balances for all periods, and this is not needed in the report, I only need the Balance at Period End for the very last period
Here is your first piece of troublesome code:
crossjoin (
[Measures].[Current Period Balance]
,{
[Date].[Fiscal].[Fiscal Period].&[2016]&[1]
,[Date].[Fiscal].[Fiscal Period].&[2016]&[2]
,[Date].[Fiscal].[Fiscal Period].&[2016]&[3]
}
), //<<1
crossjoin(
[Measures].[Balance At Period End]
,{[Date].[Fiscal].[Fiscal Period].&[2016]&[3]}
) on columns
from [Finance]
At point 1 you have closed the first crossjoin and then put a comma - this is a syntax error.
You could try moving that brace from 1 to the end of the statement:
crossjoin (
[Measures].[Current Period Balance]
,{
[Date].[Fiscal].[Fiscal Period].&[2016]&[1]
,[Date].[Fiscal].[Fiscal Period].&[2016]&[2]
,[Date].[Fiscal].[Fiscal Period].&[2016]&[3]
}
, //<<1
crossjoin(
[Measures].[Balance At Period End]
,{[Date].[Fiscal].[Fiscal Period].&[2016]&[3]}
)
) on columns //<<now closing initial crossjoin here
from [Finance]
Ok I just tested the above via the following and it is not a valid approach:
SELECT
CrossJoin
(
[Measures].[Internet Sales Amount]
,{
[Date].[Calendar].[Date].&[20060628]
,[Date].[Calendar].[Date].&[20060629]
}
,CrossJoin
(
[Measures].[Internet Order Quantity]
,{[Date].[Calendar].[Date].&[20060629]}
)
) ON COLUMNS
,[Product].[Product Categories].[All] ON ROWS
FROM [Adventure Works];
We get the following error:
Query (2, 3) The Measures hierarchy is used more than once in the
Crossjoin function.
You could switch to the following structure, creating a set of tuples. This does run:
SELECT
{
[Measures].[Internet Sales Amount]
*
{
[Date].[Calendar].[Date].&[20060628]
,[Date].[Calendar].[Date].&[20060629]
}
,(
[Measures].[Internet Order Quantity]
,{[Date].[Calendar].[Date].&[20060629]}
)
} ON COLUMNS
,[Product].[Product Categories].[All] ON ROWS
FROM [Adventure Works];
Result:

How to get top 5 in mdx

How to get top 5 from mdx query?
I have a this query:
WITH SET [Geography].[City] AS
TopCount(
[Geography].[City]
,5
,[Measures].[Reseller Freight Cost]
)
SELECT
NON EMPTY {
CROSSJOIN(
{
[Date].[Calendar]
},
{
[Product].[Category]
}
),
CROSSJOIN(
{
[Date].[Calendar].children
},
{
[Product].[Category]
}
)
} DIMENSION PROPERTIES children_cardinality, parent_unique_name ON COLUMNS,
NON EMPTY {
[Geography].[City],
[Geography].[City].children
} DIMENSION PROPERTIES children_cardinality, parent_unique_name ON ROWS
FROM [Adventure Works]
WHERE (
[Measures].[Reseller Freight Cost]
)
But is not working.
Now i have error
error: {"faultstring":"Query (1, 21) Parser: The syntax for '.' is incorrect.","faultcode":"XMLAnalysisError.0xc10e0002"}
The best if I not must change a code after SELECT word .
When you create a set just declare it with no associated hierarchy so this is wrong [Geography].[City].
Also there is a much more readable syntax for cross-join - just use an asterisk *
Try this:
WITH SET [CitySet] AS
TopCount(
[Geography].[City]
,5
,[Measures].[Reseller Freight Cost]
)
SELECT
NON EMPTY {
[Date].[Calendar] * [Product].[Category]
,[Date].[Calendar].children * [Product].[Category]
} DIMENSION PROPERTIES children_cardinality, parent_unique_name ON COLUMNS,
NON EMPTY {
[CitySet], //<<changed here [Geography].[City],
[Geography].[City].children
}
DIMENSION PROPERTIES children_cardinality, parent_unique_name ON ROWS
FROM [Adventure Works]
WHERE (
[Measures].[Reseller Freight Cost]
)

EXISTING equivalent to HAVING

This works:
WITH
MEMBER [Measures].[CurrentDay] AS
AGGREGATE(
[Date].[Calendar].Currentmember,
[Measures].[Reseller Sales Amount]
)
MEMBER [Measures].[CurrentMonth] AS
AGGREGATE(
[Date].[Calendar].Currentmember.parent,
[Measures].[Reseller Sales Amount]
)
SELECT
NON EMPTY
{ [Measures].[CurrentDay],
[Measures].[CurrentMonth] }
ON COLUMNS,
NON EMPTY
{ [Date].[Calendar].[Date] }
HAVING [Measures].[CurrentDay]<>null //<<<<<<<<<<<<<<having line
ON ROWS
From [Adventure Works]
Returning the following from the version of Adventure Works that I have:
If I comment out the line HAVING [Measures].[CurrentDay]<>null then this happens:
Is there another way of eliminating the rows that are null for CurrentDay without using HAVING ?
I've tried using EXISTING without any success:
WITH
MEMBER [Measures].[CurrentDay] AS
AGGREGATE(
[Date].[Calendar].Currentmember,
[Measures].[Reseller Sales Amount]
)
MEMBER [Measures].[CurrentMonth] AS
AGGREGATE(
[Date].[Calendar].Currentmember.parent,
[Measures].[Reseller Sales Amount]
)
SELECT
NON EMPTY
{ [Measures].[CurrentDay],
[Measures].[CurrentMonth] }
ON COLUMNS,
NON EMPTY
{ EXISTING [Date].[Calendar].[Date] }
ON ROWS
From [Adventure Works]
EDIT
To run nsousa's solution in SSMS I need to nest the IIF like this:
WITH
MEMBER [Measures].[CurrentDay] AS
AGGREGATE(
[Date].[Calendar].Currentmember,
[Measures].[Reseller Sales Amount]
)
MEMBER [Measures].[CurrentMonth] AS
IIF(
ISEMPTY([Measures].[CurrentDay]),
NULL,
AGGREGATE(
[Date].[Calendar].Currentmember.parent,
[Measures].[Reseller Sales Amount]
)
)
SELECT
NON EMPTY
{ [Measures].[CurrentDay],
[Measures].[CurrentMonth] }
ON COLUMNS,
NON EMPTY
{ [Date].[Calendar].[Date] }
ON ROWS
From [Adventure Works]
You can redefine your measure:
WITH
MEMBER [Measure].[Not Null Reseller Sales Amount] AS
IIF( IsEmpty( [Measures].[Reseller Sales Amount] ), 0, [Measures].[Reseller Sales Amount] )
MEMBER [Measures].[CurrentDay] AS
AGGREGATE(
[Date].[Calendar].Currentmember,
[Measures].[Not Null Reseller Sales Amount]
)
MEMBER [Measures].[CurrentMonth] AS
AGGREGATE(
[Date].[Calendar].Currentmember.parent,
[Measures].[Not Null Reseller Sales Amount]
)
SELECT
NON EMPTY
{ [Measures].[CurrentDay],
[Measures].[CurrentMonth] }
ON COLUMNS,
NON EMPTY
{ [Date].[Calendar].[Date] }
ON ROWS
From [Adventure Works]