Forum Discussion

helspaul's avatar
helspaul
Frequent Visitor
5 months ago
Solved

Order switching mid workflow

Hi, I have the most frustrating issue. Anyone seen this before? Know how to stop it? Is it a bug?   I have a basic table which records changes over time   I want to add the date from row 2 ...
  • Juan-Power-bi's avatar
    5 months ago

    Hey, this is a really common gotcha in Power Query — row order is never truly guaranteed between steps, so when you expand after the merge, the engine can internally reorder things and your Index no longer lines up correctly.
    The safest fix is to sort explicitly by DefectID + Date right before you add the Index column, and then make sure your key is built on both those columns together, not just the index. Here's what I'd recommend:

    Before adding the Index, add a sort step: sort by DefectID ascending, then Date ascending. You can do this in the UI or in M like this:

    m= Table.Sort(#"Previous Step", {{"DefectID", Order.Ascending}, {"Date", Order.Ascending}})

    Then group by DefectID keeping All Rows, and inside each group add the index — this way the index is scoped per DefectID and the order within each group is controlled.
    When you build your join keys, make sure they combine DefectID + Index so the match is always within the same defect.

    The root cause is that Power Query's query engine doesn't treat intermediate sort steps as "sticky" — it's lazy-evaluated and can re-execute steps in a different physical order. Sorting immediately before the index generation is the only reliable way to guarantee they stay in sync.
    Hope that helps!