Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines
Hi Ralf,
Probably could try the customAggregates
property of Pivot process:
$distinctValues = [];
...
->pipe(new Pivot(array(
"dimensions" => array(
"column" => ...,
"row" => ...,
),
"aggregates" => array(
"distinctCount" => "field1",
),
"customAggregates" => [
"distinctCount" => [
"{initValue}" => 0,
"{aggValue}" => function($aggValue, $value, $field, $row) use (& $distinctValues) {
if (! in_array($value, $distinctValues) {
$aggValue++;
array_push($distinctValues, $value);
}
return $aggValue;
},
]
],
)))
...
Let us know if it works for you. Thanks!
No, it is not working. It's only showing zeros. Is the $distinctValues resetting for each level, as a normal aggregate would do?
Here is my view configuration:
'waitingFields' => array(
'factor - sum' => 'data',
'detail_id - count' => 'data',
'order_id - distinctCount' => 'data',
Hi Ralf,
To count distinct values to each cell level, I think we need some changes in the Pivot process as well. Please open the file pivot/processes/Pivot.php and replace these lines:
...
$datum[$op] = $this->aggValue($op, $datum[$op], $row[$af], $af, $row);
...
$aggValue = is_callable($func) ?
$func($aggValue, $value, $af, $row) : $aggValue;
with these ones:
...
$datum[$op] = $this->aggValue($op, $datum[$op], $row[$af], $af, $row, $dn);
...
$aggValue = is_callable($func) ?
$func($aggValue, $value, $af, $row, $dn) : $aggValue;
Finally, use the custom aggregate like this:
use \koolreport\core\Utility as Util;
...
$distinctValues = [];
...
->pipe(new Pivot(array(
"dimensions" => array(
"column" => ...,
"row" => ...,
),
"aggregates" => array(
"distinctCount" => "field1",
),
"customAggregates" => [
"distinctCount" => [
"{initValue}" => 0,
"{aggValue}" => function($aggValue, $value, $field, $row, $dn) use (& $distinctValues) {
$cellDistinctValues = Util::init($distinctValues, $dn, []);
if (! in_array($value, $cellDistinctValues)) {
$aggValue++;
array_push($distinctValues[$dn], $value);
}
return $aggValue;
},
]
],
)))
...
Let me know your result. Thanks!
Let KoolReport help you to make great reports. It's free & open-source released under MIT license.
Download KoolReport View demo