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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! 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
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.