Is there an mdx function that takes a set as an argument and a member function as a separate argument and returns a set with that function applies to the members?
I have a set of selected dates, and want to return a set with the same dates less 1 year, so need to use Parallelperiod on each member.
This approach seems to work ok:
WITH
SET [fewDays] AS
[Date].[Calendar].[Date].&[20070121]
:
[Date].[Calendar].[Date].&[20070127]
SET [theDates] AS
Generate
(
[fewDays] AS s
,{ParallelPeriod
(
[Date].[Calendar].[Calendar Year]
,1
,s.CurrentMember
)}
)
SELECT
{} ON 0
,[theDates] ON 1
FROM [Adventure Works];
I think this is prettier and it avoids Generate so may be faster:
WITH
SET [fewDays] AS
[Date].[Calendar].[Date].&[20070121]
:
[Date].[Calendar].[Date].&[20070127]
MEMBER [Measures].[cntMembers] AS
[fewDays].Count
SET [LastMemLastYear] AS
{
ParallelPeriod
(
[Date].[Calendar].[Calendar Year]
,1
,Tail([fewDays]).Item(0)
)
}
SET [theDates] AS
[LastMemLastYear].Item(0).Lag(
[Measures].[cntMembers])
:
[LastMemLastYear].Item(0)
SELECT
{} ON 0
,[theDates] ON 1
FROM [Adventure Works];
Related
I'm trying to use function LOOKUPVALUE, but I have more than one ID and return error "A table of multiple values was supplied where a single value was expected"
For example:
I need to fill all lines in "Measure_id"
Group_ID
ID
Desc
Measure_ID
112233
0
close
12345
112233
12345
open
12345
112233
0
close
12345
223344
0
close
23456
223344
0
close
23456
223344
23456
open
23456
112233
12345
open
12345
LOOKUPVALUE is not compatible in DAX measure. You can replace it with following.
_return =
VAR _1 =
MAX ( 'fact'[Group_ID] )
VAR _2 =
CALCULATE (
MAX ( 'fact'[ID] ),
ALL ( 'fact' ),
FILTER ( VALUES ( 'fact'[Group_ID] ), 'fact'[Group_ID] = _1 )
)
RETURN
_2
A simpler way of calculating this is by using ALLEXCEPT() in a measure like so:
Measure =
CALCULATE (
MAX ( 'Table'[ID] ) ,
ALLEXCEPT ( 'Table' , 'Table'[Group_ID] )
)
This yields the same result as #smpa01 but is a much more performant measure, around 30-40% quicker on this data set, according to DAX Studio analysis.
I created the following DAX code in DAX Studio which works correctly:
EVALUATE
SUMMARIZECOLUMNS(
'Florida Sightings'[Locality Id],
Hotspot[Subnational 1 Code],
Hotspot[Name],
'Calendar'[Month],
FILTER(Hotspot, Hotspot[Subnational 1 Code] = "US-FL"),
"Species Count", COUNTROWS(VALUES('Florida Sightings'[Common Name]))
)
The output looks like this, sorted by month and species count:
I would like to take the results of the SUMMARIZECOLUMNS and add a rank column based on species count for each locality Id and month. So, for the first locality Id (L127258) and Month (1), the rank would be 1. And, for the second locality Id (L123565) and month (1), the rank would be 2 etc.
The months run from 1 through 12 for each locality.
Without sample data to work with, I'm shooting in the dark a bit but try something along these lines:
ADDCOLUMNS (
SummaryTable,
"Rank", RANKX (
FILTER (
SummaryTable,
[Locality Id] = EARLIER ( 'Florida Sightings'[Locality Id] )
&& [Month] = EARLIER ( 'Calendar'[Month] )
),
[Species Count]
)
)
I have 2 tables with a one-to-many relationship.
-TableGroup: table with groupletter
-TableAll: table with unique identifier, groupletter, a date
Problem: I want to get the max value of the date from TableAll into a new column in TableGroup. See below.
Question: What is the formula for column MAXdate?
TableAll:
ID | Group | date
1 A 4/01/2017
2 A 2/10/2016
3 A 2/06/2016
4 B 2/12/2016
5 B 15/12/2016
6 B 2/03/2017
7 C 5/02/2016
8 C 16/01/2016
TableGroup:
Group | MAXdate
A 4/01/2017
B 2/03/2017
C 5/02/2016
The below formula doesn't work:
See here
The answer is:
CALCULATE (
MAX ( TableAll[Date] ),
FILTER ( TableAll, TableAll[Group] = EARLIER ( TableGroup[Group] ) )
)
Try:
CALCULATE (
MAX ( TableAll[Date] ),
FILTER ( TableGroup, TableGroup[Group] = EARLIER ( TableGroup[Group] ) )
)
How it works:
EARLIER ( TableGroup[Group] ) expression essentially means "current row". Filter function goes row by row over TableGroup table, filters it by current row's group, and then finds max date for that group.
Given the MDX:
select {[Measures].[Effort], [Measures].[Count]} on columns from [Tickets]
.. How can zero (0) values for [Measures].[Effort] be filtered out from the [Measures].[Count] so that the resulting [Measures].[Count] value is reduced by the number of "Tickets" with zero (0) effort?
One would think that it would be easy to filter out values, however that's not the case. The following does not reduce the count of course because the final, single value output is naturally greater than zero (0):
select {[Measures].[Effort], FILTER([Measures].[Count], [Measures].[Effort] > 0 )} on 0
from [Tickets]
.. Also, please assume millions of tickets so placing a ticket ID on axis 1 and then filtering and then summing after the MDX result is returned would not be performant
If the performance is a matter and the following query is too slow:
With
Member [Measures].[RealCount] as
SUM(
IIF(
[Measures].[Effort] > 0,
[Measures].[Count],
Null
)
)
Select
{[Measures].[Effort],[Measures].[Count],[Measures].[RealCount]} on 0
From [Tickets]
You have to filter it out on the DWH to pre-calculate a real count.
I'm not sure of your ticket hierarchy structure so will guess that bit but I would imagine something along these lines:
WITH MEMBER [Measures].[RealCount] AS
SUM(
[Ticket].[Ticket].[Ticket Id],
Iif(
[Measures].[Effort] > 0
,1
,NULL
)
)
SELECT
{
[Measures].[Effort]
,[Measures].[Count]
,[Measures].[RealCount]
} on 0
FROM [Tickets];
If the above gives the correct result then it can be further improved by moving some of the logic to the cube script - this bit:
CREATE HIDDEN SumTicker;
[Measures].[SumTicker] = Iif([Measures].[Effort] > 0,1,NULL);
NON_EMPTY_BEHAVIOR([Measures].[SumTicker]) = [Measures].[Effort];
Then the script becomes:
WITH MEMBER [Measures].[RealCount] AS
SUM(
[Ticket].[Ticket].[Ticket Id],
[Measures].[SumTicker]
)
SELECT
{
[Measures].[Effort]
,[Measures].[Count]
,[Measures].[RealCount]
} on 0
FROM [Tickets];
I need to get the max value of a member property to use in another MDX expression.
As an example from Adventure Works I'm using the following
WITH
MEMBER DoFP AS
[Customer].[Customer].Properties("Date of First Purchase")
MEMBER MaxDoFP AS
Tail
(
NonEmpty
(
[Date].[Date].[Date]
,[Measures].[DoFP]
)
,1
).Item(0).MemberValue
SELECT
[Date].[Calendar Year].[Calendar Year] ON 0
,{
[Customer].[Customer].&[20075]
,[Customer].[Customer].&[15568]
,[Customer].[Customer].&[20285]
} ON 1
FROM [Adventure works]
WHERE
[Measures].[DoFP];
I'd like it to return June 15, 2008 for all rows/cols which is the date of first purchase for Aaron Alexander (who has the max DoFP of the customers selected) so that I can do some more calcs. Instead its giving me 31st Dec 2010 because (I assume) that's the last date in my [Date].[Date].[Date].
Not pretty:
WITH
SET X AS
{
[Customer].[Customer].&[20075]
,[Customer].[Customer].&[15568]
,[Customer].[Customer].&[20285]
}
MEMBER DoFP AS
[Customer].[Customer].Properties("Date of First Purchase")
MEMBER MaxDoFP1 AS
Max(DoFP)
MEMBER MaxDoFP2 AS
Max
(
X
,Cdate([Customer].[Customer].Properties("Date of First Purchase"))
)
SELECT
[Date].[Calendar Year].[Calendar Year] ON 0
,
X * {[Measures].[MaxDoFP1],[Measures].[MaxDoFP2]} ON 1
FROM [Adventure works];
Another option (assuming you have the ability to extend the cube structure) would be to create a measure group based on the customer dimension using a MAX aggregation function. I imagine performance would be a bit better for larger sets.
WITH
SET X AS
{
[Customer].[Customer].&[20075]
,[Customer].[Customer].&[15568]
,[Customer].[Customer].&[20285]
}
MEMBER MaxDoFP AS
Max (X,[Measures].[Customer DoFP MAX])
SELECT
[Date].[Calendar Year].[Calendar Year] ON 0
,X * {[Measures].[MaxDoFP]} ON 1
FROM [Adventure works];