@@ -149,7 +149,7 @@ export class PivotUtil {
149149 const value = this . extractValueFromDimension ( col , recData ) ;
150150 path . push ( value ) ;
151151 const newValue = path . join ( '-' ) ;
152- const newObj = { value : newValue , expandable : col . expandable , children : null } ;
152+ const newObj = { value : newValue , expandable : col . expandable , children : null , dimension : col } ;
153153 if ( ! newObj . children ) {
154154 newObj . children = new Map < string , any > ( ) ;
155155 }
@@ -228,23 +228,28 @@ export class PivotUtil {
228228 . getFieldsHierarchy ( child [ pivotKeys . records ] , [ row ] , PivotDimensionType . Row , pivotKeys ) ;
229229 const siblingData2 = PivotUtil
230230 . processHierarchy ( hierarchyFields2 , child ?? [ ] , keys , 0 ) ;
231-
232- // add children to current level
233- for ( const sib of siblingData2 ) {
234- if ( sib [ row . memberName + '_' + pivotKeys . records ] ) {
235- child [ row . memberName + '_' + pivotKeys . records ] =
236- child [ row . memberName + '_' + pivotKeys . records ] . concat ( sib [ row . memberName + '_' + pivotKeys . records ] ) ;
237- child [ row . memberName ] = sib [ row . memberName ] ;
231+ if ( siblingData2 . length === 1 ) {
232+ child [ row . memberName ] = sibling [ row . memberName ] ;
233+ // add children to current level if dimensions have same depth
234+ for ( const sib of siblingData2 ) {
235+ if ( sib [ row . memberName + '_' + pivotKeys . records ] ) {
236+ child [ row . memberName + '_' + pivotKeys . records ] =
237+ child [ row . memberName + '_' + pivotKeys . records ]
238+ . concat ( sib [ row . memberName + '_' + pivotKeys . records ] ) ;
239+ child [ row . memberName ] = sib [ row . memberName ] ;
240+ }
241+ }
242+ } else {
243+ // otherwise overwrite direct child collection
244+ child [ row . memberName + '_' + pivotKeys . records ] = siblingData2 ;
238245 }
239- }
240-
241246 PivotUtil . processSiblingProperties ( child , siblingData2 , keys ) ;
242- if ( prevRowDim . childLevel ) {
243- // Get child dimensions now as well since we go a level deeper into the hierarchy.
244- // Keep above level dims as well since lower level dims correspond to upper sibling dims as well .
245- const childDimensions = prevRowDims . filter ( dim => ! ! dim . childLevel ) . map ( dim => dim . childLevel ) ;
246- this . processSubGroups ( row , [ ... prevRowDims , ... childDimensions ] , childCollection , pivotKeys ) ;
247- }
247+ }
248+ if ( prevRowDim . childLevel ) {
249+ // Get child dimensions now as well since we go a level deeper into the hierarchy .
250+ // Keep above level dims as well since lower level dims correspond to upper sibling dims as well.
251+ const childDimensions = prevRowDims . filter ( dim => ! ! dim . childLevel ) . map ( dim => dim . childLevel ) ;
252+ this . processSubGroups ( row , [ ... childDimensions ] , childCollection , pivotKeys ) ;
248253 }
249254 }
250255 }
@@ -357,10 +362,14 @@ export class PivotUtil {
357362 hierarchy . get ( val . value ) . children = new Map < string , any > ( ) ;
358363 }
359364 if ( ! childCollection || childCollection . size === 0 ) {
360- if ( hierarchy . get ( val . value ) [ recordsKey ] ) {
361- hierarchy . get ( val . value ) [ recordsKey ] . push ( rec ) ;
362- } else {
363- hierarchy . get ( val . value ) [ recordsKey ] = [ rec ] ;
365+ const dim = hierarchy . get ( val . value ) . dimension ;
366+ const isValid = this . extractValueFromDimension ( dim , rec ) === val . value ;
367+ if ( isValid ) {
368+ if ( hierarchy . get ( val . value ) [ recordsKey ] ) {
369+ hierarchy . get ( val . value ) [ recordsKey ] . push ( rec ) ;
370+ } else {
371+ hierarchy . get ( val . value ) [ recordsKey ] = [ rec ] ;
372+ }
364373 }
365374 } else {
366375 for ( const [ key , child ] of childCollection ) {
0 commit comments