How to get max value using MDX Query - sql

I'm trying to get maximum value for Dimension table where the data as referred with Fact table
WITH
MEMBER [Measures].[Max key] AS
Max
(
[DimAnchorDate].[Date Key].MEMBERS
,[DimAnchorDate].[Date Key].CurrentMember.Member_Key
)
SELECT
{
[Measures].[Max key]
} ON COLUMNS
FROM X;
This query is giving me output : 20141231
In FactTable we have data upto 20141031. From the above query I would like to get 20141031
So now I am trying to get max value from DimAnchordate table as same as it is coming in FactPatientDr Table (i.e 20141031).So please suggest me the best way to achieve this...

I think at the moment you are looking at Dates that are empty in certain parts of the cube space - try forcing to nonempty by using a measure from the FactPatientDr
WITH
MEMBER [Measures].[Max key] AS
Max
(
nonempty(
[DimAnchorDate].[Date Key].MEMBERS
,[Measures].[SomeMeasureInFactPatientDr]
)
,[DimAnchorDate].[Date Key].CurrentMember.Member_Key
)
SELECT
{
[Measures].[Max key]
} ON COLUMNS
FROM X;

I think we can use Tail function to get desired result with measure value as well:
select non empty([Measures].[Entered Case],
tail
(Order
([Calendar].[Date].[Date].members
,[Measures].[Entered Case]
,basc
)
,1
)) on 0 from [SolutionPartner]

Related

custom count measure runs forever MDX

So this question goes off the one here
I've been trying to do a similar count measure and I did the suggested solution but it's still running.... and it's been more than 30 minutes with no results, while without that it runs in under a minute. So am I missing something? Any guidance would help. Here is my query:
WITH
MEMBER [Measures].[IteractionCount] AS
NONEMPTY
(
FILTER
(
([DimInteraction].[InteractionId].[ALL].Children,
[Measures].[Impression Count]),
[DimInteraction].[Interaction State].&[Enabled]
)
).count
SELECT
(
{[Measures].[IteractionCount],
[Measures].[Impression Count]}
)
ON COLUMNS,
(
([DimCampaign].[CampaignId].[CampaignId].MEMBERS,
[DimCampaign].[Campaign Name].[Campaign Name].MEMBERS,
[DimCampaign].[Owner].[Owner].MEMBERS)
,[DimDate].[date].[date].MEMBERS
)
ON ROWS
FROM
(
SELECT
(
{[DimDate].[date].&[2020-05-06T00:00:00] : [DimDate].[date].&[2020-05-27T00:00:00]}
)
ON COLUMNS
FROM [Model]
)
WHERE
(
{[DimCampaign].[EndDate].&[2020-05-27T00:00:00]:NULL},
[DimCampaign].[Campaign State].&[Active],
{[DimInteraction].[End Date].&[2020-05-27T00:00:00]:NULL}//,
//[DimInteraction].[Interaction State].&[Enabled]
)
I don't know if FILTER is affecting it in any way but I tried it with and without and it still runs forever. I do need it specifically filtered to [DimInteraction].[Interaction State].&[Enabled]. I have also tried to instead filter to that option in the WHERE clause but no luck
Any suggestions to optimize this would be greatly appreciated! thanks!
UPDATE:
I end up using this query to load data into a python dataframe. Here is my code for that. I used this script for connecting and loading the data. I had to make some edits to it though to use windows authentication.
ssas_api._load_assemblies() #this uses Windows Authentication
conn = ssas_api.set_conn_string(server='server name',db_name='db name')
df = ssas_api.get_DAX(connection_string=conn, dax_string=query))
The dax_string parameter is what accepts the dax or mdx query to pull from the cube.
Please try this optimization:
WITH
MEMBER [Measures].[IteractionCount] AS
SUM
(
[DimInteraction].[InteractionId].[InteractionId].Members
* [DimInteraction].[Interaction State].&[Enabled],
IIF(
IsEmpty([Measures].[Impression Count]),
Null,
1
)
)
SELECT
(
{[Measures].[IteractionCount],
[Measures].[Impression Count]}
)
ON COLUMNS,
(
([DimCampaign].[CampaignId].[CampaignId].MEMBERS,
[DimCampaign].[Campaign Name].[Campaign Name].MEMBERS,
[DimCampaign].[Owner].[Owner].MEMBERS)
,[DimDate].[date].[date].MEMBERS
)
PROPERTIES MEMBER_CAPTION
ON ROWS
FROM
(
SELECT
(
{[DimDate].[date].&[2020-05-06T00:00:00] : [DimDate].[date].&[2020-05-27T00:00:00]}
)
ON COLUMNS
FROM [Model]
)
WHERE
(
{[DimCampaign].[EndDate].&[2020-05-27T00:00:00]:NULL},
[DimCampaign].[Campaign State].&[Active],
{[DimInteraction].[End Date].&[2020-05-27T00:00:00]:NULL}//,
//[DimInteraction].[Interaction State].&[Enabled]
)
CELL PROPERTIES VALUE
If that doesn’t perform well the please describe the number of rows returned by your query when you comment out IteractionCount (sic) from the columns axis. And please describe how many unique InteractionId values you have.

MDX sort order in Crossjoin

I want to show a list of callers we have missed calls from within a daterange. I want te result to be ordered by date. But I can't figure out how to do this.
My MDX statement:
With
Member [Measures].[Gemist] AS
sum(
except({[CM resultaat].[Resultaat].[CM resultaat].allmembers},
{[CM resultaat].[Resultaat].[CM resultaat].[answer],[CM resultaat].[Resultaat].[CM resultaat].[answer overflow]}),
[Measures].[SN Gesprekken]
)
Select
order([Measures].[Gemist],[Datum].[Datum].currentMember.value, ASC) on 0,
nonempty(crossjoin(Hierarchize([ServiceNummer ANI].[Ani]),[Datum].[Datum].[Dag]),[Measures].[Gemist]) on 1
FROM (SELECT {[datum].[datum].[dag].[2020-04-01]:[datum].[datum].[dag].[2020-04-28]} ON 0 FROM [Cube])
After some google searches I tried this to order the measure by date but also tried to order the crossjoin. Output stays te same, no order on date:
Anyone has a solution for this?
You need to order the vertical axis (i.e., the axis 1):
order(
nonempty(
crossjoin( [ServiceNummer ANI].[Ani], [Datum].[Datum].[Dag] )
),
[Datum].[Datum].currentMember.key,
BASC
) on 1
using the key (or name) of the current member of the [Datum] dimension.
Hope that helps.

MDX name set not working in calculated member

I have a named set like this :
(
[DimDate].[Hierarchy].currentmember.lag(1)
,
[Measures].[Fact Transaction Count]
)
This code give me count of transaction of yesterday current member. now I want to use it to calculate daily growth. some thing like this :
(
([Measures].[Fact Transaction Count] - GetPreviousTransactionValueDate.Item(0).Item(0)) / GetPreviousTransactionValueDate.Item(0).Item(0)
) * 100
when use it in SSMS I can get output but when I send it to SSDT I get a Null value for all hierarchy.
this code give me output :
with member measures.GetPreviousTransactionValueDate as
(
[DimDate].[Hierarchy].currentmember.lag(1)
,
[Measures].[Fact Transaction Count]
)
member measures.Roshd as
(
([Measures].[Fact Transaction Count] - measures.GetPreviousTransactionValueDate) / measures.GetPreviousTransactionValueDate) * 100
)
select non empty( measures.Roshd) on columns,
non empty([DimDate].[Hierarchy].[Month]) on rows
from [Cube]
I want to only use named Set not another calculated member for this topic. any help?
Finally I found that my way is wrong. because sets works on dimensions not use for creating a calculated member.

Getting results for even years only

MDX has a nice feature whereby I can specify a range of members:
SELECT ([Canada],[2006]:[2011]) on Rows,
[Measures].members on Columns
FROM [Sales]
Is there a way to calculate the set of even years: {[2006], [2008], [2010]}? I am looking for a way that would work for large sets, so that listing the years manually is not an option.
You can filter you function using a filter function, a declared function and MOD function (MOD returns the remainder from the division - like % in Java ) :
WITH
FUNCTION isEven(Value _number) as Mod( Int(_number) , 2 ) = 0
SELECT
FILTER( [Date].[Date].[Year] as t, isEven( Year( t.current.key) ) ) on 0
FROM [Cube]
If you are using this filter often you could create a FilterEven declared function once in the script (same for isEven() )
Try this. I used adventure works for the query.For the mod logic i took help from
Mod Logic
WITH
MEMBER [Measures].[Data Type] AS
[Date].[Day of Year].CurrentMember.Properties ("Member_Value",TYPED)
MEMBER [Measures].[IsEven] as
[Measures].[Data Type]-Int([Measures].[Data Type]/2)*2
select {[Measures].[Internet Order Count] }
on columns,
filter (
[Date].[Day of Year].[Day of Year],
[Measures].[IsEven]=0)
on rows
from [Adventure Works]
Plus you can have a column in the date dimension have 1,0 to indicate if the year is even or odd. Then simply use that column in the MDX query , no need to do all the above manipulations

MDX: I need only those highlighted records

I need only those highlighted records.
SELECT
[Measures].[Assessment Patients Detail] ON COLUMNS,
NON EMPTY([DimAssessment].[Assessment Text].&[Employee Wellness HRA],
[DimAssessment].[Question Text].&[Do you use tobacco products?],
[DimPatient].[Patient Key].[Patient Key],
Generate(
[DimAssessment].[Answer Text].[Answer Text].MEMBERS
,[DimAssessment].[Answer Text].CURRENTMEMBER
*TAIL(
NonEmpty(
[DimDate].[Full Date Alternate Key].[Full Date Alternate Key].MEMBERS
,[DimAssessment].[Answer Text].CURRENTMEMBER
)
,[DimAssessment].[Question Text].&[Do you use tobacco products?]
)
)
)ON ROWS
FROM
[Care];
This probably isn't the answer as I've tried to help with this before but the following is a little mysterious:
The second argument of the function Tail is usually an integer e.g. 2 or 3 which means you would like the last 2, or last 3, members from the set specified in first arg of Tail.
I prefer to use cross-join rather than a tuple for members on rows but I don't think this will give you the rows you require:
SELECT
[Measures].[Assessment Patients Detail] ON COLUMNS,
NON EMPTY
{[DimAssessment].[Assessment Text].&[Employee Wellness HRA]}
*{[DimAssessment].[Question Text].&[Do you use tobacco products?]}
*[DimPatient].[Patient Key].[Patient Key].members
*Generate(
[DimAssessment].[Answer Text].[Answer Text].MEMBERS
,[DimAssessment].[Answer Text].CURRENTMEMBER
*TAIL(
NonEmpty(
[DimDate].[Full Date Alternate Key].[Full Date Alternate Key].MEMBERS
,[DimAssessment].[Answer Text].CURRENTMEMBER
)
,1 //[DimAssessment].[Question Text].&[Do you use tobacco products?]
)
)
ON ROWS
FROM
[Care];
We will need to move more of the logic inside the generate.
First try the following to see if it returns two correct columns?
SELECT
[Measures].[Assessment Patients Detail] ON COLUMNS,
NON EMPTY
Generate(
[DimPatient].[Patient Key].[Patient Key].MEMBERS
,[DimPatient].[Patient Key].CURRENTMEMBER
*TAIL(
NonEmpty(
[DimDate].[Full Date Alternate Key].[Full Date Alternate Key].MEMBERS
,[DimPatient].[Patient Key].CURRENTMEMBER
)
,1
)
ON ROWS
FROM
[Care];