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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

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
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.