Forum Discussion
Power Query Custom Grouping
- 1 year ago
Dicken You can use the 5th parameter of Table.Group to split this based on the equality of values in Item column.
Table.Group ( Sub, "Item", { "T", each _ }, GroupKind.Local, (x, y) => Byte.From ( x <> "" and y <> "" and x <> y ) )And since you have the "Total" word in the column Name that can also be used.
Table.Group ( Sub, {"Item", "Name"}, { "T", each _ }, GroupKind.Local, (x, y) => Byte.From ( y[Name] <> "Total" and x[Item] <> y[Item] ) ) - 1 year ago
Thanks, I'll have to work through that, not very good with custom comperers, and have not used
Byte.From, will go through it.
I may have questions,
Richad. - 1 year ago
Dicken (x, y) are are basically (CurrentGroup, CurrentRow), a group starts with the first row and then based on comparsion between x and y or just y with some condition either rows are kept in the CurrentGroup or a new group is started.
If a boolean operation between x and y return True then a new group is created, if it is False then CurrentRow is added to the CurrentGroup.
Byte.From is used because the accepted values by 5th argument are either 0 or 1, I could have used Number.From as well.
Now in your scenario you could use the following code, but I don't know how your data will look like or if you are even interested in a second local grouping, if you're then previous code is fine but if not then probably this should be enough, it just adds a small overhead that later if you don't like you will have to make Item = null for total row.
let ..., Sub = Table.Combine ( Group[Count] ), ReplaceWithNull = Table.ReplaceValue ( Sub, "", null, Replacer.ReplaceValue, { "Item" } ), FillDown = Table.FillDown ( ReplaceWithNull, { "Item" } ), Group2 = Table.Group ( FillDown, "Item", { "Count", each _ } ) in Group2
Thanks for the help , I've sort of got it,
= Table.Group( Source, {"Item"},
{{"N", each _ }} , 0 ,(x,y)=>
Number.From(
x[Item] <> y[Item] and y [Item] <> null ) )
I still find it odd true = new group false adds to same, so thank you for your explanation.