import%20marimo%0A%0A__generated_with%20%3D%20%220.14.16%22%0Aapp%20%3D%20marimo.App(%0A%20%20%20%20width%3D%22medium%22%2C%0A%20%20%20%20app_title%3D%22Percentage%20of%20Total%20Pattern%22%2C%0A%20%20%20%20css_file%3D%22marimo_custom.css%22%2C%0A)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20text_cell(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%23%20Percentage%20of%20Total%0A%0A%20%20%20%20Calculate%20percentage%20contributions%20using%20window%20functions.%0A%0A%20%20%20%20%23%23%20Overview%0A%0A%20%20%20%20Percentage%20of%20total%20is%20a%20common%20analytical%20pattern%20that%20helps%20you%20understand%0A%20%20%20%20the%20relative%20contribution%20of%20each%20group%20to%20the%20overall%20total.%20In%20BSL%2C%20this%20is%0A%20%20%20%20achieved%20using%20the%20%60.all()%60%20method%20which%20computes%20an%20aggregate%20over%20all%20rows%2C%0A%20%20%20%20ignoring%20any%20grouping.%0A%0A%20%20%20%20This%20pattern%20is%20essential%20for%3A%0A%20%20%20%20-%20**Market%20share%20analysis**%20-%20Understanding%20each%20carrier's%20portion%20of%20total%20flights%0A%20%20%20%20-%20**Revenue%20contribution**%20-%20Calculating%20what%20%25%20of%20total%20revenue%20each%20product%20generates%0A%20%20%20%20-%20**Performance%20benchmarking**%20-%20Comparing%20segments%20against%20the%20total%20population%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20text_cell()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20ibis%0A%20%20%20%20from%20ibis%20import%20_%0A%20%20%20%20from%20boring_semantic_layer%20import%20to_semantic_table%0A%20%20%20%20import%20textwrap%0A%0A%20%20%20%20BASE_URL%20%3D%20%22https%3A%2F%2Fpub-a45a6a332b4646f2a6f44775695c64df.r2.dev%22%0A%0A%20%20%20%20%23%20Define%20code%20as%20strings%20for%20accordion%0A%20%20%20%20imports_code%20%3D%20textwrap.dedent(%22%22%22%5C%0A%20%20%20%20import%20ibis%0A%20%20%20%20from%20ibis%20import%20_%0A%20%20%20%20from%20boring_semantic_layer%20import%20to_semantic_table%0A%20%20%20%20%22%22%22)%0A%0A%20%20%20%20data_code%20%3D%20textwrap.dedent(f%22%22%22%5C%0A%20%20%20%20BASE_URL%20%3D%20%22%7BBASE_URL%7D%22%0A%20%20%20%20con%20%3D%20ibis.duckdb.connect(%22%3Amemory%3A%22)%0A%0A%20%20%20%20%23%20Load%20flights%20and%20carriers%20data%0A%20%20%20%20flights_tbl%20%3D%20con.read_parquet(f%22%7B%7BBASE_URL%7D%7D%2Fflights.parquet%22)%0A%20%20%20%20carriers_tbl%20%3D%20con.read_parquet(f%22%7B%7BBASE_URL%7D%7D%2Fcarriers.parquet%22)%0A%0A%20%20%20%20%23%20Join%20flights%20with%20carrier%20nicknames%0A%20%20%20%20flights_with_carriers%20%3D%20flights_tbl.join(%0A%20%20%20%20%20%20%20%20carriers_tbl%2C%0A%20%20%20%20%20%20%20%20flights_tbl.carrier%20%3D%3D%20carriers_tbl.code%2C%0A%20%20%20%20%20%20%20%20how%3D%22inner%22%2C%0A%20%20%20%20)%0A%20%20%20%20%22%22%22)%0A%0A%20%20%20%20semantic_code%20%3D%20textwrap.dedent(%22%22%22%5C%0A%20%20%20%20flights%20%3D%20(%0A%20%20%20%20%20%20%20%20to_semantic_table(flights_with_carriers%2C%20name%3D%22flights%22)%0A%20%20%20%20%20%20%20%20.with_measures(%0A%20%20%20%20%20%20%20%20%20%20%20%20flight_count%3Dlambda%20t%3A%20t.count()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20total_distance%3Dlambda%20t%3A%20t.distance.sum()%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.with_measures(%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Use%20.all()%20to%20compute%20percentage%20of%20total%0A%20%20%20%20%20%20%20%20%20%20%20%20market_share%3Dlambda%20t%3A%20t.flight_count%20%2F%20t.all(t.flight_count)%20*%20100%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20distance_share%3Dlambda%20t%3A%20t.total_distance%20%2F%20t.all(t.total_distance)%20*%20100%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20%22%22%22)%0A%0A%20%20%20%20%23%20Execute%20setup%0A%20%20%20%20con%20%3D%20ibis.duckdb.connect(%22%3Amemory%3A%22)%0A%20%20%20%20flights_tbl%20%3D%20con.read_parquet(f%22%7BBASE_URL%7D%2Fflights.parquet%22)%0A%20%20%20%20carriers_tbl%20%3D%20con.read_parquet(f%22%7BBASE_URL%7D%2Fcarriers.parquet%22)%0A%0A%20%20%20%20flights_with_carriers%20%3D%20flights_tbl.join(%0A%20%20%20%20%20%20%20%20carriers_tbl%2C%0A%20%20%20%20%20%20%20%20flights_tbl.carrier%20%3D%3D%20carriers_tbl.code%2C%0A%20%20%20%20%20%20%20%20how%3D%22inner%22%2C%0A%20%20%20%20)%0A%0A%20%20%20%20flights%20%3D%20(%0A%20%20%20%20%20%20%20%20to_semantic_table(flights_with_carriers%2C%20name%3D%22flights%22)%0A%20%20%20%20%20%20%20%20.with_measures(%0A%20%20%20%20%20%20%20%20%20%20%20%20flight_count%3Dlambda%20t%3A%20t.count()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20total_distance%3Dlambda%20t%3A%20t.distance.sum()%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.with_measures(%0A%20%20%20%20%20%20%20%20%20%20%20%20market_share%3Dlambda%20t%3A%20t.flight_count%20%2F%20t.all(t.flight_count)%20*%20100%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20distance_share%3Dlambda%20t%3A%20t.total_distance%20%2F%20t.all(t.total_distance)%20*%20100%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Display%20accordion%20with%20setup%20code%0A%20%20%20%20_title%20%3D%20mo.md(%22%23%23%20Define%20Semantic%20Model%22)%0A%20%20%20%20_desc%20%3D%20mo.md(%22We%20create%20a%20semantic%20table%20with%20percentage%20measures%20using%20the%20%60.all()%60%20method%3A%22)%0A%20%20%20%20_accordion%20%3D%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%220%EF%B8%8F%E2%83%A3%20Imports%22%3A%20mo.md(f%22%60%60%60python%5Cn%7Bimports_code%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%221%EF%B8%8F%E2%83%A3%20Load%20%26%20join%20data%22%3A%20mo.md(f%22%60%60%60python%5Cn%7Bdata_code%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%222%EF%B8%8F%E2%83%A3%20Define%20semantic%20table%22%3A%20mo.md(f%22%60%60%60python%5Cn%7Bsemantic_code%7D%5Cn%60%60%60%22)%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%20%20%20%20mo.vstack(%5B_title%2C%20_desc%2C%20_accordion%5D)%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell%0Adef%20text_cell(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%23%23%20Market%20Share%20by%20Carrier%0A%0A%20%20%20%20Query%20the%20top%2010%20carriers%20by%20market%20share%3A%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20q1%20%3D%20%22%22%22%0A%20%20%20%20flights.group_by(%22nickname%22)%0A%20%20%20%20.aggregate(%22flight_count%22%2C%20%22market_share%22)%0A%20%20%20%20.order_by(_.market_share.desc())%0A%20%20%20%20.limit(10)%0A%20%20%20%20%22%22%22%0A%0A%20%20%20%20q1_e%20%3D%20eval(%22(%22%20%2B%20q1%20%2B%20%22)%22)%0A%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%60%60%60python%5Cn%7Bq1%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%20%20%20%20mo.ui.tabs(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%F0%9F%93%8A%20Table%22%3A%20mo.ui.table(q1_e.execute())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%F0%9F%93%88%20Chart%22%3A%20q1_e.chart(backend%3D%22altair%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%F0%9F%92%BB%20SQL%22%3A%20mo.md(f%22%60%60%60sql%5Cn%7Bq1_e.sql()%7D%5Cn%60%60%60%22)%0A%20%20%20%20%20%20%20%20%7D)%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
d9734dcd24bfaabfdaf4ced5235e123c2f3851a2c93bf3171d26432b0f7e0dff