How can I return 2 decimals if they end in 0 - jinja2

How can I return two decimals if the number ends in 0? What I am using now returns two decimals only if the last number is >0 (i.e., 60.58). Two decimals aren't returned if decimal ends in 0. (I.e., 50.80 returns as 50.8)
Here is what I am using ${{ |float|round(2) }}

I understand your query relates to showing values in jinja template. This expression in your jinja template show the zero at the end:
{{ "%.2f"|format(your_variable) }}
So, if the value of your_variable is 123.4000, the template will show as 123.40.

Related

SSRS expression seems to be rounding the decimal amount when it sets the value into the variable

My expression is taking a dollar amount from the database and storing it into a variable or taking the variable and adding a dollar amount to it before updating the variable based on some other fields criteria, and then displaying the variable in the textbox.
It seems that the variable is being rounded to the whole number but still displaying two decimal points. How to keep variable from being rounded?
=IF(Fields!APBAC_AMOUNT.Value > 0 And Fields!APBAC_TYPE.Value = "AP" Or Fields!APBAC_TYPE.Value = "BD", Variables!rptBalance.setValue(Variables!rptBalance.Value + Fields!AMOUNTRECEIVED.Value) And Variables!rptBalance.Value, Variables!rptBalance.setValue(Fields!APBAC_BALANCE.Value) AND Fields!APBAC_BALANCE.Value)

How to get group total inside a row in a rdl report?

How do I get the total of the group inside a row item?
I have tried Fields!Sales.Value/SUM(Fields!Sales.Value) but it returns 1 instead of the row_amount/group_total the report returns row_amount/row_amount
PS: Data comes from a SSAS cube. I made picture bellow1 to summarize what I wanna do
You could potentially just compare the values by the ReportItems. That is, you can specify a name for the sales textbox and for the totalsales textbox and do the comparison using those values. The following expression would likely work as intended.
= (ReportItems!SalesTextbox.Value / ReportItems!SalesTotalTextbox.Value) * 100.0
The other thing to consider with this is that if your result is 1, you may be dealing with integer division from your data. Make sure the data being returned is coming in with a double or decimal datatype, not an integer. It may work as you have it currently if you get the datatype corrected.
You also need to handle "divide by Zero' errors. Using an "=IIf (denominator = 0, 0, numerator/denominator) doesn't work since an "IIf" function evaluates both the "then" and the "else" and will still return an error. We use a bit of custom code that I found on SO (I wish I could remember where it came from so I could give credit where it's due).
Public Function HandleDivideByZero(ByVal numerator As Decimal, denominator As Decimal) As Decimal
If denominator = 0 Then
Return 0
Else
Return numerator / denominator
End If
End Function

SSRS #Error in combination with IIF and Cdbl

hope someone more experienced can give me answer/solution to my problem.
In a database I have two columns, one to control do we show data and the data to be shown is in the second column.
I have data and expression as below:
ShowPriceOnMatrix | RSP
-------------------------------
0 | 1.48
1 | 10.26 euro -> This one fails with #Error
1 | 4.59
0 | 7.12
=IIF(Fields!ShowPriceOnMatrix.Value = 1, CDbl(Fields!RSP.Value), "")
This expression sometimes gives me #Error.
The problem is that the RSP field was defined as nvarchar and I cant change it now so I'm trying to convert to number and show as. Some users when input price on the filed they also type a text ex. "12.45 euro" or similar. When the expression hits a value with text the whole IIF fails with an #Error even if the first column is stated not to show the price.
The problem with IIF is that it is not skipping the second parameter if the first one evaluates to false - and therefore produces #Error in my case.
I've tried to use
IsNumeric(Cdbl(Fields!RSP.Value))
and
IsError(Cdbl(Fields!RSP.Value))
but both of them give an #Error also. From SSRS explanation for those functions, they should return a Boolean value but somehow I'm getting #Error :)
Just convert the field to numeric using
VAL(Fields!RSP.Value)
Also, you might want to consider using SWITCH instead of IIF. SWITCH stops at the first expression which evaluates to True, whereas IIF will evaluate all expressions regardless of if they will be used or not.
In your case, with a single IIF, SWITCH is not relevant but in cases where nested IIFs are used or situations where you may get a divide by zero error, SWITCH is often easier to read/debug. So something like this where we have a hypothetical situation that we want to test for divide by zero and return zero or if the thing we are dividing by is negative we want to return -1, otherwise do the calc...
=IIF(Fields!MyDivisor.Value = 0, 0, IIF(Fields!MyDivisor.Value <0, -1, Fields!MyNumber.Value/ Fields!MyDivisor.Value))
would become
=SWITCH (
Fields!MyDivisor.Value =0, 0,
Fields!MyDivisor.Value <0, -1,
True, Fields!MyNumber.Value/ Fields!MyDivisor.Value
)
each expression/result pair is evaluated in order until it hits the first True expression, so if MyDivisor was zero it would return 0 and stop, if it was negative, it would return -1 and stop. The final True acts like an else (as True always returns True ! ) so it would only do the calculation if all other expressions returned false.
You can use this expression to extract your numeric values:
=System.Text.RegularExpressions.Regex.Replace(Fields!RSP.Value, "[^0-9,]", "")
But you have to take care of the . in 1.48. With , and the language DE it worked for me. With . it would be:
=System.Text.RegularExpressions.Regex.Replace(Fields!RSP.Value, "[^0-9.]", "")

SSRS Custom Code Loop

Can any one of you please explain me how the loop (For, While) loops works in here
A B
Apple 13
Grape 5
Orange 16
I have written this code, but it's not working here.
Public Function SafeConvert(ByVal num As String) As String
Dim S as Integer
For i as Integer = 0 to 2
s += num
next
Return s
End Function
Instead of adding each number, SSRS is adding only the last cell 16 * 3 times and showing incorrect result.
And can you please answer this question as well.
I have a matrix whose text box contains 3 values, how do I calculate the sum of all the values for Q27_A_1 if the value is less than 5?
You do not need to use any custom code to achieve this. If you are confident that all your string values are actually numbers you can use one of the many conversion functions along with a sum in your tablix expression to add everything together.
For whole numbers:
=sum(cint(Fields!B.Value))
For decimal values:
=sum(cdec(Fields!B.Value))
And for double values:
=sum(cdbl(Fields!B.Value))
To use these expressions, you will need to add a group to your tablix based on your dataset and put these expressions in your Group Total textbox.
To sum up all the values that are less than 5, again you need to have a group on your tablix and you can use the sum function. In this instance however, you will need to substitute any values of 5 or over with 0, so they do not add to the total of the sum:
=sum(iif(Fields!Q27_A_1.Value < 5, Fields!Q27_A_1.Value, 0))
Yeah, I understand your point of Sum function to perform this operation. it would be very easy to achieve this by using Sum function. But i have a giant report with more than 5000 matrix boxes and i need to apply on it. So just wants to confirm if the custom codes works in here and whats wrong in this code of lines.
Public Function SafeConvert(ByVal num As String) As String
Dim S as Integer
For i as Integer = 0 to 2
s += num
next
Return s
End Function
What it is doing is just adding the last cell for 3 times. i want to add all the value of cell during runtime.

SSRS 2008 Conditional data driven sum

The matrix's odd rows contain strings, representing integer numbers. The even rows contain strings, representing dates in mm/dd/yyyy format. The order is not guaranteed, it could be the other way around if someone changed the category names.
The grand total row has to be added to this matrix, but the following expressions throw an #Error:
=Sum(Iif(IsDate(Fields!Data.Value), 0, CInt(Fields!Data.Value)))
=Sum(Iif(InStr(Fields!Data.Value, "/"), 0, CInt(Fields!Data.Value)))
Converting 0 and field value to some other numeric data types did not work too.
Interesting enough, the expressions partially work for the grand total column, i.e. they calculate the numbers' row total, but #Error on the dates rows.
Protecting the row grand total as follows did not help either:
=Iif(Fields!Results.Value = "# of items", CStr(Sum(CInt(Fields!Data.Value))), "")
What is the correct way to implement data driven conditional totals?
I could do this in plain SQL and dump into a tablix in a heartbeat but this has to be wrapped in SSRS and used with an existing matrix which must not be changed otherwise.
The #ERROR you get on the date rows is due to the CInt conversion failing.
This is because IIF is a function, not a language construct so both the true and false parameters get evaluated before being passed to the function regardless of the value of the boolean condition parameter. This means that:
=Sum(Iif(IsDate(Fields!Data.Value), 0, CInt(Fields!Data.Value)))
will always attempt the conversion to integer regardless of the result of the IsDate function.
Try using Val instead of CInt. The problem with CInt is it errors when the string to be converted is an inappropriate form; Val doesn't have that problem - it simply grabs whatever numbers it can. So you can use the expression:
=Sum(Iif(IsDate(Fields!Data.Value), 0, Val(Fields!Data.Value)))
and then simply format it like an integer.
Note that the Val function is still being run even when the field is not numeric but this expression succeeds because the Val function doesn't raise errors like Cint. We simply make the calculation and discard the result when the field is a date.
This expression, combining the tips from both answers with additional protection, works:
=Iif(
IsNumeric(Fields!Data.Value),
Sum(Val(Iif(InStr(Fields!Data.Value, "/"), "", Fields!Data.Value))),
0
)