IIF THEN in MDX for handling ∞ values - reporting-services

I am getting an
∞
value in my calculated measures.
I want to handle this value, & if it comes, it should display 0.
What I tried is,
1. IIF([Meaures].[Plant Share] = "∞" , 0, [Meaures].[Plant Share])
2. IIF([Meaures].[Plant Share] = "Infinity" , 0, [Meaures].[Plant Share])
Both seems not working.
In query output, I am getting ∞ as Infinity.
Any Idea how should I ?
Thanks.
Bytheway, to type ∞,
Windows ALT + 2 3 6
Macintosh Inser --> Symbol -> ∞
UPDATE:
This way I calculate the member.
WITH
MEMBER [Meaures].[Plant Share] AS(
([Meaures].[YTD Plant Share] / [Meaures].[PY YTD Plant Share] - 1 ) * 100
)

Change your measure to the following:
WITH
MEMBER [Meaures].[Plant Share] AS(
iif
([Meaures].[PY YTD Plant Share] = 0,
null,
([Meaures].[YTD Plant Share] / [Meaures].[PY YTD Plant Share] - 1 ) * 100
)
)

Awesome trick I found for this situation.
Simply use Divide function, newly introduced.
Divide (<numerator>, <denominator> [,<alternateresult>])
For more details, refer here.

Related

SSRS number of occurences for runningvalue

I'm trying to create the ABC / Pareto Analysis. Intention is to mark by 'A' all products which generate 80% of total sales, 'B' products which generate additional 15% and 'C' the rest. It's up to 100k products which have to be taken into account and fastest way to calculate running total is by using the RunningValue in the Report Builder / Paginated Reports.
I did some testing by calculating running total while downloading data to the dataset but it's very slow and therefore not an option. RunningValue on the other hand works perfectly fine and I can mark the products by 'A', 'B', and 'C' in the Tablix. This is the formula:
=IIF(RunningValue(Fields!ACT.Value, Sum, Nothing ) / Sum(Fields!ACT.Value, "Facts")<0.8, "A", IIF(RunningValue(Fields!ACT.Value, Sum, Nothing ) / Sum(Fields!ACT.Value, "Facts")<0.95,"B","C"))
However, I can't find a way how to count the number of products with A, B and C. Idea is to show that X% of products create 80% of total sales, Y% additional 15% and Z% generate only 5% of turnover.
I'd be thankful for any advice, I'm running out of options. Original datasource is Power BI dataset and calculating running total while downloading the data is really slow for this number of products. Tested with MDX and DAX.
You can use custom code for your pareto count calculation.
Add the following code to your report
Public Dim a_count, b_count, c_count As Integer
Public Function Pareto( current As Integer, total As Integer) As String
If current / total < 0.8 Then
a_count = a_count + 1
Return "A"
ElseIf current / total < 0.95 Then
b_count = b_count + 1
Return "B"
Else
c_count = c_count + 1
Return "C"
End If
End Function
For your parent classification column use the expression
= Code.Pareto( RunningValue(Fields!ACT.Value, Sum, Nothing ) , Sum(Fields!ACT.Value, "Facts"))
To display the counter variables use the expressions
= Code.a_count
= Code.b_count
= Code.c_count

Get value between from to dataset columns ssrs

I have a data set like that:
Data Set Contents
From To Comment
----+---+--------
0 50 Bad
50 70 Good
70 100 Excellent
If I have a value of 75, I need to get Excellent by searching the Dataset.
I know about the lookup function but it is not what I want. How can I do that?
The values should be in percentage.
Note : the value (75) is Average of a column (Calculated) it
calculate student grade from max and student mark Version SQL Server
2016
Note 2 : the dataset is from database not static values
Thank You
Assuming you only ever have a fixed number of 'grades' then this will work. However, I would strongly recommend doing this type of work on the server where possible.
Here we go...
I created two datasets
dsGradeRange with the following sql to recreate your example (more or less)
DECLARE #t TABLE (low int, high int, comment varchar(20))
INSERT INTO #t VALUES
(0,49,'Bad'),
(50,69,'Good'),
(70,100, 'Excellent')
SELECT * FROM #t
dsRandomNumbers This just creates 30 random numbers between 0 and 100
SELECT *
FROM (SELECT top 30 ABS(CHECKSUM(NEWID()) % 100) as myNumber FROM sys.objects) x
ORDER BY myNumber
I added a table to the report to show the grades (just for reference).
I then added a table to show the dsRandomNumbers
Finally I set the expression of the 2nd column to the following expression.
=SWITCH
(
Fields!myNumber.Value < LOOKUP("Bad", Fields!comment.Value, Fields!high.Value, "dsGradeRange"), "Bad",
Fields!myNumber.Value < LOOKUP("Good", Fields!comment.Value, Fields!high.Value, "dsGradeRange"), "Good",
True, "Excellent"
)
This gives the following results
As you can see we only need to compare to the high value of each case, the first match will return the correct comment.
Right click on your dataset and add a calculated field. Go to Field Properties > Fields > Add and add the following expression, which descripes your scenario:
=IIF(Fields!Number.Value < 50, "Bad", "Good")

Complex SSRS expression (Average of Average)

I have 4 datasets and I need to calculate the average of a field and their cumulative average.
Here are my 4 datasets : Dataset1,Dataset2,Dataset3,Dataset4:
This what I want . I want to find the average of the average values as given below :
Avg(Fields!Discount.Value,"Dataset1")
Avg(Fields!Discount.Value,"Dataset2")
Avg(Fields!Discount.Value,"Dataset3")
Avg(Fields!Discount.Value,"Dataset4")
A logic of = Avg(Avg,Avg,Avg..) throws an error. So basically it doesn't work. There's gotta be a way surely ?
We need to also take into account that sometimes one of the datasets may be empty ( null or 0 ). Is there any way of doing it in SSRS ?
If you don't need a weighted average you can use ISNOTHING to check for the NULLs like:
=(IIF(ISNOTHING(Avg(Fields!Discount.Value,"Dataset1")), 0, Avg(Fields!Discount.Value,"Dataset1") ) +
IIF(ISNOTHING(Avg(Fields!Discount.Value,"Dataset2")), 0, Avg(Fields!Discount.Value,"Dataset2") ) +
IIF(ISNOTHING(Avg(Fields!Discount.Value,"Dataset3")), 0, Avg(Fields!Discount.Value,"Dataset3") ) +
IIF(ISNOTHING(Avg(Fields!Discount.Value,"Dataset4")), 0, Avg(Fields!Discount.Value,"Dataset4") ) ) / 4

MDX Help: - Comparing Values in Two Max Time Periods within a larger Set of Time Periods to Populate an Indicator

I'm brand new to MDX and need some help. In SSRS I have a dataset that pulls from an SSAS cube. The dataset always contains six months of data. What I need to be able to do is to compare a value for the max(timeID) with a value for the second max(timeID) and if the value for the max(timeID) > value for the second max(timeID) than the arrow goes up in the indicator, etc...
So for the dataset below I would subtract 20130201's Value which is 8 from
20130301's Value which is 10. The result would be a positive number and the indicator would be an upward pointing green arrow. If it was 0 it would be straight and if negative the arrow would be red and point down. I understand how to deal with the indicator - that's not an issue. It's the MDX I need help with.
20130201 8
20130301 10
20121201 4
I can write it in SQL and it would look like this.
Select Item, case when sum(Time1ContentCount) > sum(Time2ContentCount) then 3 when sum(Time1ContentCount) = sum(Time2ContentCount) then 2 when sum(Time1ContentCount) sum(Time2ContentCount) then 1 end as Indicator, sum(Time1ContentCount) as Time1Count, sum(Time2ContentCount) as Time2Count from (Select timeID, dc.Item, Case when timeID = (Select max(timeID) from FactUsage) then count(fu.Contentid) else 0 END as Time1ContentCount, Case when timeID = (Select max(timeID) from FactUsage where timeID <>(Select max(timeID) from FactUsage)) then count(fu.Contentid) else 0 END as Time2ContentCount from factUsage fu INNER JOIN dimContent dC on dc.ContentID = fu.ContentID WHERE TimeID in (Select distinct top 6 timeid from factUsage order by timeID desc) Group by timeID, Item) a group by Item
Thanks so much for your help!
Edit:
I changed the statement to read as follows for the indicator.
WITH Member MEASURES.Indicator AS (
IIF(( [Measures].[Activity], [Time].[Time ID].LastChild ) >
( [Measures].[Activity], [Time].[Time ID].LastChild.PrevMember),3,
(IIF(([Measures].[Activity], [Time].[Time ID].LastChild ) =
([Measures].[Activity], [Time].[Time ID].LastChild.PrevMember), 2,1))))
SELECT {Measures.Indicator} on 0
FROM [DW]
It works when I run it as a query against the cube in SSMS but I tried to put it in the indicator and that doesn't work. Just adding the IIF statement doesn't work either. When I tried to add it into the query or the cube itself so I could just pull from there it errors out with an out of memory error.
I don't know how much you can edit in the MDX expression - or in your report builder, but to get the difference between two values in a series, you can create a measure (in your report) that is the difference between the CurrentMember and PrevMember. Since the time series (timeid) is sorted by the key, it will always be in the right order (or your schema and architecture needs a rework)
So basically, you can do :
WITH
MEMBER MEASURES.GrowthTime AS (
( [Measures].[Value], [TimeID].CurrentMember ) -
( [Measures].[Value], [TimeID].PrevMember )
)
MEMBER MEASURES.GrowthRatio AS (
( [Measures].[Value], [TimeID].CurrentMember ) /
( [Measures].[Value], [TimeID].PrevMember )
)
SELECT { Measures.Value, Measures.GrowthTime, Measures.GrowthRatio } on 0,
[TimeID].CHILDREN on 1
FROM Cube
This is pseudo as i don't know your cube structure. For TimeID you would want it like [DimensionName].[AttributeName].CurrentMember and PrevMember

Correction Needed for Error in Access Query

I want to create a table [Top Stores] that has 10 most active stores in terms of quantity of product sold. Each store sells 3 products. I just need to add the quantity sold for each product to arrive at StoreSale. The problem is when there is no sale for a product, the quantity value is left blank and not 0. I try to change the blank to 0 by the isnull tests. Please point out the error in my code below:
SELECT top 10 StoreName, StoreSale = (iif (isnull(Product1), 0, Product1) + iif (isnull(Product2), 0, Product2) + iif (isnull(Product3), 0, Product3)) INTO [Top Stores]
FROM SaleTable ORDER BY StoreSale DESC;
Two things:
if you use =, you will be doing an equality test, you can't have an assignment like that in SQL.
You will need to name your calculated result AS StoreSale instead.
Use Nz() to get a 0.00 when the field value is NULL
Result:
SELECT TOP 10 StoreName,
Nz(product1) + Nz(Product2) + Nz(Product3) AS StoreSale
INTO [Top Stores]
FROM SaleTable
ORDER BY StoreSale DESC;