0

I want to use python-polars replace Power BI desktop tools. Data model can be a mulicolumns dataset include alll tabular model columns, but How Can I use dynamic measure definition in python-polars. For example: sum('amount') filter ('Country' = 'USA')

I want to define this measure in configuration files.

Hengaini
  • 44
  • 5

1 Answers1

0

(disclaimer: I am not familiar with Power Bi, so I am answering this based on the remainder of your question).

Let's say the data is

import polars as pl
df = pl.DataFrame({"Country": ["USA", "USA", "Japan"],
                        "amount": [100, 200, 400]})
shape: (3, 2)
┌─────────┬────────┐
│ Country ┆ amount │
│ ---     ┆ ---    │
│ str     ┆ i64    │
╞═════════╪════════╡
│ USA     ┆ 100    │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ USA     ┆ 200    │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ Japan   ┆ 400    │
└─────────┴────────┘

You could define your expression in the configuration file as follows:

expr = pl.col('amount').filter(pl.col('Country')=='USA').sum()

and in your code base you could do

from <expression file above> import expr
df.select(expr)
shape: (1, 1)
┌────────┐
│ amount │
│ ---    │
│ i64    │
╞════════╡
│ 300    │
└────────┘

If the configuration file should be plain text (would strongly advise against this, because unsafe, not testable, no tooling support, etc), you could store in the config file

pl.col('amount').filter(pl.col('Country')=='USA').sum()

and read this as expr in your Python code, and use eval:

expr = <read config file as string>
df.select(eval(expr))
jvz
  • 1,183
  • 6
  • 13
  • I will try your suggestion. Can I link select? Such as df.select(eval(expr1)).select(eval(expr2)), because I have so many metrics. – Hengaini Jul 28 '22 at 02:27
  • Yes, that is possible, as `select` will return a new dataframe. – jvz Jul 31 '22 at 20:33