Error: Binder Error: Referenced column "Position" not found in FROM clause!

I try to create a plot using duckdb wasm.
The select looks like this - and it works in DBeaver:

WITH ranked_data AS (
    SELECT
        s.DeviceTime,
        s.Distance,
        a.TPSCode,
        RANK() OVER (PARTITION BY s.DeviceTime ORDER BY s.Distance DESC) AS Position
    FROM
        simpos s
    JOIN
        athlete a ON a.TPSCode = s.TPSCode
)
SELECT DeviceTime, Distance, TPSCode, Position FROM ranked_data
ORDER BY DeviceTime, Distance DESC;

But in the code i get this error:

async_bindings.ts:150 Uncaught (in promise) Error: Binder Error: Referenced column “Position” not found in FROM clause!
Candidate bindings: “source.Id”
LINE 1: …ESCRIBE SELECT “DeviceTime” AS “col0”, “Position” AS “col1”, “TPSCode” AS "col…

How can i do subqueries or functions ?

Subqueries work — I’ve tried a simpler example with penguins with success. I’m sorry I don’t have an idea why this one fails in particular. Would it be possible to reproduce the issue with a public dataset?

---
sql:
  penguins: https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv
---

```sql
WITH ranked_data AS (
  SELECT *, RANK() OVER (PARTITION BY sex ORDER BY island DESC) AS Position FROM penguins
)
SELECT Position, sex, species, island FROM ranked_data
```