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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
jwin2424
Resolver I
Resolver I

Return value from prior date

Hello all. 

I am trying to get the results below in the "Previous Scheduling Month" via the Power Query Editor. 

I have a single query named 'Backlog Reports'. Each day [Run Date] I append the report. Each [Order #] has numerous lines with different [Scheduling Month] values. I have a column [Order Line ID] that is unique to each row on the [Order #]. I need to create a custom column that returns the [Scheduling Month] value from the previous [Run Date] for that [Order Line ID]. There is one caviate - we run the reports during the week, so the [Run Date] only provides the Mon-Fri work week. Can someone help me achieve the column [Previous Scheduling Month] via a custom column in the query?

 

Run DateScheduling MonthOrder Line IDOrder #Previous Scheduling Month
4/1/2024JUL-2412355JUL-24
4/2/2024JUN-2412355JUL-24
4/3/2024JUN-2412355JUN-24
4/4/2024JUN-2412355JUN-24
4/5/2024JUN-2412355JUN-24
4/8/2024MAY-2412355JUN-24
4/1/2024JUN-2412455JUL-24
4/2/2024JUN-2412455JUL-24
4/3/2024JUN-2412455JUN-24
4/4/2024JUN-2412455JUN-24
4/5/2024JUN-2412455JUN-24
4/8/2024MAY-2412455JUN-24

 

Thanks!

1 ACCEPTED SOLUTION

OK, @jwin2424 . I think I understand your requirements. You want to get previous row group by one or two fields.

Here's a blog about this which really helps. Please refer to

Value from previous row – Power Query, M language – Trainings, consultancy, tutorials (exceltown.com...

The second part from this blog(Value from previous row, without writing of M code) can fix this.

I made a sample for you also, you can download it.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtE31DcyMDJR0lHyCvXRBTMMjYyBpKmpUqwOSIERQoEfVgXGhBSYEFJgSkiBBUyBr2MkVgWGWEwwIeQLE0K+MCHkCxNCvjAh5AuYglgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Run Date" = _t, #"Scheduling Month" = _t, #"Order Line ID" = _t, #"Order #" = _t]),
    #"Grouped Rows" = Table.Group(Source, {"Order Line ID"}, {{"allrows", each _, type table [Run Date=nullable text, Scheduling Month=nullable text, Order Line ID=nullable text, #"Order #"=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([allrows],"index",1,1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"allrows"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Run Date", "Scheduling Month", "Order #", "index"}, {"Run Date", "Scheduling Month", "Order #", "index"}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Expanded Custom", "index", "index - Copy"),
    #"Added to Column" = Table.TransformColumns(#"Duplicated Column", {{"index - Copy", each _ + 1, type number}}),
    #"Merged Queries" = Table.NestedJoin(#"Added to Column", {"index", "Order Line ID"}, #"Added to Column", {"index - Copy", "Order Line ID"}, "Added to Column", JoinKind.LeftOuter),
    #"Expanded Added to Column" = Table.ExpandTableColumn(#"Merged Queries", "Added to Column", {"Scheduling Month"}, {"Scheduling Month.1"})
in
    #"Expanded Added to Column"

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
v-stephen-msft
Community Support
Community Support

Thanks for your concern in this issue, @johnbasha33 .

 

@jwin2424 , I have reviewed your case and key information is needed to give you the best possible support, please send the following information/details at your earliest convenience.
Information Required:

1.Why is your red underline JUL-24, knowing that your [Order Line ID] has changed from 123 to 124?

2.The green line, [Order Line ID] is 124, [Run Date] is 4/2/2024, the previous line is 4/1/2024, and the corresponding [Scheduling Month] is JUN-24, why JUL-24?

vstephenmsft_2-1714547421456.png

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

My appologies. I was just copy/pasting from the previous 123 line IDs. Those would all read JUN-24 if done correctly. 

OK, @jwin2424 . I think I understand your requirements. You want to get previous row group by one or two fields.

Here's a blog about this which really helps. Please refer to

Value from previous row – Power Query, M language – Trainings, consultancy, tutorials (exceltown.com...

The second part from this blog(Value from previous row, without writing of M code) can fix this.

I made a sample for you also, you can download it.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtE31DcyMDJR0lHyCvXRBTMMjYyBpKmpUqwOSIERQoEfVgXGhBSYEFJgSkiBBUyBr2MkVgWGWEwwIeQLE0K+MCHkCxNCvjAh5AuYglgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Run Date" = _t, #"Scheduling Month" = _t, #"Order Line ID" = _t, #"Order #" = _t]),
    #"Grouped Rows" = Table.Group(Source, {"Order Line ID"}, {{"allrows", each _, type table [Run Date=nullable text, Scheduling Month=nullable text, Order Line ID=nullable text, #"Order #"=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([allrows],"index",1,1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"allrows"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Run Date", "Scheduling Month", "Order #", "index"}, {"Run Date", "Scheduling Month", "Order #", "index"}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Expanded Custom", "index", "index - Copy"),
    #"Added to Column" = Table.TransformColumns(#"Duplicated Column", {{"index - Copy", each _ + 1, type number}}),
    #"Merged Queries" = Table.NestedJoin(#"Added to Column", {"index", "Order Line ID"}, #"Added to Column", {"index - Copy", "Order Line ID"}, "Added to Column", JoinKind.LeftOuter),
    #"Expanded Added to Column" = Table.ExpandTableColumn(#"Merged Queries", "Added to Column", {"Scheduling Month"}, {"Scheduling Month.1"})
in
    #"Expanded Added to Column"

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

This is exactly what I needed. Thank you!

johnbasha33
Solution Sage
Solution Sage

@jwin2424 

To achieve the desired result of obtaining the previous scheduling month for each order line ID based on the run date, you can use Power Query's capabilities to add a custom column. Here's how you can do it:

1. Open the Power Query Editor.

2. Select the "Backlog Reports" query.

3. Go to the "Add Column" tab.

4. Click on "Custom Column".

5. In the Custom Column dialog, enter the following formula:

```m
= let
PreviousDate = Date.AddDays([Run Date], -1),
PreviousSchedulingMonth = List.Last(
Table.SelectRows(
#"Added Custom",
each [Order Line ID] = [Order Line ID] and [Run Date] = PreviousDate
)[[Scheduling Month]]
)
in
PreviousSchedulingMonth
```

This formula first calculates the previous date based on the run date using Date.AddDays(). Then, it filters the table to select rows with the same order line ID as the current row and the previous run date. Finally, it extracts the last scheduling month from the filtered rows using List.Last().

6. Click OK to add the custom column.

This should create a new column called "Previous Scheduling Month" with the desired values. Make sure to replace "Added Custom" with the actual name of your previous step in the query if it's different.

Once you've added the custom column, you can close and apply the changes to your query. Now, you should see the "Previous Scheduling Month" column populated with the correct values based on the previous run date for each order line ID.


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

Here is what it returned. 

jwin2424_0-1714577940464.png


That last step in my query 'Custom' is just me clicking on the error to see what it was. 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors