I'm working on Power BI with a live connection SSAS cube. I have two dimension tables, Customer and Year and a fact table. I want to generate a report which shows the proportion of sales amount by citizenship for each Year. I used this MDX expression on SSAS Cube to create a new calculated member.
(([Year].[Year].currentmember,[Dim Customer].[Cittzenship].currentmember),[Measures].[Totale Sales]) /([Cittzenship].currentmember.parent,[Measures].[Totale Sales])
The result of the query works as expected but on power BI report table the total value is infinity.
Result
I'm stuck solving this problem. I would love if somebody guide me how to solve this.
The reason is probably that for the totals, the parent does not exist, and hence, its value is replaced by null for the calculation. In most contexts, SSAS replaces null by 0, and hence the division is by 0, which mathematically is infinity. What you can do is use the Divide function instead of /, which is exactly meant for this purpose, as it delivers the same result as /, except if the divisor is zero, in which case it delivers null.
By the way: Divide was introduced in SSAS 2012, so if you are not on that version at least, you would have to use a construct like IIf(divisor <> 0, dividend / divisor, null), which is exactly how Divide(dividend, divisor) is defined. Like I said
above, in MDX in contrast to SQL, comparing to 0 also implicitly compares to null.
Related
I have a requirement where I have to get the values of different measure with each measure having its own date range, the following query works and gets me the data over an year,
WITH MEMBER [Measures].[Ex1] AS ([Measures].[Average Spending], [Date].[Year].&[2020])
MEMBER [Measures].[Ex2] AS ([Measures].[Average Time Spent], [Date].[Year].&[2019])
SELECT {[Customer].[Name].[Name]} ON 1, {[Measures].[Ex1],[Measures].[Ex2]} ON 0 FROM [Model];
but when I try to pass a date range instead of year, I'm getting an error,
WITH MEMBER [Measures].[Ex1] AS ([Measures].[Average Spending], [Date].[Year].&[2020])
MEMBER [Measures].[Ex2] AS ([Measures].[Average Time Spent], [Date].[Date].[01-May-2020]:[Date].[Date].[31-May-2020])
SELECT {[Customer].[Name].[Name]} ON 1, {[Measures].[Ex1],[Measures].[Ex2]} ON 0 FROM [Model];
I'm getting the following error,
Executing the query ... The function expects a string or numeric
expression for the argument. A tuple set expression was used. Run
complete
How do I get the measure for a date range for each calculated member?
If you do not use a tuple (i. e. a combination of single members of the hierarchies like Measures or Year), but a set, then you need to tell MDX what to do with the set elements to get to a single value. I would assume the following will give what you want:
WITH
MEMBER [Measures].[Ex2] AS
Aggregate([Date].[Date].[01-May-2020]:[Date].[Date].[31-May-2020],
[Measures].[Average Time Spent]
)
...
Instead of Aggregate, you could also use Sum, Avg, Variance etc. but normally, I prefer using Aggregate, as this uses whichever aggregation method is defined for the measure in the cube, i. e. it is more universal, and the query does not need to be adapted to changes in the cube calculation scripts.
See also Aggregate in the SSAS documentation
This is a question regarding SSAS Cubes, MDX formulas and Power BI.
I have a measure with the active members per each month. So when I select for example 2018 it shouldn´t aggregate but return the last available month with active members, and if I break down by month it should give the active members for each month.
So I have this formula which works almost fine if querying in MS Management Studio:
with member [Measures].[Last existing SOCIOS] AS
Max(
EXISTING [DIM FECHA].[Jerarquía].[MES NOMBRE].members,
iif([Measures].[ACTIVOS] = 0,null,
[Measures].[ACTIVOS])
)
select {[Measures].[Last existing SOCIOS]} on columns,
[DIM FECHA].[MES NOMBRE].members on rows
from [cubo_Compromisos]
where [DIM FECHA].[AÑO].&[2018]
I would prefer to have the november value returned at the 'All' level. But this is not my main problem. The real issue is that when I use this measure in Power BI it behaves differently: when selecting multiple months it ignores the selected values and just returns the last value for the whole year.
In the screenshot below I have added the value returned by the KPI Card because that is the value that I want returned:
If I select items like this it does it right, but I need it to select all months, and not just one because I am using this measure along others:
Does anyone know the right MDX function to use or an alternative?
Edited: 23-11-2018
It does the same in a Pivot Table connected to a SSAS Cube.When I add the date dimension to the table it works fine. But when using the date dimension and filtering it without the dimension added as rows it returns the value for the whole year.
The function you are looking at is LastChild. Last Child on the upper level of the hierarchy will return the value you are looking at.
I think that function can be used in the Cube design in SSAS - then this will be the standard behavior. If you want to do it with a query you need to do something like:
SELECT [Date].[Fiscal].[Fiscal Quarter].[Q1 FY 2002].LastChild ON 0
FROM [Adventure Works]
To get the last month of the 1st quater (I used example from microsoft and another post on the subject )
I'm trying to create an SSAS calculated measure off of an inventory cube. The underlying data contains an inventory_date, a product code, and some inventory values for the product for that date. I am trying to create a measure which will return the first inventory value for the month, for the product (and overall). I've seen people try to use the head function to no avail. I've also just seen a reference to the openingperiod function, which seems to be exactly what I want, but it also isn't returning the correct value. Both of them, FWIW, are returning the last value in the month rather than the first.
Here is a query that I am issuing:
select
[Measures].[Qty On Hand EOD] on 0,
{openingperiod([Inventory Date].[Calendar].[Month]):
[Inventory Date].[Calendar].[Month].&[2018-06-25T00:00:00]
} on 1
from [Daily Inventory]
This query returns for May 2018 the value of [Qty On Hand EOD] for May 31, rather than May 1, which is what I want. What am I doing wrong?
FWIW, in the cube definition the AggregateFunction property for [Qty On Hand EOD] is set to LastNonEmpty (as opposed to Sum or Avg). Could that play a role?
Would much appreciate any help on this.
I have a measure called "Sales" populated with values, however i am trying to turn the "Sales" value to 0, whenever the "Sales Flag" is set to 0.
Important Note: The Sales Flag is based on Date (lowest level of detail).
The difficulty that i am really experiencing and cant get a grip on, is how i am trying the display the MDX outcome.
As explained above, i would want to make the "Sales" value 0 whenever we have a 0 in the "Sales Flag" (which is based on the Date), but when I run the MDX Script I would wan't on the ROWS to NOT display the Date, but instead just the Week (higher Level to Date), as below shows:
I really have spent hours on this and can't seem to understand how we can create this needed custom Sales measure based on the Sales Flag on the date level, but having the MDX outcome display ROWS on Week level.
Laz
You need to define the member in the MDX before the select. Something like that:
WITH MEMBER [Measures].[Fixed Sales] as IIF([Sales Flag].currentMember=1,[Sales], 0)
SELECT [Measures].[Fixed Sales] on 0, [Sales Flag] on 1 from [Cube]
I am writing the code without SSAS here so it might not be the 100% correct syntax but you can get the general idea ;)
You can add the iif in the SELECT part but I find creating member to be the cleaner solution.
SELECT IIF([Sales Flag].currentMember=1,[Sales], 0) on 0, [Sales Flag] on 1 from [Cube]
If you have a control over the cube in SSAS you can create a calculated member there and you can access it easier.
Glad to hear if Veselin's answer works for you, but if not...
Several approaches are also possible.
Use Measure expression for Sales measure:
Use SCOPE command for Day level (if it's Key level of Date dimension). If it's not a key level you have to aggregate on EVERY level (week, year etc) to emulate AggregateFunction of Sales measure but with updated behavior for one flag:
SCOPE([Date].[Your Date Hierarchy].[Day].members,[Measures].[Sales]);
THIS=IIF([Sales Flag].CurrentMember = 1,[Measures].[Sales],0);
END SCOPE;
Update logic in DSV to multiply Sales column by SalesFlag. This is the easiest way from T-SQL perspective.
How can i ignore repeated days values using MDX ?
For example, from this:
12/1/1997
12/2/1997
12/3/1998
12/4/1999
To this:
12
I'm already tried use Distinct, but i'ts like they still considere as unique dates.
SELECT
{} ON COLUMNS,
DISTINCT([Time].[Weekly].[Day].Members) ON ROWS FROM [Sales 2]
These are different values and your cube treats them as such. I guess you can do some mimicing of that behavior with MDX by creating a MEMBER which has just the date part and doing distinct on that member. Something like:
WITH MEMBER [MonthPart] AS LEFT([Time].[Weekly].[Day].currentMember, 2)
SELECT
{} ON COLUMNS,
DISTINCT([MonthPart].Members) ON ROWS FROM [Sales 2]
Something like that but I am not sure for the correct syntax.
The proper way to fix that though is to create a good Time Dimension for your dates in the cube. There you can define hierarchies, levels and just take the [Month] part which will give you what you want (and will be much faster than writing complex MDX queries)