Trying to calculate the Average of the non zero rows in a column in an RDLC - reportviewer

Is there any way to caculate the average of the non zero rows only for a column of data in a table in an RDLC for Microsoft report viewer?
ie
0
0
0
5
5
= 5 not 2
I tried Count( fields.n.value > 0 ) to get the count of non zero rows, but it returned the
count of all rows.
Thanks!
Eric-

Try this:
=Sum(Fields!n.Value > 0) / Sum(IIf(Fields!n.Value > 0, 1, 0))
Notice how the average is computed manually by summing all values then dividing by another sum that mimics a specialized count mechanism.

Related

Grouping rows so a column sums to no more than 10 per group

I have a table that looks like:
col1
------
2
2
3
4
5
6
7
with values sorted in ascending order.
I want to assign each row to groups with labels 0,1,...,n so that each group has a total of no more than 10. So in the above example it would look like this:
col1 |label
------------
2 0
2 0
3 0
4 1
5 1
6 2
7 3
I tried using this:
floor(sum(col1) OVER (partition by ORDER BY col1 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) /10))
But this doesn't work correctly because it is performing the operations
as:
floor(2/10) = 0
floor([2+2]/10) = 0
floor([2+2+3]/10) = 0
floor([2+2+3+4]/10) = 1
floor([2+2+3+4+5]/10 = 1
floor([2+2+3+4+5+6]/10 = 2
floor([2+2+3+4+5+6+7]/10) = 2
It's all coincidentally correct until the last calculation, because even though
[2+2+3+4+5+6+7] / 10 = 2.9
and
floor(2.9) = 2
what it should do is realise 6+7 is > 10 so the 5th row with value 7 needs be in its own group so iterate the group number + 1 and allocate this row into a new group.
What I really want it to do is when it encounters a sum > 10 then set group number = group number + 1, allocate the CURRENT ROW into this new group, and then finally set the new start row to be the CURRENT ROW.
This is too long for a comment.
Solving this problem requires scanning the table, row-by-row. In SQL, this would be through a recursive CTE (or hierarchical query). Hive supports neither of these.
The issue is that each time a group is defined, the difference between 10 and the sum is "forgotten". That is, when you are further down in the list, what happens earlier on is not a simple accumulation of the available data. You need to know how it was split into groups.
A related problem is solvable. The related problem would assign all rows to groups of size 10, splitting rows between two groups. Then you would know what group a later row is in based only on the cumulative sum of the previous rows.

Python SettingWithCopyWarning, but I'm trying to set the value using .ix

I have a pandas dataframe in python and I'm trying to modify a specific value in a particular row. I found a solution to this problem Set value for particular cell in pandas DataFrame using index, but it is still generating the SettingWithCopy error.
The name of the data frame is internal_df and it has columns 'price', 'visits', and 'orders'. Specifically, I want to add the number of orders and visits to a lower price point if we don't have a sufficient number of visits (100 in this example). Note that below the variable 'price' is a float, and the data types for 'price' within the internal_df data frame is float, while price and orders are ints.
if int(internal_df[internal_df['price']==price]['visits']) < 100:
for index, row in internal_df.iterrows():
if float(row['price']) > price:
internal_df.ix[internal_df['price'] == price, 'visits'] = internal_df.ix[internal_df['price'] == price, 'visits'] + row['visits']
internal_df.ix[internal_df['price'] == price, 'orders'] = internal_df.ix[internal_df['price'] == price, 'orders'] + row['orders']
Here is a sample of the data
price visits sales
0 1399.99 2 0
1 169.99 2 0
2 99.99 1 0
3 99.99 1 0
4 139.99 1 0
5 319.99 1 0
6 198.99 1 0
7 119.99 1 0
8 39.99 1 0
9 259.98 1 0
Does anyone have any suggestions, or should I just ignore the error?
Brad
Note that .ix is deprecated because it indexes by position or by label, depending on the data type of the index. Use .loc or .iloc instead.
This SettingWithCopyWarning might originate from a "get" operation several lines of code above what you've provided. A quick fix might be to find where internal_df is first assigned, and to add .copy() to the end of the assignment statement. For example, if you have internal_df = df[df['colname'] <= value], change that to internal_df = df[df['colname'] <= value].copy() and hopefully that resolves the error.
Also, I think you can do what you're trying to do without a for loop, which would be faster and more readable!

DAX - Reference measure in calculated column?

I have data like this
EmployeeID Value
1 7
2 6
3 5
4 3
I would like to create a DAX calculated column (or do I need a measure?) that gives me for each row, Value - AVG() of selected rows.
So if the AVG() of the above 4 rows is 5.25, I would get results like this
EmployeeID Value Diff
1 7 1.75
2 6 0.75
3 5 -0.25
4 3 -1.75
Still learning DAX, I cannot figure out how to implement this?
Thanks
I figured this out with the help of some folks on MSDN forums.
This will only work as a measure because measures are selection aware while calculated columns are not.
The Average stored in a variable is critical. ALLSELECTED() gives you the current selection in a pivot table.
AVERAGEX does the row value - avg of selection.
Diff:=
Var ptAVG = CALCULATE(AVERAGE[Value],ALLSELECTED())
RETURN AVERAGEX(Employee, Value - ptAVG)
You can certainly do this with a calculated column. It's simply
Diff = TableName[Value] - AVERAGE(TableName[Value])
Note that this averages over all employees. If you want to average over only specific groups, then more work needs to be done.

Issue in Grand Totals (Excel Pivot table)

I am working on an Excel pivot table that looks like the following:
Prj 30Days 60Days GreaterThan60
128139 0 0 118484.02
123123 0 0 10115.01
234232 0 0 4609.81
121313 0 0 314.33
343432 0 0 4000
232323 0 0 164.27
121212 164994.98 0 0
232323 0 0 1046.58
Grand Total 1075731.89 535507.27 199200.01
Here is my expected/desired result:
Prj 30Days 60Days GreaterThan60 GrandTotal
128139 0 0 118484.02 118484.02
123123 0 0 10115.01 10115.01
234232 0 0 4609.81 4609.81
121313 0 0 314.33 314.33
343432 0 0 4000 4000
232323 0 0 164.27 164.27
121212 164994.98 0 0 164994.98
232323 0 0 1046.58 1046.58
Grand Total 1075731.89 535507.27 199200.01 1810439.17
The Grand Totals at the bottom of the pivot table are Grand Totals for columns. I also need the Grand Totals for Rows as the right most Column. I am not able to do this, though I checked the option to SET Grand Totals for both rows and columns.
I researched this issue online and it says that we need to have at-least one field in Column Labels to get the Row Totals, I don't have a field that I want to put in the Column Labels.
Can I create a calculate measure to achieve this? I looked into creating a calculated field in Excel under PivotTable/Options/FieldItems and Sets/New Calculated Field. But the New Calculated Field option is disabled.
I have added some more 'data' to match up with your totals. A PT with Grand Total for rows seems feasible:
Since you have tried what I used for the GT for rows without success I'm guessing your data input (not shown) must not be in the same format as in my example. If for example you have dates rather than the (credit period?) bands then using these dates to calculate the appropriate bands in the source data may be all that you are missing.

SSRS 2008 divide by zero error and "NaN" value

hoping to get some help here. I have a report that shows 4 fields: current YTD sales, previous YTD sales, the difference between the 2 in dollars, and the difference between the 2 in percent. I'm running into a divide by 0 error and the value of "NaN" as the value for the percent field. I get the divide by 0 error when I have a value in the current YTD ("OrderInfoConstruction") but 0 in the previous YTD ("OrderInfoClosedConstruction"), since my expression for the % field is:
=(Sum(Fields!PRICE_EXT.Value, "OrderInfoConstruction") -
Sum(Fields!PRICE_EXT.Value, "OrderInfoClosedConstruction")) /
Sum(Fields!PRICE_EXT.Value, "OrderInfoClosedConstruction")
and the value of "Sum(Fields!PRICE_EXT.Value, "OrderInfoClosedConstruction") is 0 (the previous YTD value). For the NaN value issue, it's the same expression, but in this case, BOTH current and previous YTD's are 0. How can I have it NOT divide if the value is 0 to solve the divide by 0 error and what is a NaN and how can I have it just show "0" instead? I've found some help on this but have NO idea how to take the IIF statement below and adapt it for my statement above?
=IIf(Fields!SomeField.Value = 0, 0, Fields!SomeOtherField.Value / IIf(Fields!SomeField.Value = 0, 1, Fields!SomeField.Value))
thanks in advance for the help!!!
If you want to display 0 for both 0/0 and #/0, you just need to check the denominator value for zero. Basically IIf(PrevYTD = 0, 0, (CurrYTD - PrevYTD) / PrevYTD), or with your actual fields:
=IIf(Sum(Fields!PRICE_EXT.Value, "OrderInfoClosedConstruction") = 0, 0,
(Sum(Fields!PRICE_EXT.Value, "OrderInfoConstruction") -
Sum(Fields!PRICE_EXT.Value, "OrderInfoClosedConstruction")) /
Sum(Fields!PRICE_EXT.Value, "OrderInfoClosedConstruction"))
Also, NaN stands for not a number, and 0/0 is one operation that produces it.