A column made in one step is not there yet for the rest of that same step. Every value in a step is worked out from the table as it arrives, so the second one cannot see the first.
Error:
!
illegal: `[margin]` is made by this same `add`, so it is not on the table yet. Every value in one step is worked out from the table as it arrives. Make it in a step of its own: `then add [margin] as ... then add ...`
|
2 | then add [margin] as ([revenue] - [cost]), [doubled] as ([margin] * 2)
| ^^^^^^
illegal: `[margin]` is made by this same `add`, so it is not on the table yet. Every value in one step is worked out from the table as it arrives. Make it in a step of its own: `then add [margin] as ... then add ...`
|
2 | then add [margin] as ([revenue] - [cost]), [doubled] as ([margin] * 2)
| ^^^^^^
Give it a step of its own and it works, and from there on the new column is an ordinary column that any later step can use.
This is worth knowing early if you are arriving from dplyr or pandas, because mutate and assign both let the second column read the first. This grammar does not, for the same reason SQL does not: a step is one step, rather than a sequence hiding inside one. Replacing a column is a different thing and works as you would expect, because the old value is on the table when the step begins.