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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
JOHN14400
Frequent Visitor

rank index custom columns query

Hello

 

I would like create in power query  a custom column in order to have rank index max 1 to 3 . I explain with a list of first name

Name          Index

ALBERT           1

ROGER           2

GILBERT         3

SOPHIE          1

THERESE        2

MARIE           3

JEAN             1

PAUL             2

etc and so on

what is the instructions in language M ?

thanks for your help

 

2 ACCEPTED SOLUTIONS
ziying35
Impactful Individual
Impactful Individual

Hi, @JOHN14400 

 

let
    Source = Table.FromRecords(Json.Document(Binary.Decompress(Binary.FromText("i65W8kvMTVWIKTUwSDTASilZKTn6OLkGhUC4uEilWh2izAryd3cNoo5R7p543UWsMcH+AR6ertRxUoiHa5BrME7DiDXG1zGIWi7ycnX0w28SaeYFOIb6EGdeLAA=",BinaryEncoding.Base64),Compression.Deflate))),
    addidx = Table.AddIndexColumn(Source,"Index"),
    result = Table.TransformColumns(addidx, {"Index", each Number.Mod(_, 3)+1, type number})
in
    result

If my code solves your problem, mark it as a solution

 

View solution in original post

ziying35
Impactful Individual
Impactful Individual

@JOHN14400 

According to the description of your problem, modify the code as follows. But I see that you are expecting the result to be a change every three lines, in which case the loop number doesn't need to be added. Just start directly with the split step.

 

 

let
    Source = Table.FromRecords(Json.Document(Binary.Decompress(Binary.FromText("i65W8kvMTVWyUnL0cXINCokpNTBINMBFKtXqwNUH+bu7BhGv3N0Tr/nISoP9Azw8XYk3OsTDNcg1GKcGZKW+jkGkmOzl6uiHXzWmngDHUB/i9MQCAA==",BinaryEncoding.Base64),Compression.Deflate))),
    addidx = Table.AddIndexColumn(Source,"Index"),
    mod = Table.TransformColumns(addidx, {"Index", each Number.Mod(_, 3)+1, type number}),
    split = Table.Split(mod, 3),
    trans = List.Transform(split, each Table.PromoteHeaders(Table.Transpose(Table.ReorderColumns(_,{"Index", "Name"})))),
    result = Table.Combine(trans)
in
    result

 

 

But that would require a code change, so you'll try with code above

View solution in original post

6 REPLIES 6
JOHN14400
Frequent Visitor

JOHN14400_0-1597394943971.png

 

ziying35
Impactful Individual
Impactful Individual

@JOHN14400 

According to the description of your problem, modify the code as follows. But I see that you are expecting the result to be a change every three lines, in which case the loop number doesn't need to be added. Just start directly with the split step.

 

 

let
    Source = Table.FromRecords(Json.Document(Binary.Decompress(Binary.FromText("i65W8kvMTVWyUnL0cXINCokpNTBINMBFKtXqwNUH+bu7BhGv3N0Tr/nISoP9Azw8XYk3OsTDNcg1GKcGZKW+jkGkmOzl6uiHXzWmngDHUB/i9MQCAA==",BinaryEncoding.Base64),Compression.Deflate))),
    addidx = Table.AddIndexColumn(Source,"Index"),
    mod = Table.TransformColumns(addidx, {"Index", each Number.Mod(_, 3)+1, type number}),
    split = Table.Split(mod, 3),
    trans = List.Transform(split, each Table.PromoteHeaders(Table.Transpose(Table.ReorderColumns(_,{"Index", "Name"})))),
    result = Table.Combine(trans)
in
    result

 

 

But that would require a code change, so you'll try with code above

Hello ziying 35

Thanks for yours fasters answers it works !

 

ziying35
Impactful Individual
Impactful Individual

@JOHN14400 

According to the description of your problem, modify the code as follows. But I see that you are expecting the result to be a change every three lines, in which case the loop index number doesn't need to be added. Just start directly with the split step.

 

 

 

let
    Source = Table.FromRecords(Json.Document(Binary.Decompress(Binary.FromText("i65W8kvMTVWyUnL0cXINCokpNTBINMBFKtXqwNUH+bu7BhGv3N0Tr/nISoP9Azw8XYk3OsTDNcg1GKcGZKW+jkGkmOzl6uiHXzWmngDHUB/i9MQCAA==",BinaryEncoding.Base64),Compression.Deflate))),
    addidx = Table.AddIndexColumn(Source,"Index"),
    mod = Table.TransformColumns(addidx, {"Index", each Number.Mod(_, 3)+1, type number}),
    split = Table.Split(mod, 3),
    trans = List.Transform(split, each Table.PromoteHeaders(Table.Transpose(Table.ReorderColumns(_,{"Index", "Name"})))),
    result = Table.Combine(trans)
in
    result

 

 

 

 

ziying35
Impactful Individual
Impactful Individual

Hi, @JOHN14400 

 

let
    Source = Table.FromRecords(Json.Document(Binary.Decompress(Binary.FromText("i65W8kvMTVWIKTUwSDTASilZKTn6OLkGhUC4uEilWh2izAryd3cNoo5R7p543UWsMcH+AR6ertRxUoiHa5BrME7DiDXG1zGIWi7ycnX0w28SaeYFOIb6EGdeLAA=",BinaryEncoding.Base64),Compression.Deflate))),
    addidx = Table.AddIndexColumn(Source,"Index"),
    result = Table.TransformColumns(addidx, {"Index", each Number.Mod(_, 3)+1, type number})
in
    result

If my code solves your problem, mark it as a solution

 

THanks  for your solution via the function Mod. But in final way , i Would like to pivot in order to have the first name like a values.

I have a message error like (

There were too many elements in the enumeration to complete the operation).

Yet there is only 8 rows

 

 

 

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.