'Power Query Assign group identification every n number of elements
I have a CSV file that I need to import with PowerQuery. I want to assign groups of 3 and give them a name or room number. I know it is possible to use the Table.FillUp function to fill the values up. But I don't know how to assign a variable or another method to change the classroom identification
Parts of M code
#"Added Index" = Table.AddIndexColumn(Source, "Fila", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "CadaV3", each if Number.Mod([Fila], 3)=0 then 1 else 0)
Solution 1:[1]
If your student IDs are consecutive, per your example, then you can use Number.IntegerDivide:
#"Added Room Number" = Table.AddColumn(#"Previous Step", "Room Number", each Number.IntegerDivide([studentId] - 1,3))
Alternatively, add an Index column, and use that similarly:
#"Added Index" = Table.AddIndexColumn(#"Previous Step", "Index", 1, 1, Int64.Type),
#"Added Room Number" = Table.AddColumn(#"Added Index", "Room Number", each Number.IntegerDivide([Index] - 1,3))
Solution 2:[2]
Depending on only the assign groups of 3 direction, with no dependency on Student ID or Index columns:
good for up to 26 classrooms -- if it might be more, we need a different approach
et
Source = Excel.CurrentWorkbook(){[Name="Students"]}[Content],
//generate a list of classrooms "by threes"
classRooms = List.Transform(
List.Range(
List.Sort(
List.Repeat({1..
Number.RoundAwayFromZero(Table.RowCount(Source)/3,0)},3)),
0,Table.RowCount(Source)), each Character.FromNumber(_+64)),
//add the classroomID column
roomAssignment = Table.FromColumns(
Table.ToColumns(Source) & {classRooms},
type table[StudentID=Int64.Type, Name=text, ClassroomID=text])
in
roomAssignment
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Olly |
| Solution 2 | Ron Rosenfeld |

