Conditional formatting ssrs - ssrs-2012

I have a dashboard, where a field displays values of different types. Something like this
Row Average of 3 years
1 Percent
2 Count
3 Count
4 Count
..
7 Percent
9 String (e.g. ABC : 100, EDF: 2900)
10 Percent
I wrote an expression
=IIF(Fields!mos_id.Value = 1, FormatPercent(Fields!Average_prev_3_years.Value,2),
IIF(Fields!mos_id.Value = 6, FormatPercent(Fields!Average_prev_3_years.Value,2),
IIF(Fields!mos_id.Value = 7, FormatPercent(Fields!Average_prev_3_years.Value,2),
IIF(Fields!mos_id.Value = 8, FormatPercent(Fields!Average_prev_3_years.Value,2),
IIF(Fields!mos_id.Value = 10, FormatPercent(Fields!Average_prev_3_years.Value,2),
Fields!Average_prev_3_years.Value))))
The percentages and the counts show up fine but where I had strings, I get the #Error. What am I missing?

Related

Split a number into random number of parts where the sum of each part forms the number again

Let's say I have an integer 50. I'm trying to split this into a random number of parts so that the sum of each part form 50 again. E.g, if I split it in 4 parts: 20 + 10 + 15 + 5 = 50.
The number of parts (always less than the total amount) will have to be randomly generated and provided. How can I achieve this?
python code:
def split_num():
n = 50
parts = random.randint(1,n)
result = []
for i in range(parts-1):
x = random.randint(1,n-parts+i+1)
n = n - x
result.append(x)
result.append(n)
print(result)
print(sum(result))
import random
split_num()
result:
[4, 33, 4, 1, 8]
50

Generate rows based to make a sequence in a column of a dataframe

I'm trying to generate new rows based on values in a certain column. In current data as you can see 'days_left' column does not have all sequential values.
current = {'assignment': [1,1,1,1,2,2,2,2,2], 'days_left': [1, 2, 5, 9,1, 3, 4, 8, 13]}
dfcurrent = pd.DataFrame(data=current)
dfcurrent
While I want to generate rows into that dataframe to create make sequential list for for 'days_left' for each 'assignment'. Please see the desidered output below:
desired = {'assignment': [1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2],
'days_left': [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,10,11,12,13]}
dfdesired = pd.DataFrame(data=desired)
dfdesired
Note: The original data is much bigger and has other columns as well but I just simplified it for this question.
Could you please help me how I can solve this?
Thank you very much in advance!
You can iterate through the rows of the current dataframe and create a new dataframe. For each days_left range, copy the current row to the new dataframe and update the days_left column value.
Try this code:
import pandas as pd
current = {'assignment': [1,1,1,1,2,2,2,2,2], 'days_left': [1, 2, 5, 9, 1, 3, 4, 8, 13]}
dfc = pd.DataFrame(data=current)
dfd = pd.DataFrame() # new dataframe
for r in range(1,len(dfc)): # start at 2nd row
for i in range(dfc.iloc[r-1]['days_left'],dfc.iloc[r]['days_left']): # fill gap of missing numbers
dfd = dfd.append(dfc.iloc[r]) # copy row
dfd.reset_index(drop=True, inplace=True) # prevent index duplication
dfd.loc[len(dfd)-1, 'days_left'] = i # update column value
if r == len(dfc)-1 or dfc.iloc[r+1]['assignment']!=dfc.iloc[r]['assignment']: # last entry in assignment
dfd = dfd.append(dfc.iloc[r]) # copy row
dfd.reset_index(drop=True, inplace=True) # prevent index duplication
dfd = dfd.astype(int) # convert all data to integers
print(dfd.to_string(index=False))
Output
assignment days_left
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
2 1
2 2
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
2 11
2 12
2 13

SPSS Randomly select a number from a respondent's input

I'm hoping there is SPSS syntax that I can use to randomly select a number from among a couple of variables. For example: the data lists the ages of respondent's children in four variables - Age1 Age2 Age3 Age4
Resp 1: 3 6 8
Resp 2: 2 10
Resp 3: 4
I want to create a variable that stores a randomly selected age for each respondent - something like:
Resp 1: 6
Resp 2: 2
Resp 3: 4
The code I'm using at the moment:
COUNT kids=age1 to age4 (1 thru 16).
COMPUTE rand=RND(RV.UNIFORM(1, kids),1).
DO REPEAT
x1=age1 to age4
/x2=1 to 4.
IF (rand=x2) random_age=x1.
END REPEAT.
Here is my suggested code for the task.
First creating some sample data to demonstrate on:
data list list/id age1 to age4 (5f2).
begin data
1, 4, 5, 6, 7
2, 4, 5, 6,
3, 6, 7,,
4, 8,,,
5, 5, 6, 7,
6, 10,,,
end data.
Now to randomly select one of the ages:
compute numages=4-nmiss(age1 to age4).
compute SelectThis = rnd(uniform(numages)+.5).
do repeat ag=age1 to age4 /ind=1 to 4.
if SelectThis=ind SelectedRandAge=ag.
end repeat.
exe.
Well, here's my attempt for the time being:
data list list /age1 to age4.
begin data.
10 9 5 8
3
13 15
1 4 5
4 7 8 2
end data.
count valid=age1 to age4 (lo thru hi).
compute s=trunc(1+uniform(valid)).
vector age=age1 to age4.
compute myvar=age(s).
list age1 to age4 myvar.

How to tell when number has passed a multiple of another number?

Say I have a list of numbers:
foo = [random.randint(0, 9) for n in range(10)]
and I have a function
def bar(lst):
total = 0
for i in lst:
total += i
if total % 4 == 0:
print "passed multiple of 4"
bar(foo)
How do I get the print statement to execute if total skips over a multiple of 4?
EDIT: say foo = [4,5], then total takes the values 0, 4, and 9. I want the print statement to execute twice, once for 4, which it does, and once for 8, which total "skips over" when it jumps from 4 to 9.
I want the print statement to execute twice, once for 4, which it does, and once for 8, which total "skips over" when it jumps from 4 to 9.
So basically, you do not want to check the modulus, but just whether the total passed a multiple of four.
Something like this:
def bar(lst):
total = 0
# keep track of the numbers we need to “pass” to print the notice; the first one is 4
passed = 4
for i in lst:
total += i
# when the total passed the number
while total >= passed:
# print the notice
print('passed multiple of 4, total is {0}'.format(total))
# and skip to the next number
passed += 4
>>> bar([4, 5])
passed multiple of 4, total is 4
passed multiple of 4, total is 9
>>> bar([1, 3, 4])
passed multiple of 4, total is 4
passed multiple of 4, total is 8
>>> bar([1, 12])
passed multiple of 4, total is 13
passed multiple of 4, total is 13
passed multiple of 4, total is 13

DateTime subtraction in ruby 2?

I need to subtract two DateTime objects in order to find out the difference in hours between them.
I try to do the following:
a = DateTime.new(2015, 6, 20, 16)
b = DateTime.new(2015, 6, 21, 16)
puts a - b
I get (-1/1), the object of class Rational.
So, the question is, how do I find out what the difference betweent the two dates is? In hours or days, or whatever.
And what does this Rational mean/represent when I subtract DateTimes just like that?
BTW:
When I try to subtract DateTime's with the difference of 1 year, I get (366/1), so when I do (366/1).to_i, I get the number of days. But when I tried subtracting two DateTime's with the difference of 1 hour, it gave me -1, the number of hours. So, how do I also find out the meaning of the returned value (hours, days, years, seconds)?
When you substract two datetimes, you'll get the difference in days, not hours.
You get a Rational type for the precision (some float numbers cannot be expressed exactly with computers)
To get a number of hours, multiply the result by 24, for minutes multiply by 24*60 etc...
a = DateTime.new(2015, 6, 20, 16)
b = DateTime.new(2015, 6, 21, 16)
(a - b).to_i
# days
# => -1
((a - b)* 24).to_i
# hours
# => -24
# ...
Here's a link to the official doc
If you do subtraction on them as a Time object it will return the result in seconds and then you can multiply accordingly to get minutes/hours/days/whatever.
a = DateTime.new(2015, 6, 20, 16)
b = DateTime.new(2015, 6, 21, 16)
diff = b.to_time - a.to_time # 86400
hours = diff / 60 / 60 # 24

Resources