Using a Crosstab dimensional report I having issues filtering out a specific value that I am getting from my calculated measure:
I am using the following calculated measure in Reporting Studio to get the required output..
ROUND(([A1_SCHEDQTY]+[B1_SCHEDQTY]) / ([A1_QTY]+[B1_QTY]) * 100, 1)
However, in a few cases I am getting an output of '/0' I am unable to filter this out. I have tried using an IF statement, but I believe my syntax is incorrect.
IF (ROUND(([A1_SCHEDQTY]+[B1_SCHEDQTY]) / ([A1_QTY]+[B1_QTY]) * 100, 1) = '/0') THEN NULL ELSE ROUND(([A1_SCHEDQTY]+[B1_SCHEDQTY]) / ([A1_QTY]+[B1_QTY]) * 100, 1)
Any advice?
You need to check the divisor of the equation for 0 instead of checking the entire equation for '/0'. If you check the entire equation, the divide by 0 still takes place.
Try this:
IF ([A1_QTY]+[B1_QTY] = 0) THEN (NULL) ELSE (ROUND(([A1_SCHEDQTY]+[B1_SCHEDQTY]) / ([A1_QTY]+[B1_QTY]) * 100, 1))
Related
I'm trying to calculate an unweighted average given a data for averages and counts, and I can't figure out why my Total calculation is throwing #Error.
Preliminary aggregation for this report (aggregation by color) is done within the dataset's query, but drilldown functionality has been requested for this report, and as far as I know that requires totaling to be done on the Report Builder side. That requires calculating the total aggregate from a list of subtotal aggregates.
=Sum(Fields!weightedAveragePercent.Value * Fields!weight.Value) / Sum(Fields!weight.Value) gets me what I need for the total row, which is the aggregate weighted average percent for the category. In light of that, I'd expect that =Sum(Fields!averagePercent.Value * Fields!count.Value) / Sum(Fields!count.Value) would behave similarly. However, when the averagePercent field is blank (representing a NULL value passed over from the SQL query underlying the report) the calculation for the total averagePercent returns #Error.
Color
count
weight
weightedAveragePercent
averagePercent
Red
14
$10.10
3.702%
2.500%
Blue
25
$5.38
5.016%
7.181%
Brown
0
$0.00
Total
39
$15.48
4.544%
#Error
Things I've checked:
=Sum(Fields!averagePercent.Value) and =Sum(Fields!count.Value) both return the expected values. =Sum(Fields!averagePercent.Value * Fields!count.Value) returns #Error.
=Sum(Iif(IsNumeric(Fields!averagePercent.Value), 1, 0)) returns the expected value (2, in this case) but =Sum(Iif(IsNumeric(Fields!averagePercent.Value), Fields!averagePercent.Value, 0)) returns #Error. This is not resolved by changing 0 to 0.000, or by wrapping IsNumeric() in Not().
Pushing =Fields!averagePercent.Value * Fields!count.Value into a calculated column still throws #Error.
Data Types:
count: int
weight: float
weightedAveragePercent: float
averagePercent: float
I am looking for some help totaling up a column. This column is using expressions from other columns to get its total.
This is the expression I am using:
=IIF(avg(Fields!UOS_2017.Value) <= 0, sum(Fields!PROD_HRS.Value) * - 1,
((avg(Fields!BUD_PROD_HRS___UOS.Value) * avg(Fields!UOS_2017.Value)) -
sum(Fields!PROD_HRS.Value)))
I want to sum this column.
I do want to note that this expression does work but only if the UOS_2017 column has a zero value. I do not know what to do.
Here is what my design view looks like.
I am also going to attached the results.
These are the results
Thank you in advance for your help.
I hate to add this as an answer, but could you try one of these and see what happens? I am not familiar with avg function, but if you're pivoting data you may have to add sum/avg outside of if statement as well:
=SUM(IIF(avg(Fields!UOS_2017.Value) <= 0, sum(Fields!PROD_HRS.Value) * - 1, ((avg(Fields!BUD_PROD_HRS___UOS.Value) * avg(Fields!UOS_2017.Value)) - sum(Fields!PROD_HRS.Value))))
=SUM(IIF(avg(Fields!UOS_2017.Value) <= 0, (Fields!PROD_HRS.Value) * - 1, ((avg(Fields!BUD_PROD_HRS___UOS.Value) * avg(Fields!UOS_2017.Value)) - (Fields!PROD_HRS.Value))))
I am a heavy user of R who is trying to port some basic calculations to Power BI but unable to get DAX to perform vector/rowwise division.
For example, with this dataset:
Category Value
-------- -----
A 1
A 2
B 3
C 4
I have created a measure: Value Sum = SUM(Table[Value])
and I would like to create a third column: Value Share = Value / Value Sum to get:
Category Value Value Share
-------- ----- -----------
A 1 0.1
A 2 0.2
B 3 0.3
C 4 0.4
The equivalent in R would be :
table$Value.Share = table$value/Value.Sum
I have tried: Value Share = [Value] / [Value Sum] but I ended up with 1s in all Value Share rows. Tried SUMX and CALCULATETABLE functions but I believe I am missing something fundamental. Can anyone help?
Sure! If you're starting out with DAX, I can highly recommend to view one of the video lectures of Alberto Cairo / Marco Russo like https://www.youtube.com/watch?v=klQAZLr5vxA, because your question (and all of DAX difficulty) is primarily about contexts.
As for your question, I think you're looking for
=[Value]/CALCULATE(SUM([Value]); ALL(TableName))
Of course, you can replace SUM([Value]) with a measure name too.
See also http://www.daxpatterns.com/cumulative-total/
try this formula: divide([value],sum([value]))
SNAP of DAX formula
I have a few things I am struggling with so hopefully I can ask all at once ?
I am using VS 2010 and I think with Vb.net to build reports, I use databases from Sql - I am mainly using matrix tables
I have a report that is multiple tables in one but not sure how to set/define to still show the tables that has no data ? So currently if there is a blank one it messes up the full report look ?
In another scenario how can I use an expression/custom code to filter out items in one row - in a calculation for example if I only want to sum 3 items of 5 etc
How can I work out % of a row or coloumn based on criteria or filters so if total items is 30 and item 1 is 5 the % of will be 17% and all items will total to 100%
How can I work out growth of the row/column so if year 1 is 50 and year 2 is 60 the growth/variance will be 20%
There are some issues with the expressions:
=IIF(Fields!Total_Amount__Excl_VAT_.Value = 0
OR Fields!Total_Amount__Excl_VAT_.Value = "", 0, Sum(Fields!Total_Amount__Excl_VAT_.Value))
The SUM should be around the IIF:
=SUM(IIF(Fields!Total_Amount__Excl_VAT_.Value = 0
OR Fields!Total_Amount__Excl_VAT_.Value = "", 0, Fields!Total_Amount__Excl_VAT_.Value))
The same issue for
=IIF(Fields!Total_Amount__Excl_VAT_.Value = 0
OR Fields!Total_Amount__Excl_VAT_.Value = "",0,Sum(Fields!Total_Amount__Excl_VAT_.Value))
Should Be:
=SUM(IIF(Fields!Total_Amount__Excl_VAT_.Value = 0
OR Fields!Total_Amount__Excl_VAT_.Value = "", 0, Fields!Total_Amount__Excl_VAT_.Value))
The growth formula looks correct - are you getting a different result than expected?
Here's my problem: a user searches for products by size. The result should show all products of the desired size (if any) plus products progressively larger and smaller until there are at least 50 undersized and 50 oversized products displayed in addition to the correctly-sized products.
The result should always show all products of a certain size; in other words, if moving to the next size up or down will result in more than 50 products, show them all - don't stop at 50.
Example: Imagine there are 25 distinct sizes with 20 products of each size. The user asks for size 12. We need to go three sizes down and three sizes up to get at least 50 in each direction. The query should return all size-12 products, plus the size 9, 10, 11, 13, 14, and 15 products. The query would return 140 products total (the 20 size-12 plus 60 above and 60 below.)
Unfortunately the sizes are not nice integers like my example. They are arbitrary decimal values.
A Linq to SQL query to do this would be really cool, but plain SQL or C# is welcome, too.
(My environment is C#, SQL Server 2005)
Thanks
Here's a sample SQL statement (for mysql) that should do what you want. But depending on what else your procedure is doing, you may find it faster to do some of the processing in the C# code:
SELECT
*
FROM
products
WHERE
size = [[desired_size]] OR
size IN (
SELECT DISTINCT
size
FROM
products
WHERE
size > [[desired_size]]
ORDER BY
size
LIMIT 50
)
OR
size IN (
SELECT DISTINCT
size
FROM
products
WHERE
size < [[desired_size]]
ORDER BY
size DESC
LIMIT 50
)
I'll explain by starting at the beginning (and use your example for the values)...
Firstly we need to generate a list of the next 50 larger (or smaller) items. The following query should do this:
SELECT * FROM products WHERE size > 12 ORDER BY size LIMIT 50
So, right now, we're grabbing everything from the products table that's larger than the desired size. We order it by size, then limit it to only the first 50.
So, in this case, it should return (in this order) 20 products of size 13, 20 products of size 14 and 10 products of size 15.
You can try this in the Visual Studio SQL editor, and see which rows it returns.
But for our purposes, we only want a list of sizes, so we can limit the query further by changing the SELECT clause to:
SELECT DISTINCT size...
so, now we're only looking at the "size" column, and we use the DISTINCT keyword to avoid duplicate values
so now, the query returns just the list: (13, 14, 15)
We make a similar query to get the next 50 smaller items:
SELECT DISTINCT size FROM products WHERE size < 12 ORDER BY size DESC LIMIT 50
this is just the same as the above query, but we limit to only sizes that are smaller, and we reverse the ordering, so we get the 50 biggest items that are smaller than the desired size.
in this case this query will return the list (11, 10, 9)
If we put it all together in the outer query using these two lists, we get:
SELECT
*
FROM
products
WHERE
size = 12 OR
size IN (13, 14, 15) OR
size IN (11, 10, 9)
So we pull all the products that have a size of 9 to 15
I hope this makes sense :-)
A little late so you may have already solved this...
It seems the problem you're hitting is that you can't easily define the boundary conditions for your result set.
You can obviously easily do:
Get me everything of size X
Get me 50 of size >X
Get me 50 of size <X
However, since size is linear not discrete and we can't predict how many of each size exist, we can't group/count sizes which makes it difficult to determine which size will be at the outside of the +/-50 records - hence we need to read those values before we can get a final recordset. It may be possible to roll this into one with sub-queries but it wouldn't surprise me if LINQ executed in multiple requests,,,,
Something like:
Dim ExactSize = 1.1
Dim MaxSizeRecord = MyRepository.Get(function(x) x.Size > ExactSize).OrderBy(function(y) y.Size).Skip(50).First
Dim MinSizeRecord = MyRepository.Get(function(x) x.Size < ExactSize).OrderByDescending(function(y) y.Size).Skip(50).First
Dim FinalResults = MyRepository.Get(function(x) x.Size >= MinSizeRecord.Size and X.Size <= MaxSizeRecord.Size)
I haven't played around with the guts of LINQ enough to know if ...
Dim MaxSize = MyRepository.Get(function(x) x.Size > ExactSize).OrderBy(function(y) y.Size).Skip(50).First.Size
Dim MinSize = MyRepository.Get(function(x) x.Size < ExactSize).OrderByDescending(function(y) y.Size).Skip(50).First.Size
Dim FinalResults = MyRepository.Get(function(x) x.Size >= MinSize and X.Size <= MaxSize)
(ie getting the exact size of the boundaries rather than a record whose size happens to be at the boundary)
...would generate the same SQL - It's certainly more readable but LINQ may potentially execute the Min/Max queries immediately as the value is being stored in a Double - It's possible that by defining the type of MinSize/MaxSize implicitly it would treat it as an IQueryable(of double).
Using records instead of doubles whilst being slightly less readable should guarantee it's treated as a query not a value.