This is best Fabric, Power BI, SQL and AI community event. How do we know? The last event sold out! Save €200 with code FABCMTY200.
Register nowA new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.
I have a table with 3 columns that signify the unique ID, the day in the month the ID is serviced and the sequence in service on the given day. I have another table with 3 columns that signify the unique ID, the plant the ID gets fulfilled from and the number that is assigned to the grouping of IDs.
I am in need of assigning the plant as the 1st stop or origination location ahead of every Day Seq 1 and the plant as the last stop or destination location in order to correctly calculate distance per day. Is it possible to add rows and change the sequencing to get the intended result below?
| ID | Day # | Day Seq |
| B | 2 | 1 |
| C | 2 | 2 |
| D | 2 | 3 |
| E | 2 | 4 |
| F | 2 | 5 |
| G | 2 | 6 |
| H | 2 | 7 |
| I | 2 | 8 |
| J | 2 | 9 |
| ID | Number | Plant |
| B | 12 | 456 |
| C | 12 | 456 |
| D | 12 | 456 |
| E | 12 | 456 |
| F | 12 | 456 |
| G | 12 | 456 |
| H | 12 | 456 |
| I | 12 | 456 |
| J | 12 | 456 |
Intended result:
| ID | Day # | Day Seq |
| 456 | 2 | 1 |
| B | 2 | 2 |
| C | 2 | 3 |
| D | 2 | 4 |
| E | 2 | 5 |
| F | 2 | 6 |
| G | 2 | 7 |
| H | 2 | 8 |
| I | 2 | 9 |
| J | 2 | 10 |
| 456 | 2 | 11 |
Hi @unknown917 ,
From what I could understand from your query, please look over this M code and see if this what you are looking for as the ouput?
If the result is supposed to be more dynamic in terms of joins between the two tables, this could be a good starting point.
I'll attach the excel file for your reference. Here is the M code below:
let
Source = [Src],
T1 = Table.TransformColumnTypes(
Source,
{{"ID", type text}, {"Number", Int64.Type}, {"Plant", Int64.Type}},
"en-US"
),
T3 = Table.AddColumn(
Table.Group(T2, {"Day #"}, {{"A", each _}}),
"F",
each Table.TransformColumns(
Table.FirstN([A], 1),
{"ID", each T1[Plant]{List.PositionOf(T1[ID], _)}}
)
),
T4 = Table.AddColumn(
T3,
"L",
each Table.TransformColumns(
Table.TransformColumns(
Table.LastN([A], 1),
{"ID", each T1[Plant]{List.PositionOf(T1[ID], _)}}
),
{"Day Seq", each _ + 2}
)
),
C = Table.Combine(
Table.AddColumn(T4, "C", each [F] & Table.TransformColumns([A], {"Day Seq", each _ + 1}) & [L])[
C
]
)
in
C
Thanks,
Yes, so it seems we'll have to input the required two tables here in this code:
Source is the second table that is there in your problem statement. The table that contains ID, Number and Plant columns.
T2 is the first table which contains ID, Day #, Day Seq.
Can you try modifying the queries with these required input tables in the queries and see if that works out?
Also, check out the Power Query Editor for the file linked above.
Thanks,
Hi @unknown917,
Hope you're doing well!
In Power Query, create a New Blank Query and paste this M code:
let
// Reference your existing tables (rename to match your actual query names)
RouteTable = Excel.CurrentWorkbook(){[Name="RouteTable"]}[Content],
PlantTable = Excel.CurrentWorkbook(){[Name="PlantTable"]}[Content],
// Ensure correct types
RouteTyped = Table.TransformColumnTypes(RouteTable,{
{"ID", type text},
{"Day #", Int64.Type},
{"Day Seq", Int64.Type}
}),
PlantTyped = Table.TransformColumnTypes(PlantTable,{
{"ID", type text},
{"Number", Int64.Type},
{"Plant", type text}
}),
// Join Plant info onto Route rows
Joined = Table.NestedJoin(
RouteTyped, {"ID"},
PlantTyped, {"ID"},
"PlantData", JoinKind.Left
),
Expanded = Table.ExpandTableColumn(
Joined, "PlantData", {"Plant", "Number"}, {"Plant", "Number"}
),
// Group by Day # (and Number/Plant if multiple groups exist)
Grouped = Table.Group(
Expanded,
{"Day #", "Number", "Plant"},
{{"Rows", each _, type table}}
),
// For each group: add START row (plant, seq=1), re-seq original rows, add END row
Transformed = Table.TransformColumns(Grouped, {"Rows", each
let
grp = _,
dayNum = grp[Day #]{0},
plant = grp[Plant]{0},
rowCount = Table.RowCount(grp),
// Build START row
startRow = #table(
{"ID", "Day #", "Day Seq"},
{{plant, dayNum, 1}}
),
// Re-sequence original rows starting at 2
reSeqd = Table.AddColumn(
Table.SelectColumns(grp, {"ID", "Day #"}),
"Day Seq",
each Table.RowCount(
Table.SelectRows(
Table.SelectColumns(grp, {"ID"}),
(r) => r[ID] <= [ID] // positional trick — see note below
)
) + 1
),
// Better re-sequencing using index
withIndex = Table.AddIndexColumn(
Table.SelectColumns(grp, {"ID", "Day #"}),
"Day Seq", 2, 1
),
// Build END row
endRow = #table(
{"ID", "Day #", "Day Seq"},
{{plant, dayNum, rowCount + 2}}
),
// Stack all three parts
Final = Table.Combine({startRow, withIndex, endRow})
in
Final
}),
// Expand grouped results back into flat table
Result = Table.Combine(Transformed[Rows])
in
Result
Here's the clean, production-ready version with no noise:
let
RouteTable = YourRouteQueryName, // ← change to your query name
PlantTable = YourPlantQueryName, // ← change to your query name
Joined = Table.NestedJoin(RouteTable, {"ID"}, PlantTable, {"ID"}, "P", JoinKind.Left),
Expanded = Table.ExpandTableColumn(Joined, "P", {"Plant", "Number"}),
Grouped = Table.Group(Expanded, {"Day #", "Plant", "Number"}, {{"Rows", each _, type table}}),
AddedRows = Table.AddColumn(Grouped, "Final", each
let
src=[Rows],
day = [Day #],
plant = [Plant],
cnt = Table.RowCount(src),
startRow = #table({"ID","Day #","Day Seq"}, {{plant, day, 1}}),
middle = Table.AddIndexColumn(Table.SelectColumns(src,{"ID","Day #"}), "Day Seq", 2, 1),
endRow = #table({"ID","Day #","Day Seq"}, {{plant, day, cnt + 2}})
in
Table.Combine({startRow, middle, endRow})
),
Result = Table.Combine(AddedRows[Final])
in
Result
What This Produces
ID
Day #
Day Seq
456
2
1 ← Plant (Start)
B
2
2
C
2
3
D
2
4
E
2
5
F
2
6
G
2
7
H
2
8
I
2
9
J
2
10
456
2
11 ← Plant (End)
Check out the May 2026 Power BI update to learn about new features.
Sign up to receive a private message when registration opens and key events begin.
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.