SSRS expression for variance % - sql

I have two "Total" columns across two tablix as below:
one is SUM(Previous) and the other is SUM(Current)
i'm trying to calculate Variance between these columns as "Expressions" in a new Tablix, I'm trying to get the PreviousTotal and CurrentTotal through SQL and then to calculate Variance as an expression.
how to calcualte variance in ssrs expression between these two columns ?

You don't mean the variance, I think you mean something like "percentage deviation" from a previous to the current value. Why would you want to do this with a SUM? The difference between a "current sum" and a "previous sum" usually is just a "current value", and if you set this in relation to a 'SUM', it might look very small. But ok, in some scenarios this might make sense.
So, If you really want to calculate the "percentage deviation" of SUM(Previous) and SUM(Current), the formula would be:
(SUM(Current) - SUM(Previous)) / SUM(Previous)
Format the value as a percentage.

Reference the PreviousTotal and CurrentTotal by Textbox ID.Value, and do math on them.

Related

Advice needed on weighted averaging in power query or power pivot

I need to create a weighted average that multiplies a column of volume manufactured for multiple manufacturing plants by a column containing the cost to manufacture at each plant, and returns one weighted average value for a specific product type for all plants.
I've tried adding this as a calculated column using:
=sumx('Plant','Plant'[Cost]*'Plant'[Tonnage])/sum('Plant'[Tonnage])
But this goes row by row, so it doesn't give me the full over riding average that I need for the company. I can aggregate the data, but really want to see the average lined up against individual plant for benchmarking
Any ideas how I can do this?
You can do this in multiple ways. You can either make a single more complex calculation, or you can make a few calculated columns to make the final calculation more transparent. I will pick the latter approach here, because it is more easy to show what is going on. I'm going to use the following DAX functions: CALCULATE, SUM, and ALLEXCEPT.
First, create three new calculated columns.
The first one should contain the [Volume] times [Cost] for each record:
VolumeTimesCost:=[Volume] * [Cost]
The second one should contain the sum of [VolumeTimesCost] for all plants within a given product type. It could look like this:
TotalProductTypeCost:=CALCULATE(SUM([VolumeTimesCost]),ALLEXCEPT([Product Type]))
Using the ALLEXCEPT([Product Type]) removes the filter from all other columns than the [Product Type] column.
The third calculated column should contain the SUM of [Volume] for all plants within a given product type. It could look like this:
TotalProductTypeVolume:=CALCULATE(SUM([Volume]),ALLEXCEPT([Product Type]))
You can then create your measure based on the two calculated columns [TotalProductTypeCost] and [TotalProductTypeVolume].
I hope that helps you solve the issue correctly. Otherwise feel free to let me know!

SSRS Divide Sums from Different Scopes Returns 0

I have a grouped dataset. From the parent group down, the group names are:
Company > Plant > Details. In the end, I want to be able to take the sales of each plant and see what percentage it is of the entire company's sales. Let's say the company has a total sales of $500 and the sales of "Plant A" were $100 and "Plant B" sales were $400. I would image that I would need an expression at the Plant grouping level that was like:
=Sum(Sales)/Sum(Sales, "Company")
And I would get .2 for Plant A and .8 for Plant B. But if I do that, I get 0. I am at a complete loss. Any help with this would be greatly appreciated.
Additional information:
My exact setup is a little more complex than the example I gave below, but I believe the general idea still holds the same. I have a total of 6 groups:
The circled group is the equivalent of the "Plant" Group in my example. Here is the row group in my tablix for the GroupBy group (the one with the arrow pointing to it).
The expression that is circled in the picture above is the expression in question to get my percentage (right now really just a decimal, not formatted to be a percentage yet).
=sum(Fields!ActualCurrent.Value)/sum(Fields!ActualCurrent.Value, "Company")
Fields!ActualCurrent.Value is the equivalent of "Sales" in my example above. The expression above returns 0 for all groups. But yet, if I change it to
=sum(Fields!ActualCurrent.Value)+sum(Fields!ActualCurrent.Value, "Company")
It will produce the equivalent of $600 for "Plant A" and $900 for "Plant B."
I can't seem to find how it reacts as expected when adding the two sums, but produces 0 when I divide them.
It would be useful to see where are you using that expression to determine what is wrong here, but I think you can use this guide to get your desired result.
Create a tablix like this:
Note I've added Company and Plant fields as groups. Also I've deleted details group. Right click details and select Delete group and set Delete group only option.
Now in the percentage column use the following expression:
=FORMAT(
SUM(Fields!Sales.Value,"Plant")/SUM(Fields!Sales.Value,"Company")
,"P2"
)
The sum of every plan divided by the sum of the whole company group. It is not necessary but I am using FORMAT function to format the float value returned by the expression to percentage format using two decimal places.
It should show something like this:
UPDATE: Try scoping the sum to your specific group: GroupBy
=sum(Fields!ActualCurrent.Value, "GroupBy")/sum(Fields!ActualCurrent.Value, "Company")
UPDATE 2: Format the cell to show decimal digits.
Use thiss expression:
=FORMAT(
sum(Fields!ActualCurrent.Value, "GroupBy")/sum(Fields!ActualCurrent.Value, "Company"),
"F2"
)
It will format the value returned by the expression as a float with two decimal digits.
If you want to show the value in percentage format replace F2 in the expression for P2 (Percentage format with two decimal digits.)
Let me know if this helps.

MDX Query SUM PROD to do Weighted Average

I'm building a cube in MS BIDS. I need to create a calculated measure that returns the weighted-average of the rank value weighted by the number of searches. I want this value to be calculated at any level, no matter what dimensions have been applied to break-down the data.
I am trying to do something like the following:
I have one measure called [Rank Search Product] which I want to apply at the lowest level possible and then sum all values of it
IIf([Measures].[Searches] IS NOT NULL, [Measures].[Rank] * [Measures].[Searches], NULL)
And then my weighted average measure uses this:
IIf([Measures].[Rank Search Product] IS NOT NULL AND SUM([Measures].[Searches]) <> 0,
SUM([Measures].[Rank Search Product]) / SUM([Measures].[Searches]),
NULL)
I'm totally new to writing MDX queries and so this is all very confusing to me. The calculation should be
([Rank][0]*[Searches][0] + [Rank][1]*[Searches][1] + [Rank][2]*[Searches][2] ...)
/ SUM([searches])
I've also tried to follow what is explained in this link http://sqlblog.com/blogs/mosha/archive/2005/02/13/performance-of-aggregating-data-from-lower-levels-in-mdx.aspx
Currently loading my data into a pivot table in Excel is return #VALUE! for all calculations of my custom measures.
Please halp!
First of all, you would need an intermediate measure, lets say Rank times Searches, in the cube. The most efficient way to implement this would be to calculate it when processing the measure group. You would extend your fact table by a column e. g. in a view or add a named calculation in the data source view. The SQL expression for this column would be something like Searches * Rank. In the cube definition, you would set the aggregation function of this measure to Sum and make it invisible. Then just define your weighted average as
[Measures].[Rank times Searches] / [Measures].[Searches]
or, to avoid irritating results for zero/null values of searches:
IIf([Measures].[Searches] <> 0, [Measures].[Rank times Searches] / [Measures].[Searches], NULL)
Since Analysis Services 2012 SP1, you can abbreviate the latter to
Divide([Measures].[Rank times Searches], [Measures].[Searches], NULL)
Then the MDX engine will apply everything automatically across all dimensions for you.
In the second expression, the <> 0 test includes a <> null test, as in numerical contexts, NULL is evaluated as zero by MDX - in contrast to SQL.
Finally, as I interpret the link you have in your question, you could leave your measure Rank times Searches on SQL/Data Source View level to be anything, maybe just 0 or null, and would then add the following to your calculation script:
({[Measures].[Rank times Searches]}, Leaves()) = [Measures].[Rank] * [Measures].[Searches];
From my point of view, this solution is not as clear as to directly calculate the value as described above. I would also think it could be slower, at least if you use aggregations for some partitions in your cube.

How to Calculate specific cell total in SSRS?

As shown below image i have problem to calculated specific fields total.
I want to just calculate only two cells value like
total=(total opening-qty) + (total purchase-qty) cell and opening, purchase, sales are all from Type Group.
Please tell me the Expression or tips.
It's hard to tell without seeing your DataSet/report, but the expression would be similar to:
=Sum(IIf(Fields!Type.Value = "OPENING" or Fields!Type.Value = "PURCHASE"
, Fields!qty.Value
, Nothing)
This will take a SUM of all qty values, but it will ignore any Type rows that don't match the types you're specifying, i.e. OPENING and PURCHASE.
The most import thing is to make sure the expression is in the correct Scope, i.e. if you're grouping by Type the expression should be applied outside of that group Scope to make sure the expression is considering all the required rows.

reporting services - sum expression column in tablix

I have a column in my tablix where I manually entered a long nested IF expression (its not in my dataset.) I want to sum the whole column that contains my expression. Is there anyway to do this?
Can I give the tablix column a name, and sum that? Or do I have to take my expressions and add them to my dataset? I rather not do this, because i have 8 datasets, and I would have to add 20 expressions to each one manually.
Assuming your column's expression is like:
=iif(...)
- simply add a similar expression into your summary cell, with sum( ) around it - like so:
=sum(iif(...))