Forum Discussion
List.Generate to calculate column based on the previous row value of the Same Column
Hi DSR , Could you try this
let
// Example table
Source = Table.FromRows(
{
{"A", 0, "Yr00", 0, 2, 0, 0},
{"A", 1, "Yr01", 0, 2, 1, 1},
{"A", 2, "Yr02", 1, 2, 2, 1},
{"A", 3, "Yr03", 1, 2, 3, 1},
{"A", 4, "Yr04", 1, 2, 4, 0},
{"A", 5, "Yr05", 0, 2, 5, 1},
{"A", 6, "Yr06", 1, 2, 6, 0},
{"A", 7, "Yr07", 0, 2, 7, 0},
{"A", 8, "Yr08", 0, 2, 8, 0},
{"A", 9, "Yr09", 0, 2, 9, 0},
{"A", 10, "Yr10", 0, 2, 10, 0}
},
{"Case.CC.EEName", "PeriodNo", "Yr", "NoEE.inPrvPrd", "Yr Move Out", "Index.inGrp", "NoEE"}
),
// Generate calculated column
AddedColumn = Table.AddColumn(Source, "CalculatedColumn", each null),
ListValues = List.Generate(
() => [Index = 0, PreviousValue = 0], // Start condition
each [Index] < Table.RowCount(Source), // Continue while there are rows
each [
Index = [Index] + 1,
PreviousValue = if Table.Column(Source, "Index.inGrp"){[Index]} = 0 then
0
else if Table.Column(Source, "NoEE"){[Index]} = 0 or Table.Column(Source, "NoEE"){[Index]} = null then
null
else if [PreviousValue] = Table.Column(Source, "Yr Move Out"){[Index]} then
Table.Column(Source, "NoEE"){[Index]}
else
[PreviousValue] + 1
],
each [PreviousValue]
),
// Add ListValues back to the table
Result = Table.FromColumns(Table.ToColumns(Source) & {ListValues}, Table.ColumnNames(Source) & {"CalculatedColumn"})
in
Result
In the above
- Recursive Calculation: List.Generate tracks the previous row's value and uses it for the next row.
- Conditions: Checks are implemented for Index.inGrp , NoEE , and Yr Move Out values as specified.
- Merge Back: The calculated list is combined back into the table.
If this post helped please do give a kudos and accept this as a solution
Thanks In Advance
Hello Akash, Thank you for your work. I have applied your codes. Coply below. CalculatedColumn did not answer correctly when [Index.inGrp] = 1,2,3,4,5
Case.CC.EENamePeriodNoYrNoEE.inPrvPrdYr Move OutIndex.inGrpNoEECalculatedColumn
| A | 0 | Yr00 | 0 | 2 | 0 | 0 | 0 |
| A | 1 | Yr01 | 0 | 2 | 1 | 1 | 0 |
| A | 2 | Yr02 | 1 | 2 | 2 | 1 | 1 |
| A | 3 | Yr03 | 1 | 2 | 3 | 1 | 2 |
| A | 4 | Yr04 | 1 | 2 | 4 | 0 | 1 |
| A | 5 | Yr05 | 0 | 2 | 5 | 1 | null |
| A | 6 | Yr06 | 1 | 2 | 6 | 0 | null |
| A | 7 | Yr07 | 0 | 2 | 7 | 0 | null |
| A | 8 | Yr08 | 0 | 2 | 8 | 0 | null |
| A | 9 | Yr09 | 0 | 2 | 9 | 0 | null |
| A | 10 | Yr10 | 0 | 2 | 10 | 0 | null |
- DSR1 year ago
Resolver I
Hello Akash,
If I modify the conditions to become:
if Table.Column(ChangedType, "Index.inGrp"){[Index]} = 0 then null
else if Table.Column(ChangedType, "NoEE"){[Index]} = 0 or Table.Column(ChangedType, "NoEE"){[Index]} = null then null
else if [PreviousValue] = 0 or [PreviousValue] = null then 1
else if [PreviousValue] = Table.Column(ChangedType, "Yr Move Out"){[Index]} then Table.Column(ChangedType, "NoEE"){[Index]}
else [PreviousValue] + 1 ],
=======It resulted in correct numbers but, not aligned, shifted one row down.
Yr Move OutIndex.inGrpNoEEAnswershallbeCalculatedColumn
2 0 0 null 0 2 1 1 1 null 2 2 1 2 1 2 3 1 1 2 2 4 0 null 1 2 5 1 1 null 2 6 0 null 1 2 7 0 null null 2 8 0 null null 2 9 0 null null 2 10 0 null null