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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
joeneedstoknow2
New Member

Add a column to an Embedded Table, as a transformation. Lookup Previous Row.

I have a Customer Fact Table in power query, showing each customer who placed an order.

There is a column in the customer fact table, called dbo _ Orders. dbo _ Orders is an embedded table, showing the order information for each customer. The Orders table has an Index column, and an ReadyDate column.

 

I need to write a function that Adds a column into the embedded table, for each Customer in the fact table. The goal of the function is to add a column that looks up the value of Date in the previous row. It must calculate in the embedded Orders table.

 

Can somone please help me write the function, and explain how to it works?094903.png

1 ACCEPTED SOLUTION
jgeddes
Super User
Super User

Here is some code that should do what you need.

let
    nested_5493 = 
    #table(
        type table [OrderId=nullable number, OrderDate=nullable date, CustomerID=nullable number, Status=nullable text, Index=nullable number],
        {
            {496977, #date(2025,3,3), 5493, "Shipped", 0},
            {498804, #date(2025,3,10), 5493, "Shipped", 1},
            {514401, #date(2025,5,4), 5493, "Shipped", 2}
        }
    ),
    nested_5626 = 
    #table(
        type table [OrderId=nullable number, OrderDate=nullable date, CustomerID=nullable number, Status=nullable text, Index=nullable number],
        {
            {496988, #date(2025,2,3), 5626, "Shipped", 0},
            {498805, #date(2025,3,12), 5626, "Pending", 1},
            {514402, #date(2025,5,6), 5626, "Shipped", 2}
        }
    ),
    dim_table = 
    #table(
        type table [CustomerID=nullable number, Email=nullable text, dbo_Orders=table],
        {
            {5493, "cust5493@domain.com", nested_5493},
            {5626, "cust5626@domain.com", nested_5626}
        }
    ),
    add_nested_column = 
    Table.TransformColumns(
        dim_table, 
        {
            {"dbo_Orders", each Table.AddColumn(_, "PreviousOrder", (r)=> try [OrderDate]{r[Index] - 1} otherwise null, type date), type table}
        }
    )    
in
    add_nested_column

In the sample table I created...

jgeddes_0-1777650383261.png

I added a column to the nested tables that shows the previous OrderDate.

jgeddes_1-1777650423424.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

2 REPLIES 2
joeneedstoknow2
New Member

Amazing. Thank you

jgeddes
Super User
Super User

Here is some code that should do what you need.

let
    nested_5493 = 
    #table(
        type table [OrderId=nullable number, OrderDate=nullable date, CustomerID=nullable number, Status=nullable text, Index=nullable number],
        {
            {496977, #date(2025,3,3), 5493, "Shipped", 0},
            {498804, #date(2025,3,10), 5493, "Shipped", 1},
            {514401, #date(2025,5,4), 5493, "Shipped", 2}
        }
    ),
    nested_5626 = 
    #table(
        type table [OrderId=nullable number, OrderDate=nullable date, CustomerID=nullable number, Status=nullable text, Index=nullable number],
        {
            {496988, #date(2025,2,3), 5626, "Shipped", 0},
            {498805, #date(2025,3,12), 5626, "Pending", 1},
            {514402, #date(2025,5,6), 5626, "Shipped", 2}
        }
    ),
    dim_table = 
    #table(
        type table [CustomerID=nullable number, Email=nullable text, dbo_Orders=table],
        {
            {5493, "cust5493@domain.com", nested_5493},
            {5626, "cust5626@domain.com", nested_5626}
        }
    ),
    add_nested_column = 
    Table.TransformColumns(
        dim_table, 
        {
            {"dbo_Orders", each Table.AddColumn(_, "PreviousOrder", (r)=> try [OrderDate]{r[Index] - 1} otherwise null, type date), type table}
        }
    )    
in
    add_nested_column

In the sample table I created...

jgeddes_0-1777650383261.png

I added a column to the nested tables that shows the previous OrderDate.

jgeddes_1-1777650423424.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 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.

Top Kudoed Authors