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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply

Index by pattern

Hello,

My data have a pattern, generally of 4 rows, but sometimes it breaks by 3 or 5 rows.

I am looking for solution to start again the "pattern_index" from zero every time I have TRUE in the "pattern" column.

As now I use Number.Mod([index], 4) in the "patten_index".

Thanks in advance

 

indexpatternpattern_index
0TRUE0
1FALSE1
2FALSE2
3FALSE3
4TRUE0
5FALSE1
6FALSE2
7FALSE3
8TRUE0
9FALSE1
10FALSE2
11FALSE3
12TRUE0
13FALSE1
14FALSE2
15FALSE3
16TRUE0
17FALSE1
18FALSE2
19FALSE3
20TRUE0
21FALSE1
22FALSE2
23TRUE3
24FALSE0
25FALSE1
26FALSE2
27TRUE3
28FALSE0
29FALSE1
1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @Ara_Karapetyan,,

 

Fastest solution:

dufoq3_0-1710061423617.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XZArDoAwEAXvshrRfS20SAQoFB/V9P7XgATTQU52J5m8Wi3YYNdxr9aGav7CtuznRwJFUOq1EacJlEGl12acPBDZ4kInYzwR2eMTVAZ5IbJJWEe/ebiPIn6ZJCaJGylDZZK6pPYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [index = _t, pattern = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"pattern", type logical}, {"index", Int64.Type}}),
    IndexPattern = 
        [ p = List.Buffer(ChangedType[pattern]),
          lg = List.Generate( 
            ()=> [ x = 0, y = 0 ],
            each [x] <= List.Count(p) -1,
            each [ x = [x]+1, y = if p{x} = true then 0 else [y]+1 ],
            each [y] )
        ][lg],
    Ad_IndexPattern = Table.FromColumns(Table.ToColumns(ChangedType) & {IndexPattern}, Table.ColumnNames(ChangedType) & {"Index Pattern"})
in
    Ad_IndexPattern

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

2 REPLIES 2
dufoq3
Super User
Super User

Hi @Ara_Karapetyan,,

 

Fastest solution:

dufoq3_0-1710061423617.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XZArDoAwEAXvshrRfS20SAQoFB/V9P7XgATTQU52J5m8Wi3YYNdxr9aGav7CtuznRwJFUOq1EacJlEGl12acPBDZ4kInYzwR2eMTVAZ5IbJJWEe/ebiPIn6ZJCaJGylDZZK6pPYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [index = _t, pattern = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"pattern", type logical}, {"index", Int64.Type}}),
    IndexPattern = 
        [ p = List.Buffer(ChangedType[pattern]),
          lg = List.Generate( 
            ()=> [ x = 0, y = 0 ],
            each [x] <= List.Count(p) -1,
            each [ x = [x]+1, y = if p{x} = true then 0 else [y]+1 ],
            each [y] )
        ][lg],
    Ad_IndexPattern = Table.FromColumns(Table.ToColumns(ChangedType) & {IndexPattern}, Table.ColumnNames(ChangedType) & {"Index Pattern"})
in
    Ad_IndexPattern

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Brilliant!

Thank you!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors