KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Summing in columns #2166

Open Richb201 opened this topic on on Jun 28, 2021 - 4 comments

Richb201 commented on Jun 28, 2021

I have a need to sum columns in my datastore. The same datastore is used in other places in the report so I don't want to modify it. Here is an example. My datastore has columns business component, role and wages. There are only three rows in this example. Each is for a different employee, business_component, and taxyear. I'd like to have columns in my table business component, research$,support$,superv$,totalresearch$,totalact$

"showFooter"=>"bottom",
        "columns"=>array(
            "business_component"=>array("label"=>"Business Component"),
            "a"=>array("label"=>"research$"),
            "b"=>array("label"=>"support$"),
            "c"=>array("label"=>"superv$"),

etc

Should I be using piping to create these totals within koolreport?

Richb201 commented on Jun 29, 2021

With much effort (a whole day) I managed to solve this. I just have one problem.

           array(
                "label"=>"Substantially All %",
                "value"=>function($row) {
                    return $row["research_dol"]/($row["research_dol"]+$row["support_dol"]+$row["supervision_dol"]);
                },
                "type"=>"number",
                "prefix"=>"%",
            )

if ($row["research_dol"]+$row["support_dol"]+$row["supervision_dol"]) equals zero, I get a divide by zero error. What can I do about this in the above structure?

Sebastian Morales commented on Jun 29, 2021

Just add an if to see if ($row["research_dol"]+$row["support_dol"]+$row["supervision_dol"] == 0) and decide which result to return in that case. This result when divisor is zero depends on your business domain so we can not say much. Otherwise return the quotient like normal. Rgds,

Richb201 commented on Jun 29, 2021

Something like this?

array(
                "label"=>"POE$/total$",
                "value"=>function($row) {
                    if (($row["research_dol"]+$row["support_dol"]+$row["supervision_dol"])==0)
                         return 0;
                    else
                        return($row["research_dol"]/($row["research_dol"]+$row["support_dol"]+$row["supervision_dol"]));
                },
                "cssStyle" => "text-align:center",
                'formatValue'=>' @value %', // format to $12
                "type"=>"number",
                "prefix"=>"%",
            )
Richb201 commented on Jun 29, 2021

seems to work. thx

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed
solved

None