KoolReport's Forum

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

Pivot table sorting issue #2087

Closed Christian Voß opened this topic on on May 17, 2021 - 4 comments

Christian Voß commented on May 17, 2021

Hi all,

I hope you're well!

I have an issue with the pivot table. I have two aggregates "sum of points" and "sum of total". Ideally I'd like have the percentage "sum of points" / "sum of total" to show in the pivot table instead of the actual values.

I figured out how to do that by using the mapping functionality of the datacell. However, now I'd like to sort the values by this percentage. This doesn't work as it seems that i can only sort by the initial values.

Is there a work around?

Thanks in advance!

Best regards

Christian

Sebastian Morales commented on May 18, 2021

Christian, would you pls give some examples with specific numbers for us to get an idea of the percentage? Tks,

Christian Voß commented on May 18, 2021

Hi Sebastian,

sure no problem and thanks for your support:

Lets image I had this data:

Row    | Category    |       Score      |     Max
A             X                2                2
A             Y                3                5
A             X                1                4
B             X                2                2
B             Y                2                3
B             Y                4                5
B             X                3                3
C             Y                2                4
C             Y                1                1
D             X                2                5
D             Y                1                1
D             Y                0                1
E             X                2                3
F             Y                2                2
F             X                2                4

I have a select to filter for the category. That means if I don't apply a filter the pivot table looks like this:

Row        |         Score            |         Max
A                      6                         11
B                     11                        13
C                      3                         5
D                      3                         7
E                      2                         3
F                      4                         6

However, instead of the two column Score and Max, I'd like to show a percentage = Score *100 / Max % It should look like this:

Row        |         Percentage
A                        54.54%                                
B                        84.62%                              
C                        60.00%                               
D                        42.86%                                
E                        66.67%                               
F                        66.67%                                

I currently achieve that using the mapping functionality for the datacell:

'map' => [
    'dataCell' => function($v, $ci) {
        if ($ci['fieldName'] === 'score - sum') {
            return sprintf("%.2f%%", $ci['indexToData'][$ci['rowIndex']][$ci['columnIndex']]['max - sum'] > 0 ?                 $ci['indexToData'][$ci['rowIndex']][$ci['columnIndex']]['score - sum] / $ci['indexToData'][$ci['rowIndex']][$ci['columnIndex']]['max- sum']  * 100: 0 );
} else {
    return $v;
}

My problem is that I now want to sort the field by the percentage. That means my end result should look like this:

Row        |         Percentage
B                        84.62% 
E                        66.67%                               
F                        66.67% 
C                        60.00%   
A                        54.54%                                                            
D                        42.86%                                
                               

However, if I apply the sort functionality like this:

 'rowSort' => array(
                        'score- sum' => 'desc' 
                        
                    ),

It uses the values before the mapping so that I don't get the result above.

I also have the category above for which I need to be able to filter. In my real life example I have a couple of categories and various filter.

I hope this helps. Thanks a lot!

Christian

Sebastian Morales commented on May 18, 2021

Christian, thanks for your detail feedback! I think "rowSort" wouldn't work here because the percentage values are computed after they were sorted.

In this case I suggest you try the Pivot2D process with sum and max, which results in a table like data structure:

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

Pipe it to a datastore and print out the datastore's data to have a view of its data structure.

After that use the Map process right after Pivot2D to compute the percentage column:

https://www.koolreport.com/docs/processes/map/

Finally in your report's view, use "rowSort" for the percentage column.

Let us know if you need further detail. Rgds,

Christian Voß commented on May 18, 2021

Hi Sebastian,

thank you very much! The second link helped a lot, but it wasn't necessary to use the Pivot2D.

Thanks for your support!

Best regards

Christian

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