Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
VilmarSch
Post Partisan
Post Partisan

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 4
Anonymous
Not applicable

Hi @VilmarSch ,

Based on my testing, please try the following methods:

1.Create the simple table.

vjiewumsft_0-1714353794357.png

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.

vjiewumsft_1-1714353840195.png

 

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.

@Anonymousthanks for the answer, but it didn't give the expected result. I believe it is something more complex.


Sem título.png

Anonymous
Not applicable

Hi @VilmarSch ,

May be you can try to use power query.

Solved: Do Not Consider If Accumulated Value Is Negative P... - Microsoft Fabric Community

 

Best Regardsd,

Wisdom Wu

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

@AnonymousI saw this topic, but I really needed to do it in DAX

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.