cancel
Showing results for
Did you mean:

# Cthulhu

Super User IV
11697 Views
Highlighted
Super User IV

## Cthulhu

Why Cthulhu? Because the mental gymnastics required to figure this out nearly drove me insane. And because the exact reason you would need this measure is probably beyond the comprehension of mere mortals. Besides, what else am I supposed to call it, "Repeating Counter Indexing Thingy"?

But, if you need a column or measure that counts a group of things consequetively but restarts after a non-consequetive row, well then you are likely the second person to need this...

```Cthulhu =
VAR __index = CALCULATE(MAX([Index])) //What is my current row index?
VAR __group = CALCULATE(MAX([Animal])) //What is my current group?
VAR __tmpTable1 = FILTER(ALL('Cthulhu'),[Animal]=__group&&[Index]<__index) //Return all rows earlier than the current row within the same "group"
VAR __tmpTable2 = ADDCOLUMNS(__tmpTable1,"__diff",[Index] - MAXX(FILTER(ALL('Cthulhu'),[Index]<EARLIER([Index]) && [Animal]=EARLIER([Animal])),[Index])) //For each returned row, calculate the difference between the current index value and the previous index value within the same group. For rows in grouped sequence, this will be 1 but for rows within a group that are out-of-sequence this value will be greater than 1
VAR __max = MAXX(__tmpTable2,[Index]) //Figure out the max index in the current filtered table.
VAR __maxStart = MAXX(FILTER(__tmpTable2,[__diff]>1),[Index]) //In order to account for "skips" in the grouping, figure out the max index value of the latest "skip" (the row right after the skip where the group starts again) This will be the greatest index where the difference from the previous index in the same group is greater than 1 (previous row)
VAR __tmpTable3 = FILTER(__tmpTable2,[Index]>=__maxStart) //Filter out all the other junk because we don't want to count rows before the skip
RETURN IF(ISBLANK(__max),1,IF(__max=__index-1,COUNTROWS(__tmpTable3)+1,1)) //If __max is blank, we know that we are at the start of the table, so 1. If the max index of our original table is 1 less than the current index, we know that we are in sequence so we count all of our filtered rows (which don't include rows past a "skip"), otherwise return 1 because we know we are on the row immediately after a "skip.```

The other person would be this guy @coreyweiss in this thread: https://community.powerbi.com/t5/Desktop/Consecutive-Row-Counter-Column/td-p/509553/highlight/false

### I have book! Learn Power BI from Packt

Proud to be a Datanaut!

Member

## Re: Cthulhu

Hahaha, I wish the people using this visual truly know the blood sweat and tears behind it! Thanks for your hard work Greg!

Member

## Re: Cthulhu

Note: this is used to splice up a sensor input by shift hour: ie

shift 1:

7am hour 1

8am hour 2

9am hour 3

Shift 2:

10pm hour 1

11pm hour 2

.....

Not for counting zoo animals!

Frequent Visitor

## Re: Cthulhu

I may be a 3rd guy... Looks like I may be using this work.

I would have preferred a Power Query based solution, as the mix of PQ and using add column or measure is a bit too unpredictable for a beginner like me. I have lots of PQ in a complex table, along with using this, as an add column.

It worked! But devs are buffled as to how it is all works.

Amazing work - I've just signed up to say thank you!...

Super User IV

## Re: Cthulhu

@eugenm - Glad it is of use! It was a fun one to put together. For a Power Query version, your best bet would be @ImkeF !

### I have book! Learn Power BI from Packt

Proud to be a Datanaut!

Super User III

## Re: Cthulhu

I call that "Nested Index"-column: https://www.youtube.com/watch?v=-3KFZaYImEY

Table.Group(YourTable, {"Animal"}, {{"All", each Table.AddIndexColumn(_,"Index",1,1), type table}})

and expand 😉

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark my post as a solution. Thanks!

Imke Feldmann

Frequent Visitor

## Re: Cthulhu

Thank you. I will circle back, should I have any questions.