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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
PaulDBrown
Community Champion
Community Champion

Add a column to create subsets: index rows from 1 to 3

How can I add a column in Power Query which basically indexes rows from 1 to 3 (I need to split the data into 3 subsets). For example:

 

Index Field 1 Field 2 Field 3 Subset
1 A aa 25 1
2 B bb 25 2
3 C cc 49 3
4 D dd 38 1
5 E ee 37 2
6 F ff 42 3
7 G gg 23 1
8 H hh 31 2
9 I ii 23 3
10 J jj 27 1
11 K kk 33 2
12 L l 42 3

 

Thanks for your help





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






2 ACCEPTED SOLUTIONS
Vijay_A_Verma
Super User
Super User

Insert an Index column (default base is 0 which should not be changed).
Select the column - Transform menu - Standard - Modulo - Value 3
Select the column - Transform menu - Standard - Add - Value 1

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclSK1YlWcgKTzmDSBUy6gkk3MOkOJj3ApCeY9FKKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Data = _t]),
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    #"Calculated Modulo" = Table.TransformColumns(#"Added Index", {{"Index", each Number.Mod(_, 3), type number}}),
    #"Added to Column" = Table.TransformColumns(#"Calculated Modulo", {{"Index", each _ + 1, type number}})
in
    #"Added to Column"

 

View solution in original post

BA_Pete
Super User
Super User

Hi @PaulDBrown ,

 

Add a regular index column starting from zero, then a new column with this:

 

 

Number.Mod([Index], 3) + 1

 

 

Using your actual example data:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TY65DsJADET/xXUKvBtIKIFwJfzBKkXui/+vmQmKleJZ1uzsk0MQlUguoKow3BFDpYyCOGxXUNdb7tbcY7uBpsGIzxh+zWNsGWhbRql5+PMOuo55Yp4Ttgfoe3qcedh4gmFg1ZuHxhcYR1bVPLzgDaZp6/89esCag3nmQ2Ii5d8CLAvL3kzKIz7gu7uo/AE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, #"Field 1" = _t, #"Field 2" = _t, #"Field 3" = _t, Subset = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Field 1", type text}, {"Field 2", type text}, {"Field 3", Int64.Type}, {"Subset", Int64.Type}}),
    addIndex3 = Table.AddColumn(chgTypes, "Index3", each Number.Mod([Index] - 1, 3) + 1)
in
    addIndex3

 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

4 REPLIES 4
PaulDBrown
Community Champion
Community Champion

Super. Thank you very much @BA_Pete !





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






BA_Pete
Super User
Super User

Hi @PaulDBrown ,

 

Add a regular index column starting from zero, then a new column with this:

 

 

Number.Mod([Index], 3) + 1

 

 

Using your actual example data:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TY65DsJADET/xXUKvBtIKIFwJfzBKkXui/+vmQmKleJZ1uzsk0MQlUguoKow3BFDpYyCOGxXUNdb7tbcY7uBpsGIzxh+zWNsGWhbRql5+PMOuo55Yp4Ttgfoe3qcedh4gmFg1ZuHxhcYR1bVPLzgDaZp6/89esCag3nmQ2Ii5d8CLAvL3kzKIz7gu7uo/AE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, #"Field 1" = _t, #"Field 2" = _t, #"Field 3" = _t, Subset = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Field 1", type text}, {"Field 2", type text}, {"Field 3", Int64.Type}, {"Subset", Int64.Type}}),
    addIndex3 = Table.AddColumn(chgTypes, "Index3", each Number.Mod([Index] - 1, 3) + 1)
in
    addIndex3

 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Vijay_A_Verma
Super User
Super User

Insert an Index column (default base is 0 which should not be changed).
Select the column - Transform menu - Standard - Modulo - Value 3
Select the column - Transform menu - Standard - Add - Value 1

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclSK1YlWcgKTzmDSBUy6gkk3MOkOJj3ApCeY9FKKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Data = _t]),
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    #"Calculated Modulo" = Table.TransformColumns(#"Added Index", {{"Index", each Number.Mod(_, 3), type number}}),
    #"Added to Column" = Table.TransformColumns(#"Calculated Modulo", {{"Index", each _ + 1, type number}})
in
    #"Added to Column"

 

Thank you very much, @Vijay_A_Verma !





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

Check out the September 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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

Top Solution Authors