Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Using variable for passing values in birt report
Elja
<p>Hello.<br>
I'm creating a report, in which I need to calculate a value of 2 fields, compare it to a 3.rd one. Depending on the values, I need to calculate a value and pass it to the next row and repeat the calculations there.</p>
<p> </p>
<p>I desperately need some help. I already thought I had this solved, but as I ran the report again, the report doesn't produce any result set. Although the main data set preview shows the correct collection of dataset rows..</p>
<p> </p>
<p>I checked the table shouldn't contain any extra bindings and there are no visibility issues.</p>
<p>
Now, I created:</p>
<p>1) global variable "jaannos"</p>
<p>2) I copy the dataSetRow value "SUMMA" to the global variable in Main data sets onFetch -script as follows:</p>
<p>vars["jaannos"] = dataSetRow["SUMMA"].value;</p>
<p>* In the main table I computed the value like this:</p>
<p>if((row["LIIKAMAKSU"].value + row["KULUKORVAUS"].value) < vars["jaannos"].value)<br>
{<br>
(row["LIIKAMAKSU"].value + row["KULUKORVAUS"].value)<br>
}<br>
else {<br>
vars["jaannos"]<br>
}</p>
<p>3) I count the remaining value, if any like this to another dedicated field:</p>
<p>if((row["LIIKAMAKSU"].value + row["KULUKORVAUS"].value) < row["Muuttujan_arvo"].value)<br>
{<br>
row["Muuttujan_arvo"].value - (row["LIIKAMAKSU"].value + row["KULUKORVAUS"].value)<br>
}<br>
else {<br>
0<br>
//vars["jaannos"]<br>
}</p>
<p>4) I replace the global variable value with the remaining value like follows:</p>
<p>vars["jaannos"].value = row["Seur_rivi"].value;</p>
<p> </p>
<p>The points 2-4 are used as data fields expression formulas..</p>
<p> </p>
<p>RESULT:</p>
<p>No values appear in the report.</p>
<p> </p>
<p>Do you have any idea where the problem might be?</p>
<p> </p>
<p>- Elja</p>
Find more posts tagged with
Comments
micajblock
<p>Can you provide an example using a text file with dummy data? Also what version are you using?</p>
Elja
<p>Hey Mica.</p>
<p>Thanks for assistance. I created a simple excel file and tried to fix the report to get the data out of it.</p>
<p> </p>
<p>In the sample report I present 3 tables, one just showing the data from Excel.</p>
<p> </p>
<p>The second one resembles the report I'm trying to produce. no variable is here.</p>
<p> </p>
<p>The third one is similar to the second table, but doesn't work. In this one I try to use the variable..</p>
<p> </p>
<p>- Elja</p>
<p> </p>
<p>Ps. The way I'm trying to use the variable is explained in the "Using_variable.docx" -attachment.</p>
<p> </p>
micajblock
<p>What version of BIRT are you using?</p>
Elja
<p>sorry, forgot to mention: 4.6</p>
micajblock
<p>I was not able to run the report. Can you make sure the report runs without Oracle?</p>
Elja
<p>Hello Mblock.</p>
<p>My bad, I didn't remove the old data sources and sets and there was some binding left in the background.</p>
<p> </p>
<p>Now I removed them and it seems to do the job.</p>
<p>The tables are better looking in pdf-format.</p>
<p> </p>
<p>- Elja</p>
micajblock
<p>Are you still having an issue? I am still having problems running the report.</p>
<p> </p>
<p>P.S. Why do you use the notation of row["fieldname"]<strong>.value? </strong>There is no need for the '.value' (it is actually causing me issues when running reports?</p>
<p> </p>
<p>P.P.S. If you are still having issues can you share a screenshot of the 'Output Columns' tab of the data set so I can see what data types you are using. for some reason it is erasing it for me. Also provide details of what you are trying to accomplish.</p>
Elja
<p>Hello Mblock.</p>
<p> </p>
<p>Yes, this problem still exists. I've been working on some other stuff and hoping I could find the answer from here.</p>
<p> </p>
<p>1) I've been using row["fieldname"].value -notation since we discovered that this was the necessary way to compare NULL-value in before open -script. I guess it shouldn't be done here. I'll test without the .value.</p>
<p> </p>
<p>2) I'll attach the output columns.</p>
<p> </p>
<p>3) The goal. I'll try to put this in a nut shell:</p>
<p>In this report, a value counted from a substraction (based on database field values) needs to be passed to the next row for a new calculation..</p>
<p> </p>
<p>Example:</p>
<p>* There are 3 values on row 1: A (Liikamaksu), B (Kulukorvaus), C (Summa)</p>
<p>Case 1: A+B < C, C-(A+
is transferred to the next row.</p>
<p>Case 2: A+B >= C, C is presented in the row, now value is transferred to the next row.</p>
micajblock
<p>Take a look at the simplified example</p>
Elja
<p>Hey Mica and thanks.</p>
<p> </p>
<p>I think there are some things in your example that need tuning:</p>
<p>1) The value of SUMMA should be presented, if SUMMA < LIIKAMAKSU + KULUKORVAUS</p>
<p>else "LIIKAMAKSU + KULUKORVAUS"</p>
<p>2) The remaining value of SUMMA - (LIIKAMAKSU + KULUKORVAUS) should be transmitted to variable and be used in the next rows calculation.</p>
<p>Let's see with an example:</p>
<p>LIIKAMAKSU = 300 (on each row)</p>
<p>KULUKORVAUS = 200 (on each row)</p>
<p>SUMMA = 1100 (original value, this will change row by row)</p>
<p> </p>
<p>* First row: SUMMA > LIIKAMAKSU + KULUKORVAUS; 1100 > 300+200 the value on this row should be "LIIKAMAKSU + KULUKORVAUS" = 500.</p>
<p>* For the next row, this LIIKAMAKSU+KULUKORVAUS -> 500 should be subtracted from old SUMMA to create the value of new SUMMA:</p>
<p>SUMMA (new) = SUMMA (former) - (LIIKAMAKSU + KULUKORVAUS) -> 1100 - 500 = 600.</p>
<p>* Second row: SUMMA (600) > (LIIKAMAKSU + KULUKORVAUS), 600 > 500. </p>
<p>Value of LIIKAMAKSU + KULUKORVAUS = 500 is presented. New SUMMA value is: 600-500 = 100. This is transmitted to the third row.</p>
<p>* Third row: SUMMA < (LIIKAMAKSU + KULUKORVAUS): 100 < 500. NOW, the value of SUMMA is presented (100).</p>
<p>As SUMMA was finally < LIIKAMAKSU + KULUKORVAUS, there's nothing to remain for the forth row and from now on the value of "LIIKAMAKSU + KULUKORVAUS" should be presented on each of the remaining rows.</p>
<p> </p>
<p>- Elja</p>
micajblock
<p>I am sorry, I am not sure I am clear on the logic. Instead of me can you provide a spreadsheet with formula's so I can be sure that I get it right? <br>
Thanks.</p>
Elja
<p>I created a simplified example in an excel sheet, in which I marked just the 3 fields in question and the value which reduces each time and is presented in the next row...</p>
<p> </p>
<p>Let's hope I'm able to explain this matter well enough this time.</p>
<p> </p>
<p>- Elja</p>
<p> </p>
<p>PS. I think I figured out one thing that might have made this case difficult to follow..</p>
<p>1) The field "Liikamaksu" and "Kulukorvaus" can differ on each row.</p>
<p>The field "Summa" is a one time value, which is sliced into many rows, never exceeding the sum of "Liikamaksu + kulukorvaus".</p>
<p> </p>
<p>In a nutshell: In "Summa" -field should be represented the value of either:</p>
<p>1) Summa, IF Summa <= Liikamaksu + Kulukorvaus.</p>
<p>2) Liikamaksu + Kulukorvaus, IF Liikamaksu + Kulukorvaus < Summa. AND in this case, the remaining value of "Summa - (Liikamaksu + Kulukorvaus)" is counted and used in the next rows Summa field! This will be repeated until Summa <= Liikamaksu + Kulukorvaus.</p>
Elja
<p>Hey, we managed to get this working. I'll explain shortly how..</p>
Elja
<p>Ok.</p>
<p>So, the basic dilemma was to obtain a certain value from database and then use it in Excel-like fashion on the report: to subtract the value of C from A+B until C < A+B and after that C = 0.</p>
<p> </p>
<p>Our solution for this matter was to use a variable and a computed field:</p>
<p> </p>
<p>1) we created a variable "jaannos" -> default value 0.</p>
<p>2) we created a computed field in the main data set: "Saatu_perittya" with the following formula:</p>
<p><strong><span style="color:#7f0055;"><span style="font-family:consolas;"><span style="font-size:10pt;">if</span></span></span></strong><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;"> (row[</span></span></span><span style="color:#2a00ff;"><span style="font-family:consolas;"><span style="font-size:10pt;">"LIIKAMAKSU"</span></span></span><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;">] + row[</span></span></span><span style="color:#2a00ff;"><span style="font-family:consolas;"><span style="font-size:10pt;">"KULUKORVAUS"</span></span></span><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;">] < row[</span></span></span><span style="color:#2a00ff;"><span style="font-family:consolas;"><span style="font-size:10pt;">"SUMMA"</span></span></span><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;">] - vars[</span></span></span><span style="color:#2a00ff;"><span style="font-family:consolas;"><span style="font-size:10pt;">"jaannos"</span></span></span><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;">]) {</span></span></span></p>
<p><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;"> row[</span></span></span><span style="color:#2a00ff;"><span style="font-family:consolas;"><span style="font-size:10pt;">"LIIKAMAKSU"</span></span></span><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;">] + row[</span></span></span><span style="color:#2a00ff;"><span style="font-family:consolas;"><span style="font-size:10pt;">"KULUKORVAUS"</span></span></span><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;">]</span></span></span></p>
<p><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;">}</span></span></span></p>
<p><strong><span style="color:#7f0055;"><span style="font-family:consolas;"><span style="font-size:10pt;">else</span></span></span></strong> <strong><span style="color:#7f0055;"><span style="font-family:consolas;"><span style="font-size:10pt;">if</span></span></span></strong><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;">(row[</span></span></span><span style="color:#2a00ff;"><span style="font-family:consolas;"><span style="font-size:10pt;">"SUMMA"</span></span></span><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;">] - vars[</span></span></span><span style="color:#2a00ff;"><span style="font-family:consolas;"><span style="font-size:10pt;">"jaannos"</span></span></span><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;">] > 0) {</span></span></span></p>
<p><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;"> row[</span></span></span><span style="color:#2a00ff;"><span style="font-family:consolas;"><span style="font-size:10pt;">"SUMMA"</span></span></span><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;">] - vars[</span></span></span><span style="color:#2a00ff;"><span style="font-family:consolas;"><span style="font-size:10pt;">"jaannos"</span></span></span><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;">]</span></span></span></p>
<p><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;">}</span></span></span></p>
<p><strong><span style="color:#7f0055;"><span style="font-family:consolas;"><span style="font-size:10pt;">else</span></span></span></strong><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;"> {</span></span></span></p>
<p><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;">0</span></span></span></p>
<p><span style="color:#000000;"><span style="font-family:consolas;"><span style="font-size:10pt;">}</span></span></span></p>
<p> </p>
<p>* Here, LIIKAMAKSU = A, KULUKORVAUS = B and SUMMA = C. C is the value that is obtained once from the database and after that diviced into many rows after it's been used.</p>
<p> </p>
<p>3) variable "jaannos" is given a new value also in computed column "Col_Jaannos":</p>
<p>vars["jaannos"] += row["LIIKAMAKSU"] + row["KULUKORVAUS"]</p>
<p>-> jaannos (new) = jaannos (old) + A + B</p>
<p> </p>
<p>Now, this will affect the formula shown in number 2 to give the correct values to the report.</p>
<p>I didn't go with the onFetch script anymore as it seemed to be run on every row. (Before I thought it was run once).</p>
<p> </p>
<p>- Elja</p>
micajblock
<p>So you have a solution? I was going to look at this, this morning.</p>
micajblock
<p>I think I created a much simpler solution. I have one report variable named 'nextRow' with an initial value of -1. Then I have a a single data binding with this expression. It seems to work. Let me know if this works for you.</p>
<div>
<pre class="_prettyXprint">
if (vars["nextRow"]==-1) {
vars["nextRow"]=row["SUMMA"];
}
if (row["LIIKAMAKSU"]+row["KULUKORVAUS"]<vars["nextRow"]) {
vars["nextRow"]=vars["nextRow"]-(row["LIIKAMAKSU"]+row["KULUKORVAUS"]);
row["LIIKAMAKSU"]+row["KULUKORVAUS"];
}
else {vars["nextRow"]}</pre>
</div>
<p> </p>