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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
VilmarSch
Post Patron
Post Patron

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
v-jiewu-msft
Community Support
Community Support

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.

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


Sem título.png

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.

@v-jiewu-msftI saw this topic, but I really needed to do it in DAX

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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