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

Cumulative sum by category, date and with a reset filter?

Greetings everyone!

I have a table with those features

 

Machine Production Hrs Fault Date
A801/1/20
A502/1/20
A3010/1/20
B4015/1/20
A0118/1/20
B8023/1/20
A403/2/20
B806/2/20
B0110/2/2020

 

And i would a new cumulative column which sum the hours of production by date and category, and  reset when Fault column is 1, as follows:

 

Machine Production Hrs Fault Date Cumulative Hrs
A801/1/208
A502/1/2013
A3010/1/2016
B4015/1/204
A0118/1/2016
B8023/1/2012
A403/2/204
B806/2/2020
B0110/2/202020

 

I found how to cumulate by date and categories, but not the case of reset with a value, I found only with period!

Any ideas?

1 REPLY 1
d_gosbell
Super User
Super User

So you could create a calculated column with an expression like the following

 

Cummulative Hrs = 
// get the date for the current row
VAR maxDate = 'Table'[Date]
// get the minimum date for the table
VAR minDate = MINX(ALL('Table'[Date]), 'Table'[Date])
// get the current machine
VAR machine = 'Table'[Machine]
// find the previous fault date
VAR minFaultDate = CALCULATE(MAX('Table'[Date]),ALL('Table'), 'Table'[Date] < maxDate , 'Table'[Fault] = 1, 'Table'[Machine] = machine )
// either use the previous fault date or the min date in the table as the starting point for the cummulative sum
var fromDate = COALESCE(minFaultDate, minDate)
// sum the hours between the fromDate and maxDate for the current machine
RETURN CALCULATE(sum('Table'[Production Hrs]), ALL('Table'), 'Table'[Date] >= fromDate, 'Table'[Date] <= maxDate, 'Table'[Machine] = machine)

 

Note that this calc column makes use of CALCULATE and I usually try and avoid that in calc columns as it causes a context transition and can cause issues if you try and create more than one calculated column that uses CALCULATE.

 

So you could do a similar calculation as a measure (which is calculated at run time and not stored like a column)

 

Cummulative Hrs (Measure) = 
// get the date for the current row
VAR maxDate = MAX('Table'[Date])
// get the minimum date for the table
VAR minDate = CALCULATE(MIN('Table'[Date]), all('Table'[Date]))
// get the current machine
VAR machine = SELECTEDVALUE('Table'[Machine])
// find the previous fault date
VAR minFaultDate = CALCULATE(MAX('Table'[Date]),ALL('Table'), 'Table'[Date] < maxDate , 'Table'[Fault] = 1, 'Table'[Machine] = machine )
// either use the previous fault date or the min date in the table as the starting point for the cummulative sum
var fromDate = COALESCE(minFaultDate, minDate)
// sum the hours between the fromDate and maxDate for the current machine
RETURN CALCULATE(sum('Table'[Production Hrs]), ALL('Table'), 'Table'[Date] >= fromDate, 'Table'[Date] <= maxDate, 'Table'[Machine] = machine)

 

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.