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

A 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.

Reply
unknown917
Helper IV
Helper IV

Add rows with data from another table??

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?

 

IDDay #Day Seq
B21
C22
D23
E24
F25
G26
H27
I28
J29

 

IDNumberPlant
B12456
C12456
D12456
E12456
F12456
G12456
H12456
I12456
J12456

 

Intended result:

IDDay #Day Seq
45621
B22
C23
D24
E25
F26
G27
H28
I29
J210
456211
4 REPLIES 4
SundarRaj
Super User
Super User

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,

Sundar Rajagopalan

@SundarRaj I'm getting a circular reference error.   Any ideas on what might be causing this?

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,

Sundar Rajagopalan
oussamahaimoud
Continued Contributor
Continued Contributor

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)

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.