How to create calculated measure lastchild in Kylin MDX? - mdx

Colleagues, good afternoon!
There is no lastchild type for measures by default in Kylin. Therefore, a problem arises when calculating such indicators as the stock. Monthly stock = stock on the last day of the month, stock for the year = stock on the last day of the year, stock for several days = stock on the last of these days etc.
To solve this problem, the following MDX script was developed:
IIF(
[01 DATE].[Y-Q-M-D-Hierarchy].LEVEL IS [01 DATE].[Y-Q-M-D-Hierarchy].[MONTH]
,SUM([01 DATE].[Y-Q-M-D-Hierarchy].CurrentMember.LastChild,[Measures].[STOCK QTY])
,IIF(
[01 DATE].[Y-Q-M-D-Hierarchy].LEVEL IS [01 DATE].[Y-Q-M-D-Hierarchy].[QUARTER]
,SUM([01 DATE].[Y-Q-M-D-Hierarchy].CurrentMember.LastChild.LastChild,[Measures].[STOCK QTY])
,IIF(
[01 DATE].[Y-Q-M-D-Hierarchy].LEVEL IS [01 DATE].[Y-Q-M-D-Hierarchy].[YEAR]
,SUM([01 DATE].[Y-Q-M-D-Hierarchy].CurrentMember.LastChild.LastChild.LastChild,[Measures].[STOCK QTY])
,[Measures].[STOCK QTY]
)
)
)
This MDX script solves the problem:
Monthly stock = stock on the last day of the month.
Stock for the year = stock on the last day of the year.
But with a multi-select of hierarchy elements, the sum of the Stock is calculated, and not the last child (attached a screenshot).
How can I fix the calculation of stock in multiple selection?

Related

Converting MDX query into SSAS cube expression [Calculations]

How can I go around to amending this MDX query
WITH MEMBER [Measures].[PanelSoldMonthsPre]
AS ' [In Charge Date].[In Charge Date Days in Month].CURRENTMEMBER.membervalue'
MEMBER [Measures].PanelSoldMonths
AS [Measures].[Panel Sold Days] / [Measures].[PanelSoldMonthsPre]
SELECT
[Measures].[Panel Sold Days], [Measures].PanelSoldMonths} ON COLUMNS,
NON EMPTY
([In Charge Date].[In Charge Year].Children, [In Charge Date].[In Charge Date Days in Month].Children) ON ROWS
FROM [Cube]
to..
a) bring back Year Month and corresponding number of days in month. At the moment its returning just the unique [In Charge Date Days In Months].
I want it to return the [In Charge Date Days In Months] for all 12 months for each [In Charge Date].[In Charge Year]
b) placing it as an expression in the calculations (calculated measures) section of the cube project. I decided to create them as 2 separate members. Clearly does not work since I get a return of [Measures].[PanelSoldMonthsPre] '#VALUE!' on the cube (excel workbook)

Calculated member in SSAS based on custom Last Year Date (Retail calendar)

I have spent a lot of time solving this issue, but no success yet.
I have a Date dimension called [Dim Date] and in this table I have a [Date Key] which contains all the dates from '2013-07-01' to '2016-12-31'. I also have a measure called [Retail Revenue] which is a decimal number for that date. So far so easy!
We are a retail Calendar and all our calculations/comparisons are based on a retail calendar (which is a customized table in DW).
The date hierarchy in this calendar is as below (please see the screenshot):
-- retail year (e.g. 2017)
---- retail half year (e.g. 2017-H1)
------ retail quarter year (e.g. 2017-Q1)
-------- retail month (e.g. 201702) (months from 201701 to 201712)
---------- retail week (e.g. 201708) (weeks from 201701 to 201752)
------------ date key (e.g. 2016-08-22)
SCREENSHOT
We also have an attribute called "Retail Last Year Date" which shows the equivalent date of last calendar date (e.g. 2015-08-24 in the screenshot).
I need to have a calculated member showing the "Retail Revenue" for last year (based on the attribute "Retail Last Year Date"), next to the regular "Retail Revenue" for [date key].
I tried to use ParallelPeriod and Scope, and could not get the numbers properly. Probably an easy task but I am not a hero in mdx unfortunately!
Will be more than thankful if anyone please can help me out with this.
Thanks
Rez
Hopefully this helps.
This is against AdvWrks. I've created a query scoped custom measure that gets the internet sales from the year before:
WITH
MEMBER [Measures].[Internet Sales Amount LastYear] AS
(
ParallelPeriod
(
[Date].[Calendar].[Calendar Year]
,1
,[Date].[Calendar].CurrentMember
)
,[Measures].[Internet Sales Amount]
)
SELECT
{
[Measures].[Internet Sales Amount]
,[Measures].[Internet Sales Amount LastYear]
} ON 0
,
[Date].[Calendar].[Date].&[20070121]
:
[Date].[Calendar].[Date].&[20070127] ON 1
FROM [Adventure Works];
Here are the results:
Thanks for your response, but I didn't get how it uses the dimension property? because my calendar is customized and I cannot assume the last year calendar date is the same as this year. I have to read it from the dimension property "Retail Last Year Date").
Thanks

SSAS DAX time intelligence previous year and calculation gives wrong value

We want to show current periods sales versus previous period sales.
To show the previous period we make use of a date dimenion table and the following calculation:
CALCULATE(SalesValueGross; DATEADD(Date[Date]; -1; YEAR))
Unfortunately somehow it shows minor (decimal) differences when comparing years.
The difference get's bigger when we slice to months.
Another issue we have is that this calculation does not seem to work when comparing (for example) week 1 - 2015 with week 1 - 2014.
Any help is greatly appreciated!
When I want to get prior calendar year sales, I use the a formula such as the following:
Cal Prior Yr Sales:=if(HASONEVALUE('Sale Date'[Calendar Year]),
Calculate([Total Sales],
PREVIOUSYEAR('Sale Date'[Date])),BLANK())
The HASONEVALUE just ensures that there is only one year selected so it will know the correct previous year to retrieve.
You can make a series of calculations that will let you use one calc that determines what level of the date hierarchy you are in (assuming you have the fields available in your date table). Here is something I've used in the past, with a fiscal calendar that was different from the normal calendar.
First, the base calculations:
Sales Same Week Prior Year:=
CALCULATE([Total Sales],Filter(All('Sale Date'),
'Sale Date'[Week Key] = max('Sale Date'[Same Week Last Year])))
Sales Same Month Prior Year:=CALCULATE([Total Sales], Filter(All('Sale Date'),
'Sale Date'[Month Seq] = max('Sale Date'[Month Seq])-12))
Sales Same Quarter Prior Year:=CALCULATE([Total Sales], Filter(All('Sale Date'),
'Sale Date'[Quarter Seq] = max('Sale Date'[Quarter Seq])-4))
Sales Prior Year:=CALCULATE([Total Sales], Filter(All('Sale Date'),
'Sale Date'[Fiscal Year] = max('Sale Date'[Fiscal Year])-1))
You can hide all of those calculations and then create one last calculation and leave it visible:
Sales Same Period Last Year:=
if(HASONEVALUE('Sale Date'[Week Key]), [Sales Same Week Prior Year],
if(HASONEVALUE('Sale Date'[Month Key]),[Sales Same Month Prior Year],
if(HASONEVALUE('Sale Date'[Quarter Key]),[Sales Same Quarter Prior Year],
if(HASONEVALUE('Sale Date'[Fiscal Year]), [Sales Prior Year], BLANK()))))
You may need to add a couple of calculated fields to your date table to make it work. I have fields for: [Same Week Last Year], [Month Seq], [Quarter Seq]. Same week last year is an integer field that is yyyyww. Month Seq and Quarter Seq are just autoincrementing integers in chronological order that do not repeat.
My formula for same week last year is
=if('Sale Date'[Week Nbr] = 53, (('Sale Date'[Fiscal Year]-1) * 100) + ([Week Nbr]-1),
(('Sale Date'[Fiscal Year]-1) * 100) + ([Week Nbr]))
I did the sequence numbers in my SQL view, which is the source for the date date. As an example, if my date table starts at 1/1/2010, the month seq for Jan 2010 is 1 and the month seq for Jan 2011 is 13. The quarter seq for Q1 2010 is 1 and the quarter seq for Q1 2012 is 9.
http://www.daxpatterns.com/time-patterns/ is a good read for this topic.

MDX query producing #Error as output

In my SSAS Cube, I have a measure called [Sales Total]. What I want to do is to create another measure that would give me the lowest sales figure in the last 6 months. I want this to be a moving minimum, calculated as the min of sales of every time period from the present month to 6 months back.
I wrote my MDX statement but it produces an error and I have hard time trying to figure out why. It is something to do with aggregating Date dimension members into a filter aggregate.
When i choose a single month member from the Date hierarchy, it returns the correct value. When I select multiple members from the hierarchy, as seen below, it errors out.
Any kind of help is appreciated.
WITH
MEMBER [Measures].[Min Sales Total Rolling 6 months] as '(MIN([Date].[Fiscal Month Hierarchy].currentmember.lag(6):[Date].[Fiscal Month Hierarchy].currentmember,[Measures].[Sales Total]))'
MEMBER [Date].[Fiscal Month Hierarchy].[FilterAggregate] as
'AGGREGATE({
[Date].[Fiscal Month Hierarchy].[Quarter].&[20141].&[201310],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20141].&[201311],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20141].&[201312],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20142].&[201401],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20142].&[201402],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20142].&[201403],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20143].&[201404],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20143].&[201405],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20143].&[201406],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20144].&[201407]
})'
SELECT {
[Measures].[Min Sales Total Rolling 6 months]} ON AXIS(0)
FROM [My Cube]
WHERE ([Date].[Fiscal Month Hierarchy].[FilterAggregate])
As explained in this blog by one of the SSAS developers already many years ago, multi select does not work with CurrentMember. You should use
MEMBER [Measures].[Min Sales Total Rolling 6 months] as
MIN(Tail(EXISTING [Date].[Fiscal Month Hierarchy].[Fiscal Month Hierarchy].Members).Item(0).Item(0).lag(6)
:
Tail(EXISTING [Date].[Fiscal Month Hierarchy].[Fiscal Month Hierarchy].Members).Item(0).Item(0),
[Measures].[Sales Total])
instead.
EXISTING gets the set of all selected members. This is needed as there is no single CurrentMember. Then Tail gets the set consisting of the last of these members, Item(0).Item(0) converts that single element set to a member.

MDX query to display this year's sale along with the last year's sale

Here's my query. I want to display this year's data from week 12 to week 24 along with last year's data from week 12 to week 24. But I got the same data for both years even I had the "prevMember". Could you help me?
WITH MEMBER [Measures].[Net Sales This Year] AS ([Measures].[Ticket Net Sales],[Date].[Fiscal].[Fiscal Year].currentmember.prevmember)
SELECT {[Measures].[Ticket Net Sales],[Measures].[Net Sales Last Year]} on 0,
NON EMPTY {([Concepts].[Concept Name].[Concept Name],[Locations].[Department Name].[Department Name],
[Date].[Fiscal].[Fiscal Year].Members,[Date].[Week Of Year].&[12]:[Date].[Week Of Year].&[24])} on 1
FROM spbi
You can use the PARALLELPERIOD() function to achieve this.
See http://technet.microsoft.com/en-us/library/ms145500
Could you try this query:
WITH MEMBER [Measures].[Net Sales This Year] AS ([Measures].[Ticket Net Sales],[Date].[Fiscal].CurrentMember.PrevMember)
SELECT {[Measures].[Ticket Net Sales],[Measures].[Net Sales Last Year]} ON 0,
NON EMPTY [Date].[Fiscal].[Fiscal Year].Members * [Date].[Week Of Year].&[12]:[Date].[Week Of Year].&[24]) ON 1
FROM spbi