Forum Discussion
Counting blocks of consecutive numbers -is it possible?
- 6 years ago
Sorry lazarus1907 ,
my bad. Forgot that this technique always compares against the first element in the group and not against the previous.
I'd recommend this approach then instead:
let // Replace this Source step by a reference to your table Source = Table.FromRows( Json.Document( Binary.Decompress( Binary.FromText( "JcqxEQAgCAPAXVJTEBQdhmP/NTxD98VXgTDQHW2FkCkvOeT9zTkpzznynCsnuh8=", BinaryEncoding.Base64 ), Compression.Deflate ) ), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Ref = _t, Code = _t] ), #"Changed Type" = Table.TransformColumnTypes(Source, {{"Ref", Int64.Type}, {"Code", Int64.Type}}), ToList = List.Buffer(Table.ToRecords(#"Changed Type")), AllBucketID = List.Generate( () => Record.AddField(Record.AddField(ToList{0}, "CountAll", 0), "CountGroup", 0), each [CountAll] <= List.Count(ToList), each [ CountAll = [CountAll] + 1, CountGroup = if [Code] = ToList{[CountAll]}[Code] - 1 then [CountGroup] else [CountGroup] + 1, Code = ToList{[CountAll]}[Code], Ref = ToList{[CountAll]}[Ref] ] ), SkipInitialElement = List.Skip(AllBucketID), #"Converted to Table" = Table.FromList( SkipInitialElement, Splitter.SplitByNothing(), null, null, ExtraValues.Error ), #"Expanded Column1" = Table.ExpandRecordColumn( #"Converted to Table", "Column1", {"Ref", "Code", "CountGroup"}, {"Ref", "Code", "CountGroup"} ), #"Grouped Rows" = Table.Group( #"Expanded Column1", {"CountGroup"}, { {"Ref", each List.Max([Ref]), type number}, {"Code", each List.Max([Code]), type number}, {"Count", each Table.RowCount(_), Int64.Type} }, GroupKind.Local ), #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows", {"CountGroup"}) in #"Removed Columns" - Anonymous6 years ago
using an auxiliary index column, the V element of the table.group function can dig the spider out of the hole.
let Origine = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Lc3LDcAwCATRXjjn4AV/a7HcfxtJBt/eSGjZ22SPSWHn2ea44sANV9xxwwN3PPHAC8/P7rmzcO6oEDkkEbkkJ3JKQeSW/udR71kj7lkn7tn/P1rJmIQyFuF2zgs=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Colonna1 = _t, Colonna2 = _t]), #"Modificato tipo" = Table.TransformColumnTypes(Origine,{{"Colonna2", Int64.Type}}), aci = Table.AddIndexColumn(#"Modificato tipo", "Indice", 0, 1, Int64.Type), #"Modificato tipo1" = Table.TransformColumnTypes(aci,{{"Colonna2", Int64.Type}}), #"Raggruppate righe" = Table.Group(#"Modificato tipo1", {"Colonna2", "Indice"}, {{"last", each Table.Last(_)},{"Conteggio", each Table.RowCount(_), Int64.Type}}, GroupKind.Local,(x,y)=>-x[Colonna2]+y[Colonna2]-(y[Indice]-x[Indice])), #"Tabella ref espansa" = Table.ExpandRecordColumn(#"Raggruppate righe", "last", {"Colonna1", "Colonna2"}, {"ref", "code"}), #"Rimosse colonne" = Table.RemoveColumns(#"Tabella ref espansa",{"Colonna2", "Indice"}) in #"Rimosse colonne"PS
questa soluzione presuppone che la tabella sia ordina in senso crescente nella colonna Code.
Hi lazarus1907 ,
there is a magical 5th parameter in the Table.Group function that allows for just this kind of logic:
let
// Replace this Source step by a reference to your table
Source = Table.FromRows(
Json.Document(
Binary.Decompress(
Binary.FromText(
"i45WMlTSUTI0MFCK1YlWMgKzDcFsYzDbGMw2AbNNlGJjAQ==",
BinaryEncoding.Base64
),
Compression.Deflate
)
),
let
_t = ((type nullable text) meta [Serialized.Text = true])
in
type table [Ref = _t, Code = _t]
),
#"Changed Type" = Table.TransformColumnTypes(Source, {{"Ref", Int64.Type}, {"Code", Int64.Type}}),
#"Grouped Rows" = Table.Group(
#"Changed Type",
{"Code"},
{{"Ref", each List.Max([Ref]), type nullable number}, {"Count", each List.Count(_)}},
// Don't skip the following parameter, as the logic would break otherwise:
GroupKind.Local,
// This is where the magic happens:
// The difference between the current Code value and the previous will be calculated and and a new group will be created if it is not equal to 1 (non consecutive)
(x, y) => Number.From(y[Code] - x[Code] <> 1)
)
in
#"Grouped Rows"
Brilliant answer, Imke, thanks a lot! Something is not quite right with your code, because I never get groups larget than 2, but I was unaware of these options, so maybe I can find the solution myself and learn in the process. I'll check your site carefully -it's not the first time I find useful stuff there!
- ImkeF6 years ago
Community Champion
Sorry lazarus1907 ,
my bad. Forgot that this technique always compares against the first element in the group and not against the previous.
I'd recommend this approach then instead:
let // Replace this Source step by a reference to your table Source = Table.FromRows( Json.Document( Binary.Decompress( Binary.FromText( "JcqxEQAgCAPAXVJTEBQdhmP/NTxD98VXgTDQHW2FkCkvOeT9zTkpzznynCsnuh8=", BinaryEncoding.Base64 ), Compression.Deflate ) ), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Ref = _t, Code = _t] ), #"Changed Type" = Table.TransformColumnTypes(Source, {{"Ref", Int64.Type}, {"Code", Int64.Type}}), ToList = List.Buffer(Table.ToRecords(#"Changed Type")), AllBucketID = List.Generate( () => Record.AddField(Record.AddField(ToList{0}, "CountAll", 0), "CountGroup", 0), each [CountAll] <= List.Count(ToList), each [ CountAll = [CountAll] + 1, CountGroup = if [Code] = ToList{[CountAll]}[Code] - 1 then [CountGroup] else [CountGroup] + 1, Code = ToList{[CountAll]}[Code], Ref = ToList{[CountAll]}[Ref] ] ), SkipInitialElement = List.Skip(AllBucketID), #"Converted to Table" = Table.FromList( SkipInitialElement, Splitter.SplitByNothing(), null, null, ExtraValues.Error ), #"Expanded Column1" = Table.ExpandRecordColumn( #"Converted to Table", "Column1", {"Ref", "Code", "CountGroup"}, {"Ref", "Code", "CountGroup"} ), #"Grouped Rows" = Table.Group( #"Expanded Column1", {"CountGroup"}, { {"Ref", each List.Max([Ref]), type number}, {"Code", each List.Max([Code]), type number}, {"Count", each Table.RowCount(_), Int64.Type} }, GroupKind.Local ), #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows", {"CountGroup"}) in #"Removed Columns"