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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
Anonymous
Not applicable

Cumulative sum - but only most recent one if duplicate is present

Hi guys,

I have a problem displaying data from my log file. It contains an ID, the date it got changed and a number in- / decreasing. The table looks like the following:

 IDCreatedChanged_To
 11.1.2020     5
 21.1.20202
 32.1.20203
 23.1.20200
 13.1.202010
 14.1.20201
 45.1.20202

 

My goal is to display this loghistory in a visual with dates/months as axis and sum those changes. The problem is that I need to sum those changes for each date and if the same ID has a new value it should take the most current one. Same should apply when I change the filter of the visual to monthly (if it is possible).

I.e for the dates displayed the corresponding values for the visual would be:

1.1.2020    2.1.2020    3.1.2020    4.1.2020    5.1.2020     
5+25+2+30+3+100+3+10+3+1+2

 

I tried it with many summarize, group by cumulative sums, but I cant get a measure working that "deletes/overrides" the old value. I can only get the maxdate of all of them or calculate latest contribution but I dont know how to sum/del those in my visual.

I was so desperate I did it in python. If someone is able to translate this into dax/m or give me a hint what to do I would higly appriciate that.

 

import pandas as pd
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 
import datetime as dt

result = [] 
for currentdate in pd.date_range("2022-01-01", periods=365, freq="D")
    
    allUntilCurrentDay = dataset.loc[dataset['Created']<=currentdate]
    
    filteredValues = allUntilCurrentDay.sort_values('Created').drop_duplicates('ID', keep='last')

    sumOfChanged_To = filteredValues['Changed_To'].sum()

    if(currentdate<=pd.to_datetime('today')):
        result.append([currentdate,sumOfChanged_To])
    else:
        result.append([currentdate,0])

result_df = pd.DataFrame(data=result,columns=['Date','Sum_of_changes'])

 

4 REPLIES 4
amitchandak
Super User
Super User

@Anonymous , Try a measure like


calculate( sumx( Values(Table[ID), calculate(lastnonblankvalue(Table[Created]), Sum(Table[Changed_To]))), filter(allselected(Table), Table[ID] = max(Table[ID]) && Table[Created] <= max(Table[Created])))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

@amitchandak 
Ok I tried your approach again and with a little tweak it works perfect! 

Measure = CALCULATE(
               SUMX(
                        VALUES('Table'[ID]),
                        CALCULATE(
                            LASTNONBLANKVALUE('Table'[Created],
                            SUM('Table'[Changed_to])
                            )
                        )
                    ),
               FILTER(ALLSELECTED('Table'),
//seemed to be not neccessary, ID is actually a String and max() doesnt work, is it ambigous? 
//*hopefully :D
//                         'Table'[ID] < max('Table'[ID]) && 

                           'Table'[Created] <= max('Table'[Created])))


Only problem is, that I'm missing days where no entry was made. Could you tell me where to put my calendar to get the sum of each of the days?

Although I have those connections in my model:
- Active: DimTable[ID] which is connected to the Table[ID] and DimTable[Date] to Calendar[Date]
- Inactive: connection between Table[Created] to Calendar[Date]

 

Sadly if I try to change the Relationship the measure still tries to use the first active one. My attempt was to add USERELATIONSHIP after the last FILTER like

...
FILTER(
  ALLSELECTED('Table'),
                     'Table'[Created] <= max('Table'[Created])),
USERELATIONSHIP(Calendar[Date],'Table'[Created])
)
// or to get each day
...
FILTER(
  ALLSELECTED('Table'),
                     'Table'[Created] <= max('Calendar'[Date])),
USERELATIONSHIP(Calendar[Date],'Table'[Created])
)

But the sums are not correct anymore and the date is the corresponding date from DimTable[Date]

Tyvm in advance!

Anonymous
Not applicable

Thank you for your fast reply! 

 

I might missed mentioning a few things, I'm sorry. 
It is actually possible dates are not present in the Table but I still need the sum of this missing date for the visual

  • i.e. if we remove the 2.1.2020 from the table the visual should display the 2.1.2020 with a value of 5+2=7

I have an active Relationship between [Created] and a Calender[Date]. Can I use the Calender for determining each day and sum ?

 

Expected Output(python) - Current Output(dax)

bfmalefiz_0-1646985773693.png

 

 

Anonymous
Not applicable

CALCULATE(

               SUMX(

                        Table,

                        Table[Changed_To]),

               FILTER(ALL(Calendar),

                           Calendar[Date] <= max(Table[Created])))

 

This measure should do the trick for all dates right? Sadly I dont know how to implement it in your version to exclude those old duplicates 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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