KoolReport's Forum

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

PivotTable: count percent #1015

Open Paulo Pontes opened this topic on on Jul 28, 2019 - 4 comments

Paulo Pontes commented on Jul 28, 2019

How can I calculate the percentage of the grade count per academic term?

		$this->src("table")
			->pipe(new Pivot([
				"dimensions" => [
					"row" => "academic_term, grade",
				],
				"aggregates" => [
					"count" => "grade",
					'count percent' => 'grade'
				],
			]))
			->pipe($this->dataStore('studentGradeTable'));

example: intended => 0.8 = 20 / 25 count percent => 26.67% = 20 / 75

Paulo Pontes commented on Jul 30, 2019

please reply as soon as possible.

David Winterburn commented on Jul 31, 2019

Hi Paulo,

What you describe is currently not possible with the standard Pivot process and PivotTable widget. However, we do have another process called Pivot2D:

    ->pipe(new Pivot2D(array(
        //"column"=>"year",
        "row"=>"academic_term, grade"
        "aggregates"=>array(
            "count"=>"grade",
            "count percent"=>"grade", 
        )
    )))

which results in a normal table format. From there you could use the Map process to change the count percent column:

use \koolreport\core\Utility as Util;
...
//->pipe(Pivot2d) code
->pipe(new \koolreport\processes\Map(array(
    "{value}" => function($row, $metaData, $index, $mapState) {
        $allRows = Util::get($mapState, 'allRows', []);
        array_push($allRows, $row);
        $mapState['allRows '] = $allRows;
        return ['{rows}' => null, '{state}' => $mapState];
    },
    "{end}" => function($count, $mapState) {
        $allRows = Util::get($mapState, 'allRows', []);
        //loop through all rows
        //if a row is 2nd level, i.e its label column is like "1 Trimester || 0" 
        //find its parent row label of which is like "1 Trimester || {{all}}"
        //divide that row's count percent column by its parent row's count percent column
        //the count percent column name should be like "{{all}} || grade - count percent"
        return $allRows;
    }
)))
->pipe($this->dataStore("newCountPercentPivot"));

Finally display the datastore "newCountPercentPivot" with a PivotTable widget.

For better understanding, I suggest you pipe the Pivot2D's result to a datastore and show its content with Table or DataTables widget.

Let us know if you have any difficulty. Thanks!

Paulo Pontes commented on Aug 5, 2019

https://www.koolreport.com/docs/pivot/pivot_process/

please let me know where I can find information about Pivot2D.

David Winterburn commented on Aug 6, 2019

Hi Paulo,

We're sorry that we haven't prepared documentation for Pivot2D process yet. An example code is:

    ->pipe(new Pivot2D(array(
        "column"=>"orderYear, orderMonth",
        "row"=>"customerName, productLine, productName"
        "aggregates"=>array(
            "sum"=>"dollar_sales",
            "count"=>"dollar_sales",
            "avg"=>"dollar_sales",
            'sum percent' => 'dollar_sales',
            'count percent' => 'dollar_sales',
        )
    )))

A quick way to learn its data structure is pipe pivot2D result to a datastore and display it with a table. Let us know if you have any question. Thanks!

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

Pivot