Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
THRUST SERVICES & TOOLS
CLOUD EDITIONS
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Computed column (average)
martinf
<p>Hi</p>
<p>I need to add a computed column.</p>
<p>I have guys who collect data from meters (but not every day). I would like to show in report the average by day for all days (average_by_day)</p>
<p> </p>
<p>Day incremental_field_meter average_by_day<br>
1 12 3<br>
2 3<br>
3 18 6<br>
4 6<br>
5 6<br>
6 6<br>
7 42 2 <br>
8 44 <br>
9 </p>
<p> </p>
<p>On day 3, we have 18, on day 1 we had 12 (18-12 = 6 / 2 (number of days) = 3 in average_by_day)</p>
<p>Advice: if I do (12-0-18) / -2 ('minus' days), I can have my value (3).</p>
<p> </p>
<p>I know I will need a variable to count day, etc...</p>
<p> </p>
<p>Someone can help?</p>
<p> </p>
<p>Thanks!</p>
<p> </p>
<p>Martin F</p>
Find more posts tagged with
Comments
micajblock
<p>Use datediff functions. If you provide sample data (CSV) and version of BIRT. I could probably build an example in a few minutes.</p>
martinf
<p>Mica</p>
<p> </p>
<p>CSV link</p>
<p>BIRT 4.5</p>
<p> </p>
<p>Martin</p>
<p> </p>
<p><a data-ipb='nomediaparse' href='
https://dl.dropboxusercontent.com/u/22288628/computed_average.csv'>https://dl.dropboxusercontent.com/u/22288628/computed_average.csv</a></p>
;
micajblock
<p>Is this how really how you get the data? No dates?</p>
martinf
<p>Mica</p>
<p> </p>
<p>No, I just want to simplify.</p>
<p>Find a new csv here</p>
<p> </p>
<p><a data-ipb='nomediaparse' href='
https://dl.dropboxusercontent.com/u/22288628/computed_average1.csv'>https://dl.dropboxusercontent.com/u/22288628/computed_average1.csv</a></p>
;
<p> </p>
<p>Martin</p>
micajblock
<p>Thanks. This second CSV is no harder. That said the challenge is not as easy as I thought, as this requires to passes on the data.</p>
martinf
<p>Mica</p>
<p> </p>
<p>You have some advices for me?</p>
<p> </p>
<p>Thank you for your time</p>
<p> </p>
<p>Martin</p>
micajblock
<p>Working on many projects, hope to get to this today if not Tuesday,</p>
Matthew L.
<p>Like Mica indicated earlier, this isn't an easy task.</p>
<p> </p>
<p>I'm sure there is a much better way to accomplish this, however I've attached an example to work from.</p>
<p>This makes the calculations based on your last CSV upload: <a data-ipb='nomediaparse' href='
https://dl.dropboxusercontent.com/u/22288628/computed_average1.csv'>https://dl.dropboxusercontent.com/u/22288628/computed_average1.csv</a></p>
;
<p> </p>
<p>I loaded the CSV in the open script of the "My Data Set" scripted data set.</p>
<p>You will need to change the path to point to your CSV file.</p>
<p> </p>
<p>All the code to accomplish this task is in the scripted data set called "Output Data Set".</p>
<p>In the beforeOpen statement, we query the data from "My Data Set" and build an array object to store the values in for later use.</p>
<p> </p>
<p>Then in the fetch statement of the scripted data set called "Output Data Set" we loop through the array object built in the beforeOpen statement and accomplish the math required to produce the output desired.</p>
<p> </p>
<p>Hopefully this is helpful</p>
micajblock
<p>Thanks Matthew! My idea was to calculate on the onCreate event and load an array, then display the array on the onRender event. This will only work if you are not using the runandrender task, but using separate task for run then render,</p>
<p> </p>
<p>Martin, let me know if the above solution works for you or if you want my solution </p>
martinf
<p>Thank you to both of you.</p>
<p>With some lag-lead queries, I was also able to have the number I want. But since I create a join dataset with all day of a month, I would like to be able to fill empty rows below a value... How can I do it? In next example, fill with value 8 for second an third of july... Computed column is my first choice</p>
<p> </p>
<p>2016-07-01 8</p>
<p>2016-07-02</p>
<p>2016-07-03</p>
<p>2016-07-04 12</p>
<p> </p>
<p>Martin</p>
micajblock
<p>Try this:</p>
<p> </p>
<p>In your design in the beforeFactory put this code:</p>
<pre class="_prettyXprint">
lastValue=-1</pre>
<p>;</p>
<p>Then create a data item with this in the expression:</p>
<div>
<pre class="_prettyXprint">
if (row["computed_average"]==null) {
lastValue;
}
else {
row["computed_average"];
}</pre>
</div>
<div>In the onCreate event of the data item put this code:</div>
<div>
<pre class="_prettyXprint">
if (lastValue==-1 || this.getRowData().getColumnValue("computed_average")!=null) {
lastValue = this.getRowData().getColumnValue("computed_average");
}</pre>
</div>
Matthew L.
<p>You could just create a computed column and check the "Value" column for a value.</p>
<p>If it contains a value you can store it in a report variable or global variable to be used in the next null row.</p>
<p> </p>
<p>Attached is an example, relative code below:</p>
<pre class="_prettyXprint _lang-js">
//Place this code in a computed column of your data set
//Change the next line to point to the column with your values
var dataSetColumnWithValues = row["Value"]; //Change this row["name"] per your value column
if(dataSetColumnWithValues){ //Check for value within row
reportContext.setPersistentGlobalVariable("Computed_Value", ""+dataSetColumnWithValues); //Place value in a global variable
dataSetColumnWithValues; //Return value
}else{ //If no value in row
parseInt(reportContext.getPersistentGlobalVariable("Computed_Value")); //Use exising global variable value
}
</pre>
martinf
<p>Matthew</p>
<p> </p>
<p>Work fine.</p>
<p>But another little thing: have an idea to have this work with decimal and not only integer...?</p>
<p> </p>
<p>2016-01-01 8.6</p>
<p>2016-01-02</p>
<p>2016-01-03 </p>
<p>...</p>
<p> </p>
<p>Martin</p>
Matthew L.
<p>Change parseInt to parseFloat:</p>
<pre class="_prettyXprint _lang-js">
//Place this code in a computed column of your data set
//Change the next line to point to the column with your values
var dataSetColumnWithValues = row["Value"]; //Change this row["name"] per your value column
if(dataSetColumnWithValues){ //Check for value within row
reportContext.setPersistentGlobalVariable("Computed_Value", ""+dataSetColumnWithValues); //Place value in a global variable
dataSetColumnWithValues; //Return value
}else{ //If no value in row
parseFloat(reportContext.getPersistentGlobalVariable("Computed_Value")); //Use exising global variable value
}
</pre>
martinf
<p>Matthew</p>
<p> </p>
<p>It doesn't seem to "see" a zero number. On the attached file, you see on left the number 4 follow by 0 (the day after). On right side (Computed Column), it fill with 4 instead of 0... Idea?</p>
<p> </p>
<p><a data-ipb='nomediaparse' href='
https://dl.dropboxusercontent.com/u/22288628/with_zero.png'>https://dl.dropboxusercontent.com/u/22288628/with_zero.png</a></p>
;
<p> </p>
<p>Thanks again</p>
<p> </p>
<p>Martin</p>
Matthew L.
<p>Try the following:</p>
<pre class="_prettyXprint _lang-js">
//Use this calculation to generate the results
var dataSetColumnWithValues = row["Value"]; //Change this row["name"] per your value column
if(dataSetColumnWithValues != null){ //Check for value within row
reportContext.setPersistentGlobalVariable("Computed_Value", ""+dataSetColumnWithValues); //Place value in a global variable
dataSetColumnWithValues; //Return value
}else{ //If no value in row
parseFloat(reportContext.getPersistentGlobalVariable("Computed_Value")); //Use exising global variable value
}
</pre>
martinf
<p>Matthew</p>
<p> </p>
<p>Now, can you imagine a way to fill the "previous" empty column?</p>
<p> </p>
<p>date number ave</p>
<p>1 21</p>
<p>2 </p>
<p>3 10 5.5</p>
<p> </p>
<p>Fill date 2 field ave with 5.5 because 21-10 = 11 / 2 days = 5.5...</p>
<p> </p>
<p>Martin</p>
martinf
<p>I'm now ok with my last question.</p>
<p>Thank you</p>
<p>Martin F</p>