Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Element115
Memorable Member
Memorable Member

Why does it take so long to execute this M code?

The following code snippet runs for hours and I do not know if it ever completes because after seeing how long the first run is, I canceled it. Here is the M code and table size is about 250K in import mode:

 

...	
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1),
    new_idx_list = List.Accumulate(#"Added Index"[Index],{0}, (state,current) =>  if current = 0 then {1} else if #"Added Index"{current-1}[Status] = #"Added Index"{current}[Status] then state & {List.Last(state)} else state & {List.Last(state)+1}),
    Add_columns= Table.FromColumns(Table.ToColumns(#"Added Index")&{new_idx_list})
in
    Add_columns

 

If with a small dataset like this, the M engine already struggles to generate 2 index columns, then what other solution is available?  There are only 2 categories in this table, ordered by datetime ascending, and the use case is:

 

1_assign the same index to each category cluster, ie starting with index == 1, if the first 10 rows are of the same category (cat1), then assign index value == 1 to all these rows;

 

2_if next 5 rows are of category 2 (cat2), then assign index value == 2 to all these;

 

3_if next rows are of cat1, then assign index value == 3 to these;

 

4_if next rows are of cat2, then assign index valuue == 4 to all these rows, and so on, effectively assigning the same index value to each group of rows that occur in the table if they belong to the same category AND are contiguous, thus as soon as the next row belongs to the other category, then a new index number (incremented by 1 from the previous index nuumber) will be assigned to this next group, etc.

 

 

The goal is to extract the first row from each group, that is, every time we detect a new index number, that is an index different by 1 from the previous, then we know that this is the first row of the group this index number identifies and we want to return a table that contains only these first rows.  

 

I hope this is clear enough but if not, please let me know, and I'll give it another shot.

 

1 ACCEPTED SOLUTION
HotChilli
Community Champion
Community Champion

A Table.Group with GroupKind.Local should accomplish this.  I'm not sure exactly what you want but grouping by status(?).  You can add an Index column afterwards.

View solution in original post

2 REPLIES 2
HotChilli
Community Champion
Community Champion

A Table.Group with GroupKind.Local should accomplish this.  I'm not sure exactly what you want but grouping by status(?).  You can add an Index column afterwards.

Deleted prev response... was doing some extra unnecessary steps... works like a charm now. Thanks again.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.