With MDX how to sort by month - mdx

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
...
...

Related

MDX - multiple filters on different dimension with OR condition

I have a problem with MDX querying.
I have one measure WEIGHT and two dimensions DESTINATION and SOURCE with the same attributes: NAME and TYPE.
I want to return:
SUM of WEIGHT
where
DESTINATION.TYPE="A"
**OR**
SOURCE.TYPE="B"
**AND**
(DESTINATION.TYPE **<>** SOURCE.TYPE)
If try this:
SELECT NON EMPTY {
[Measures].[Weight]
}
ON COLUMNS,
NON EMPTY {
([Source].[Name].[Name].ALLMEMBERS * [Destination].[Name].[Name].ALLMEMBERS )
}
ON ROWS
FROM
( SELECT ( { [Source].[Type].&[A] } ) ON COLUMNS FROM ( SELECT ( { [Destination].[Type].&[B] } )
ON COLUMNS FROM [CUBE])) WHERE ( [Destination].[Type].&[B], [Source].[Type].&[A] )
But it doesn't work.
In SQL it look like
Select source.name, destination.name, sum(weight) from cube
where
(source.type = "A" or destination.type = "b")
and
(source.type <> destination.type)
group by source.name, destination.name, source.type, destination.type
Your From section is a bit messy. Try the following:
SELECT
NON EMPTY { [Measures].[Weight] } ON COLUMNS,
NON EMPTY { [Source].[Name].[Name].ALLMEMBERS * [Destination].[Name].[Name].ALLMEMBERS } ON ROWS
FROM [CUBE]
WHERE ( {[Destination].[Type].&[B]} * {[Source].[Type].[All]} + {[Destination].[Type].[All]} * {[Source].[Type].&[A]} )

How to filter set on sum function in calculation mdx?

I want to make calculation in olap cube in ssas. This will be "avg sku by outlets".
There is my expression:
CREATE
MEMBER CURRENTCUBE.[Measures].[Avg Prod Art Id Distinct Count By OldcID] AS
Avg
(
Descendants
(
[dimOutlets].[OLDC ID].CurrentMember
,[dimOutlets].[OLDC ID].[OLDC ID]
)
,[Measures].[Prod Art Id Distinct Count]
);
It works fine, but when I tried to add some logic to it and something went wrong. I want to filter some docs by status or type.
This is what I did:
CREATE
MEMBER CURRENTCUBE.[Measures].[test Calculated Member] AS
Avg
(
NonEmpty
(
NonEmpty
(
{
[dimInvoice].[Doc Type].&[0]
,[dimInvoice].[Doc Type].&[1]
,[dimInvoice].[Doc Type].&[2]
,[dimInvoice].[Doc Type].&[3]
,[dimInvoice].[Doc Type].&[7]
,[dimInvoice].[Doc Type].[All].UnknownMember
}
,{
[dimInvoice].[Status].&[1]
,[dimInvoice].[Status].&[2]
,[dimInvoice].[Status].&[3]
,[dimInvoice].[Status].&[4]
,[dimInvoice].[Status].[All].UnknownMember
}
)
,Descendants
(
[dimOutlets].[OLDC ID].CurrentMember
,[dimOutlets].[OLDC ID].[OLDC ID]
)
)
,[Measures].[Prod Art Id Distinct Count]
) ;
When I try to test this on browser in visual studio, it thinks for a long time and I do not receive a reply.
Is there a best and fast way to do this?
PS. Sorry about my English.
This is work (thanks whytheq), but slowly and now i find solution to do this more faster. Wondering whether there is a possibility to do it a measure of the cube?
CREATE DYNAMIC SET CURRENTCUBE.[DocTypesSet]
AS nonempty
(
{
[dimInvoice].[Doc Type].&[0]
,[dimInvoice].[Doc Type].&[1]
,[dimInvoice].[Doc Type].&[2]
,[dimInvoice].[Doc Type].&[3]
,[dimInvoice].[Doc Type].&[7]
,[dimInvoice].[Doc Type].[All].UnknownMember
}
*{
[dimInvoice].[Status].&[1]
,[dimInvoice].[Status].&[2]
,[dimInvoice].[Status].&[3]
,[dimInvoice].[Status].&[4]
,[dimInvoice].[Status].[All].UnknownMember
}
*Descendants
(
[dimOutlets].[OLDC ID].CurrentMember
,[dimOutlets].[OLDC ID].[OLDC ID]
)
)
CREATE MEMBER CURRENTCUBE.[Measures].[test msr]
AS Sum(DocTypesSet,[Measures].[Prod Art Id Distinct Count] )
Maybe move the set out into a named set:
CREATE
SET CURRENTCUBE.[DocTypes] AS
NonEmpty
(
NonEmpty
(
{
[dimInvoice].[Doc Type].&[0]
,[dimInvoice].[Doc Type].&[1]
,[dimInvoice].[Doc Type].&[2]
,[dimInvoice].[Doc Type].&[3]
,[dimInvoice].[Doc Type].&[7]
,[dimInvoice].[Doc Type].[All].UnknownMember
}
,{
[dimInvoice].[Status].&[1]
,[dimInvoice].[Status].&[2]
,[dimInvoice].[Status].&[3]
,[dimInvoice].[Status].&[4]
,[dimInvoice].[Status].[All].UnknownMember
}
)
,Descendants
(
[dimOutlets].[OLDC ID].CurrentMember
,[dimOutlets].[OLDC ID].[OLDC ID]
)
);
CREATE
MEMBER CURRENTCUBE.[Measures].[test Calculated Member] AS
Avg
(
[DocTypes]
,[Measures].[Prod Art Id Distinct Count]
) ;

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]"
)
)
};

ssas 2005 mdx get total

I have this MDX query
select
{
[Measures].[Sold value]
,[Measures].[Units]
,[Measures].[Sales baskets]
,[Measures].[ATV]
,[Measures].[AUT]
} on columns
, filter(
nonempty(
{[Branch].[Branch].&[5] *[Receipt No - Sales].[Receipt No].[Receipt No]}
),
[Measures].[Sold value] >= 50
) on rows
from Rmis
where [Time].[Day].&[20131218]
Which generates following result:
How can I get the total of these measures of the above result set? The total should use the aggregation defined in the cube.
with set [rows] as
filter(
nonempty(
{[Branch].[Branch].&[5] *[Receipt No - Sales].[Receipt No].[Receipt No]}
),
[Measures].[Sold value] >= 50
)
member [Receipt No - Sales].[Receipt No].[Total] as
aggregate([rows])
select
{
[Measures].[Sold value]
,[Measures].[Units]
,[Measures].[Sales baskets]
,[Measures].[ATV]
,[Measures].[AUT]
} on columns
,
{ ([Branch].[Branch].&[5], [Receipt No - Sales].[Receipt No].[Total]) }
+
[rows]
on rows
from Rmis
where [Time].[Day].&[20131218]

How do you get the total rows in an MDX query to use for paging?

I am attempting to implement paging to large datasets in MDX (SSAS).
I have the following to retrieve paged data which works fine:
SELECT
{
[Measures].[Mesasure1],
[Measures].[Measure2]
} ON COLUMNS,
SUBSET
(
ORDER
(
{
(
[Item].[Category].ALLMEMBERS
)
}, NULL, BASC
), 10, 50 --10 = start index, 50 = returned roes
)
ON ROWS
FROM ( SELECT ( { [Time].[Date].&[2012-04-15T00:00:00]:[Time].[Date].&[2012-04-20T00:00:00] } ) ON COLUMNS
FROM [DataMartPerformance]
))
However I cannot for the life of me find anywhere on the internet that helps explain how to get the total rows available. Do I do it in a seperate query? If so how?
Or can I wrap it into this one query somehow?
Similar to how you'd do TSQL paging, you'll need to run another query to count the total elements. You may have to tinker with this depending on how you've done your original query, but I use something like:
WITH
MEMBER [Measures].[ElementCount] AS
{
NONEMPTY
(
{
[Item].[Category].ALLMEMBERS *
{ [Time].[Date].&[2012-04-15T00:00:00]:[Time].[Date].&[2012-04-20T00:00:00] }
},
{
[Measures].[Mesasure1],
[Measures].[Measure2]
}
)
}.COUNT
SELECT
{
[Measures].[ElementCount]
}
ON COLUMNS
FROM
[DataMartPerformance]
For filtering, you can do dimension filters by using an exists against your dimension attributes:
WITH
MEMBER [Measures].[ElementCount] AS
{
NONEMPTY
(
EXISTS
(
{
[Item].[Category].ALLMEMBERS *
{ [Time].[Date].&[2012-04-15T00:00:00]:[Time].[Date].&[2012-04-20T00:00:00] }
},
{
[Dimension].[Attribute].[FilterByThisAttribute]
}
),
{
[Measures].[Mesasure1],
[Measures].[Measure2]
}
)
}.COUNT
SELECT
{
[Measures].[ElementCount]
}
ON COLUMNS
FROM
[DataMartPerformance]
I haven't got to writing the measure value filters yet, I need to do that next for my own MDX paging constructor...
Please try this:
WITH
SET MySet As
(
NONEMPTY (
[AU Time Sale Hour].[Hour Key].[Hour Key]
* [Dim Country].[Country Key].[Country Key]
)
)
Member [Measures] .cnt AS MySet.Count
select [Measures] .cnt on Columns
from [Me Stats DW Fact Sales]
where (
{[Dim Visa].[Visa Key].&[2067],[Dim Visa].[Visa Key].&[2068] },
[AU Time Sale Date].[Date].&[20091120]:[AU Time Sale Date].[Date].&[20091125]
)