Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more
Hey
So I have data that looks like this
CustID. Transaction Amount
123WDX. $345
456FGT. $576
123WDX. $200
I'm trying to add a column which adds the value of the previous transaction on the same row as the last, without any date parameters.
So in this instance, I'd like
CustID. Transaction Amount Previous
123WDX. $345. Unknown
456FGT. $576. Unknown
123WDX. $200. $345
Appreciate it's a tricky one but would love to see if anyone has any views. Many thanks
Solved! Go to Solution.
Hey @Daretoexplore ,
To create a "Previous Transaction" column based on CustID, you can use Power Query (M language) or DAX depending on where you're building this (e.g., Excel, Power BI, etc.). Below are both approaches.
Power Query doesn’t use row numbers directly, but we can group and sort each customer’s transactions, then add an index to track the order.
Load your data into Power Query.
Sort by CustID and a temporary index (if no date exists).
Group by CustID.
Inside each group:
Sort by the temporary index.
Add an Index column starting at 0.
Add another column that’s the Transaction Amount shifted by 1 row.
Expand the tables and rename the new column as "Previous".
let
Source = YourTable,
Sorted = Table.Sort(Source, {"CustID", Order.Ascending}),
Grouped = Table.Group(Sorted, {"CustID"}, {
{"AllData", each
let
AddedIndex = Table.AddIndexColumn(_, "Index", 0, 1, Int64.Type),
Shifted = Table.AddColumn(AddedIndex, "Previous", each try AddedIndex[Transaction Amount]{[Index]-1} otherwise null)
in
Shifted
, type table}
}),
Expanded = Table.Combine(Grouped[AllData])
in
ExpandedIf you're using DAX in a calculated column:
PreviousTransaction =
VAR CurrentCust = 'Table'[CustID]
VAR CurrentIndex = 'Table'[Index] -- Add an index if needed
RETURN
CALCULATE(
MAX('Table'[Transaction Amount]),
FILTER(
'Table',
'Table'[CustID] = CurrentCust &&
'Table'[Index] = CurrentIndex - 1
)
)If you don’t have an index or timestamp, create an index using Power Query before this.
If you found this solution helpful, please consider accepting it and giving it a kudos (Like) it’s greatly appreciated and helps others find the solution more easily.
Best Regards,
Nasif Azam
Hi @Daretoexplore,
Just wanted to check if you had the opportunity to review the suggestion provided by @Nasif_Azam.
If the response has addressed your query, please Accept it as a solution and give a 'Kudos' so other members can easily find it.
Thank you.
Tejaswi.
Hi @Daretoexplore,
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please Accept it as a solution so that other community members can find it easily.
Thank you.
Tejaswi.
Hi @Daretoexplore,
I hope the information provided has been useful. Please let me know if you need further clarification or would like to continue the discussion.
If your question has been answered, please “Accept as Solution” and Give “Kudos” so others with similar issues can easily find the resolution.
Thank you.
Tejaswi.
Hey @Daretoexplore ,
To create a "Previous Transaction" column based on CustID, you can use Power Query (M language) or DAX depending on where you're building this (e.g., Excel, Power BI, etc.). Below are both approaches.
Power Query doesn’t use row numbers directly, but we can group and sort each customer’s transactions, then add an index to track the order.
Load your data into Power Query.
Sort by CustID and a temporary index (if no date exists).
Group by CustID.
Inside each group:
Sort by the temporary index.
Add an Index column starting at 0.
Add another column that’s the Transaction Amount shifted by 1 row.
Expand the tables and rename the new column as "Previous".
let
Source = YourTable,
Sorted = Table.Sort(Source, {"CustID", Order.Ascending}),
Grouped = Table.Group(Sorted, {"CustID"}, {
{"AllData", each
let
AddedIndex = Table.AddIndexColumn(_, "Index", 0, 1, Int64.Type),
Shifted = Table.AddColumn(AddedIndex, "Previous", each try AddedIndex[Transaction Amount]{[Index]-1} otherwise null)
in
Shifted
, type table}
}),
Expanded = Table.Combine(Grouped[AllData])
in
ExpandedIf you're using DAX in a calculated column:
PreviousTransaction =
VAR CurrentCust = 'Table'[CustID]
VAR CurrentIndex = 'Table'[Index] -- Add an index if needed
RETURN
CALCULATE(
MAX('Table'[Transaction Amount]),
FILTER(
'Table',
'Table'[CustID] = CurrentCust &&
'Table'[Index] = CurrentIndex - 1
)
)If you don’t have an index or timestamp, create an index using Power Query before this.
If you found this solution helpful, please consider accepting it and giving it a kudos (Like) it’s greatly appreciated and helps others find the solution more easily.
Best Regards,
Nasif Azam
Hi,
You may want to try using the OFFSET function in DAX which allows you to move up or down in data.
Link to details: OFFSET function (DAX) - DAX | Microsoft Learn
The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!
| User | Count |
|---|---|
| 38 | |
| 36 | |
| 33 | |
| 32 | |
| 29 |
| User | Count |
|---|---|
| 129 | |
| 88 | |
| 79 | |
| 68 | |
| 63 |