Forum Discussion
Consecutive Row Counter Column
- 7 years ago
See if this works:
Column 2 = VAR __index = CALCULATE(MAX([Index])) VAR __tmpTable1 = FILTER('Table34',[Animal]=EARLIER([Animal])&&[Index]<EARLIER([Index])) VAR __tmpTable2 = ADDCOLUMNS(__tmpTable1,"__diff",[Index] - MAXX(FILTER(ALL('Table34'),[Index]<EARLIER([Index]) && [Animal]=EARLIER([Animal])),[Index])) VAR __max = MAXX(__tmpTable2,[Index]) VAR __maxStart = MAXX(FILTER(__tmpTable2,[__diff]>1),[Index]) VAR __tmpTable3 = FILTER(__tmpTable2,[Index]>=__maxStart) RETURN IF(ISBLANK(__max),1,IF(__max=[Index]-1,COUNTX(__tmpTable3,[Index])+1,1))PBIX is attached.
Thanks Felix for your reply!
This unfortunately must be done with a calculated column since the "Animal" column does not exist in the query.
Hi MFelix,
setting the 4th (optional) parameter in the Table.Group-function to "GroupKind.Local" will do some magic in this use case ;-)
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCslMTy1SitUhnuWTmZ+Hg4FNuWtOakFGYl4JAQ4eO2MB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Animal = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Animal", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Animal"}, {{"Partition", each Table.AddIndexColumn(_, "Counter", 1,1), type table}}, GroupKind.Local),
#"Expanded Partition" = Table.ExpandTableColumn(#"Grouped Rows", "Partition", {"Counter"}, {"Counter"})
in
#"Expanded Partition"
Anonymous: May I ask what your source-data exactly looks like?
- Anonymous7 years agoNot applicable
ImkeFMFelixThank you so much for replying!!!
This dataset is much more hairy than what I posted as an example and cannot begin to share the source data :/ . I require a DAX solution unfortunately.
- Greg_Deckler7 years agoCommunity Champion
See if this works:
Column 2 = VAR __index = CALCULATE(MAX([Index])) VAR __tmpTable1 = FILTER('Table34',[Animal]=EARLIER([Animal])&&[Index]<EARLIER([Index])) VAR __tmpTable2 = ADDCOLUMNS(__tmpTable1,"__diff",[Index] - MAXX(FILTER(ALL('Table34'),[Index]<EARLIER([Index]) && [Animal]=EARLIER([Animal])),[Index])) VAR __max = MAXX(__tmpTable2,[Index]) VAR __maxStart = MAXX(FILTER(__tmpTable2,[__diff]>1),[Index]) VAR __tmpTable3 = FILTER(__tmpTable2,[Index]>=__maxStart) RETURN IF(ISBLANK(__max),1,IF(__max=[Index]-1,COUNTX(__tmpTable3,[Index])+1,1))PBIX is attached.
- Sean7 years agoCommunity Champion
I just wanted to add this link to this topic!
Related to ImkeF's solution...
https://blog.crossjoin.co.uk/2014/01/03/aggregating-by-local-groups-in-power-query/
Anyway I'll "process" :smileyvery-happy: Greg_Deckler's solution later today :smileywink: