I'm currently working with MDX in pentaho.
Currently I'm facing issue is that Unable to get top 10 count based on a Measure.
i.e., I want to write MDX query for find top 10 customer's based on Current year Sales volume and also needed Previous year sales amount for that customers.
My Problem is that MDX query Sum the Previous and Current year sales volume and listed out the top 10.
How can i get top 10 based on Current year column?
For Example,
Customer Current Year Previous Year
A 100 200
B 150 125
C 200 -
Expected Result:
Customer Current Year Previous Year
C 200 -
B 150 125
A 100 200
Please help me out.
The Topcount function takes 3 arguments, a Set, the number of items to return and a numeric expression.
Supposing you have [Customer].Members on Rows, two [Time] dimension members already defined and [Measures].[Sales] as your measure, you can use
Select
TopCount( [Customer].Members, 3, ( [Measures].[Sales], [Time].[Current Year]) ) on Rows,
{ [Time].[Current Year], [Time].[Previous Year] } on Columns
From [My Cube]
Where [Measures].[Sales]
The TopCount will return the top 3 customers, based on their rank by
([Measures].[Sales], [Time].[Current Year])
If you don't specify this tuple as the numeric expression you get the rank by the overall [Measures].[Sales] value.
Documentation: http://technet.microsoft.com/en-us/library/ms144792.aspx
Related
I need to find out the number of days in Month based on Time dimension. Time dimension is Month when Jan is selected it has to return 31 as value.
If you have Time dimension and Time hierarchy, this should work:
WITH MEMBER measures.NumOfDays AS
Count
(
Descendants
(
[Time].[Time].CurrentMember,
,LEAVES
)
)
SELECT Measures.NumOfDays ON 0,
[Time].[Time].Month on 1
FROM [MyCube]
The below sample shows how to get the count.
Please note the below query only show the idea how to do this. Your cube will note have these attributes you you need to replace them
with member
measures.t as Count(([Date].[Month of Year].&[1],[Date].[Day of Month].[Day of Month].members))
select {measures.t}
on columns
from [Adventure Works]
I want to calculate sales for promotion using it's date. I need 3 measures, avg sales from 21 days before promotion start date, sales in between of promotion's start and end date, and sales from 21 days after promotion's end date.
Why Visual Studio highlights avg in code below?
CREATE MEMBER CURRENTCUBE.[Measures].[Sales in promotion]
AS Avg(Existing([Promotion].[Promotion name].[Promotion name]),[Measures].[Sales]), ...
Same in here:
CREATE MEMBER CURRENTCUBE.[Measures].[Sales before promotion]
AS (EXISTING([Promotion].[Promotion name].[Promotion name]), AVG(strtomember("[Date].[Date].&["+ [Promotion].[Date].currentmember.member_key+"]").lag(21) : strtomember("[Date].[Date].&["+ [Promotion].[Date From].currentmember.member_key+"]"),
[Measures].[Sales])) ...
If I do sum(existing()) in first measure, the sum is calculated correctly, but it doesn't allow me to get average.
EXISTING will only help if [Promotion] is part of your query in either the WHERE or SELECT clause. If it is not included in either of these clause then EXISTING will be finding 1 member - the All member.
You could try NonEmpty and maybe move the period logic into a custom set?
WITH
SET [PERIOD] AS
STRTOSET(
"[Date].[Date].&["+ [Promotion].[Date].currentmember.member_key+"].lag(21)
:
[Date].[Date].&["+ [Promotion].[Date From].currentmember.member_key+"]"
)
From the code you posted I cannot tell if you want a daily average or and average per promotion ? Say there were 2 promotions over the 21 days does this mean you want (Total/2/21) ?
I've one MDX query where i want to show each of my product month wise count and along with that want to show YTD, MTD, WTD etc.
I've first created following MDx query on Adventure Works which give me YTD but its shows each months. instead i need output as below and I want to provide current month+year(Mar 2015) in MDX filter or where clause
EXPECTED Results:
Category YTD
--------- ------
Bike 4500
Accessories 78000
Clothing 8900
Can you please correct below MDX query to get above results sets
SELECT
{
([Product].[Category].CHILDREN,[Measures].[Order Quantity])
} ON 0,
{
YTD([Date].[Calendar].[Month].[August 2008])
} ON 1
FROM [Adventure Works];
or i've built another MDX query which give same results either one should be corrected to show above EXPECTED results.
SELECT
{
([Product].[Category].CHILDREN ,[Measures].[Order Quantity])
} ON Axis(0),
{
PERIODSTODATE([Date].[Calendar].[Calendar Year],[Date].[Calendar].[Month].[August 2008])
} ON Axis (1)
FROM [Adventure Works];
I don't think I have the same version of the AW cube as you (or what version of SSAS you are using), but try this. In the 2014 multidimensional cube, I have to change the measure to Order Count rather than quantity and change the date to March 2012 to get data back.
WITH MEMBER [Date].[Calendar].[CalendarYTD] AS
Aggregate(
YTD([Date].[Calendar].[Month].[March 2015])
)
SELECT
[Date].[Calendar].[CalendarYTD] ON COLUMNS,
[Product].[Category].Children ON ROWS
FROM
[Adventure Works]
WHERE
[Measures].[Order Quantity]
I want to create a member based on this problem
I have a Product A being sold
I want to find the largest range of consecutive days without sale
example:
days 1,2,3 the product not sale, after that,it sold for 15 consecutive days, at 19th day it didnt sell for 2 days and after that it sold every day until the end of the month
so my maximum days without sale was 3
The following query delivers in the Microsoft sample cube Adventure Works what you want:
WITH Member Measures.[days without sales] AS
IIf( [Measures].[Internet Sales Amount] > 0
, 0
,(Measures.[days without sales], [Date].[Calendar].CurrentMember.PrevMember) + 1
)
Member Measures.[Max days without sales] AS
Max( [Date].[Calendar].[Date].Members
,Measures.[days without sales]
)
SELECT { [Measures].[Max days without sales] }
ON COLUMNS
FROM [Adventure Works]
WHERE [Product].[Product].&[486]
The measure days without sales is defined recursively, and returns how many days up to and including the current member of the [Date].[Calendar] hierarchy there was no sales. You may need to adapt the criteria for "without sale", bearing in mind that in MDX, numerical comparisons treat NULL as 0 - which is different from SQL.
This measure only works correctly if there is a member in this hierarchy for each day, i. e. there are no gaps in this hierarchy. And actually, the definition is more general than just working for days: If you use months for the [Date].[Calendar].CurrentMember, it would give you the number of months without sales, etc. It works with each level of the hierarchy.
The measure Max days without sales does not contain the product in its definition, it delivers the maximum days for whatever is in context (in this case the product in the WHERE clause).
Please note that - as actually there is a loop over all days in the [Date].[Calendar] hierarchy when calculating Measures.[Max days without sales], and within that the recursion again iterates along the previous days, and all this for each cell in the result set - this may be slow for large reports.
I have a "Trend" dimension. The hierarchy is, Trend->Week->Day. The fact is linked to the "Day" key. There is no guarantee that measure value will exist for all days in a given week.
When the user wants to see the measure at "Week" level, I need to show only the last empty day value for that Week. I have multiple measures and hence I ain't interested in creating a new calculated measure for each one of them (like How to display the total of a level as the value of its last child in MDX)
Instead, is there any way to create a named set with which I can achieve the functionality as below?
Example
Week Day Measure
1 1 4
1 2 5
2 3 7
2 7 9
3 5 10
Should get at "Week" level as
Week Day Measure
1 2 5
2 7 9
3 5 10
Thanks! :)
If you want to create a calculated member that can be reused for several measures, you can create a utility dimension that will contain a bunch of calculated member only. Dunno how to do that in SSAS (I'm familiar with icCube). Then you can use this hierarchy in your requests to apply the calculated member.
Let's take your example. I've called the utility dimension [Stats]. Its default member is a calculated member returning the value of the current measure. And it contains the [Last Day] calc. member.
WITH MEMBER [Stats].[Stats].[Last Day] AS (
NonEmpty(
Order( [Trend].[Trend].currentMember.children,
[Trend].[Trend].currentMember.properties( 'key', TYPED ),
BDESC
),
[Stats].[Stats].defaultMember
).item(0),
[Stats].[Stats].defaultMember )
SELECT
[Measures].members on 0,
[Trend].[Trend].[Week].members on 1
FROM [your-cube]
WHERE [Stats].[Stats].[Last Month]
You can see the trick with [Last Month] in the slicer that is applied to each [MEasures] of the SELECT. Then its formula is using a NonEmpty of the [Days] (reversed with the order() based on the key - you might need to adjust) for the current [MEasures].