DAX Studio - Filter with NOT(ISNULL([METRIC])) - powerpivot

I'm thinking my problem is coming from not understanding the order of operations DAX uses because its super confusing to me, but here is my problem:
I have a fairly simple query using SUMMARIZE that basically pulls in a field I want and then calculates several metrics - each of which are filtered within the CALCULATE function. But I want to exclude all rows that end up with a null value due to no data being available at that level. I'm not on my computer with the code or I would put exactly what I have here, but a simplified version is basically like this:
EVALUATE
FILTER(
SUMMARIZE(
'Fact Table',
FieldTable[Field1],
'Metric1',
CALCULATE(
[MetricA]
FILTER(
'Fact Table'
[MetricA],
[Year] = 2017
)
),
'Metric2',
CALCULATE(
[MetricB]
FILTER(
'Fact Table'
[MetricA],
[Year] = 2016
)
)
),
NOT(ISNULL([Metric1]))
)
Hopefully I got all that right. I'm don't have DAX studio in front of me to fix my problems so there might be minor errors, but hopefully you get the gist of it.
The problem is that this returns a blank table. If I take out the FILTER surrounding the SUMMARIZE function then everything works exactly like I want it to, except it brings in a ton of blank rows, which is what I'm trying to eliminate with the FILTER on the outside. Any thoughts on how to do that?

I don't know if this is the best solution, but I figured out a solution. Basically, add 0 to my measures and then change the filter to filter on <> 0.
EVALUATE
FILTER(
SUMMARIZE(
'Fact Table',
FieldTable[Field1],
'Metric1',
CALCULATE(
[MetricA]
FILTER(
'Fact Table'
[MetricA],
[Year] = 2017
)
) + 0,
'Metric2',
CALCULATE(
[MetricB]
FILTER(
'Fact Table'
[MetricA],
[Year] = 2016
)
)
),
[Metric1]<>0
)

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.

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 Performance Issues on Count & Sum of Filtered Members/Measures

I Have the following MDX query I need to run on a cube (which I don't have access to change).
This particular query is taking around 1.5 minutes to run, which is just far too long. I've been searching for a way to speed it up, but I'm not having a heap of luck.
Can anyone see a way to improve this query? I've been tearing my hair out for the last couple of days, so any help would be greatly appreciated!
`WITH
MEMBER [Measures].[1-99_Count] AS
COUNT(FILTER ([Scam].[Scam Ref].AllMembers,
([Measures].[Amount Lost]>=1 AND [Measures].[Amount Lost]<=99)))
MEMBER [Measures].[1-99_Amount] AS
SUM(FILTER ([Scam].[Scam Ref].AllMembers,
([Measures].[Amount Lost]>=1 AND [Measures].[Amount Lost]<=99)),
Iif(IsEmpty([Measures].[Amount Lost]),0,[Measures].[Amount Lost]))
SELECT {[Measures].[1-99_Count],
[Measures].[1-99_Amount]} ON COLUMNS,
[First Resolved On Date].[Month].[Month] ON ROWS
FROM [Infocentre]
WHERE ([First Resolved On Date].[Date].[Date].&[20140101]:[First Resolved On Date].[Date].[Date].&[20150623],
[Scam].[Scam Category Level1].&[{d9d6bc38-e73e-e411-9a82-0a713f2121f7}])`
In the end, logic prevailed and I was able to get the cube owners to add a new dimension to the cube, which means I no longer need to try and get this dog's breakfast working.
(just for fun!)
Your first measure can definitely be improved via the following.
Count(Filter is a pattern you can get rid of most of the time.
Mosha blogged about this pattern, which can be improved, here:
http://sqlblog.com/blogs/mosha/archive/2007/11/22/optimizing-count-filter-expressions-in-mdx.aspx
I've also tried to improve the second calculation as well. If this condition is true [Measures].[Amount Lost] >= 1 AND [Measures].[Amount Lost] <= 99 then this must mean that this is false IsEmpty([Measures].[Amount Lost]) so maybe both conditions can be covered by 1 condition. Also replaced your 0 with null - SSAS is a lot happier (& faster) with null:
WITH
MEMBER [Measures].[1-99_Count] AS
Sum
(
[Scam].[Scam Ref].ALLMEMBERS
,IIF
(
[Measures].[Amount Lost] >= 1 AND [Measures].[Amount Lost] <= 99
,1
,null
)
)
MEMBER [Measures].[1-99_Amount] AS
Sum
(
[Scam].[Scam Ref].ALLMEMBERS
,IIF
(
[Measures].[Amount Lost] >= 1 AND [Measures].[Amount Lost] <= 99
,[Measures].[Amount Lost]
,null
)
)
SELECT
{
[Measures].[1-99_Count]
,[Measures].[1-99_Amount]
} ON COLUMNS
,[First Resolved On Date].[Month].[Month] ON ROWS
FROM [Infocentre]
WHERE
(
[First Resolved On Date].[Date].[Date].&[20140101]
:
[First Resolved On Date].[Date].[Date].&[20150623]
,[Scam].[Scam Category Level1].&[{d9d6bc38-e73e-e411-9a82-0a713f2121f7}]
);

filter in a mdx query is not correctly applied

I have a MDX Filter problem, which I think I use correctly however I still get some rows which I don't want to get.
Sample query is :
SELECT {[Measures].[AFR],[Measures].[IB],[Measures].[IC All]} ON COLUMNS,
NON EMPTY ( ([dim_ProductModel].[Product Model].&[DSDB])
* FILTER( ([dim_Country].[Country Name].members -[dim_Name].[Country Key].[All]),[Dim_Date].[Date Full].&[2014-01-01]*[Measures].[IB] > 0 AND NOT ISEMPTY ([Dim_Date].[Date Full].&[2014-01-01]*[Measures].[IB] ))
* {[Dim_Date].[Date Full].&[2013-02-01]:[Dim_Date].[Date Full].&[2014-01-01]})
ON ROWS FROM [cub_dashboard_spares]
Now what I need, is to exclude those countries! (Filter) for particular product model [DSDB] in this case , where in january 2014 (dimension) the measure IB was > 0 or not null. Now it seems that it filters out correctly some countries however I still get some results, where either IB is 0 in last month OR IB is (null) in last month (January 2014 in our case).
Could please anybody help me where can be the problem?
Thank you very much
I would add the country to the filter condition, i. e. use
SELECT {[Measures].[AFR],[Measures].[IB],[Measures].[IC All]}
ON COLUMNS,
NON EMPTY
[dim_ProductModel].[Product Model].&[DSDB]
* FILTER(([dim_Country].[Country Name].members -[dim_Name].[Country Key].[All]),
([dim_Country].[Country Name].CurrentMember, [Dim_Date].[Date Full].&[2014-01-01], [Measures].[IB]) > 0
AND NOT
ISEMPTY(([dim_Country].[Country Name].CurrentMember, [Dim_Date].[Date Full].&[2014-01-01], [Measures].[IB]))
)
* {[Dim_Date].[Date Full].&[2013-02-01]:[Dim_Date].[Date Full].&[2014-01-01]}
ON ROWS
FROM [cub_dashboard_spares]
this is my problem..: don't understand why the results with AFR 0 appear :(

MDX Filter Dimension Members Only

This is probably quite simple but I have a piece of MDX that filters all my customers, that have a balance over 100. This returns a set of tuples containing the customer and the balance. How do i return just a set of the customers?
Filter(
[Customer].[Customer Name].Children,
[Measures].[Balance] > 100
)
I intend to use this as the expression for a named set in my cube.
Thanks in advance :)
Note sure to understand how you get ( customer, balance ) tuples; anyway, you might be looking at the extract function. It allows you to retrieve the tuples with members of the specified hierarchy: e.g.,
Extract( my-set , [Customer].[Customer Name] )