Forum Discussion

VilmarSch's avatar
VilmarSch
Icon for Post Partisan rankPost Partisan
2 years ago

Convert Python script to DAX - Conditional Accumulated

Input:

 

import pandas as pd
data = {
    'Mes': list(range(1, 7)),
    'Entrada': [892.78, 322.32, 557.25, 1034.96, 1641.06, 544.83],
    'Saida': [997.81, 313.25, 734.94, 921.74, 859.57, 1142.17]
}

df = pd.DataFrame(data)

# Calculando o saldo
saldo = 0
saldo_list = []

for entrada, saida in zip(df['Entrada'], df['Saida']):
    saldo = max(0, saldo + entrada - saida)
    saldo_list.append(saldo)

df['Saldo'] = saldo_list

print(df)

 

 

output:

MesEntradaSaidaSaldo
1892.78997.810.00
2322.32313.259.07
3557.25734.940.00
41034.96921.74113.22
51641.06859.57894.71
6544.831142.17297.37

4 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi VilmarSch ,

    Based on my testing, please try the following methods:

    1.Create the simple table.

    2.Create the measure to calculate saldo.

     

    Saldo = 
    VAR sal = 0
    VAR entra = CALCULATE(SUMX('Table', 'Table'[Entrada]))
    VAR sasi = CALCULATE(SUMX('Table', 'Table'[Saida]))
    RETURN
    MAX(0, 0 + entra - sasi)

     

    3.Drag the measure into the table visual, the result is shown below.

     

    Best Regards,

    Wisdom Wu

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.