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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
MrFlox
Regular Visitor

How to calculate conditional cumulative figures

Hello everyone, 

I am relatively new to powerbi (not entirely new to programming itself) and have come upon a challenge. I need to recreate the below variable Z on BI. The whole difficulty lies in creating a cumulative figure (Y) that depends on whether the "X" went from zero to a non-zero. The explanation is simple:

Z is calculated as X - cumulative Y. However, if X went to 0 in the previous date, and now is at non-zero, then the cumulative process of Y starts again at this precise date. You can see it in the highlighted rows which signal the refresh of the cumulative process.

The Date Rank Variable is simply the ranking of the leftside dates (it alligns to the left always so it looks stuck to the dates, don't get confused).

 

DateDate RankXYZ = (X - cumulative Y) OR X-Y if refreshed
01/09/201813056-26
30/09/20182404-20
31/10/20183064-124
30/11/201841248
31/12/2018536626
31/01/201965764-17
28/02/2019786012
31/03/201980574-648
30/04/2019907-655
31/05/20191015-4
30/06/20191142-3
31/07/201912801
31/08/201913745-45
30/09/201914047-99
31/10/2019157652763
30/11/2019165677558
31/12/20191747434
31/01/20201880-5
29/02/2020194484-493
31/03/20202000-497
30/04/20202105-502
31/05/20202208-510
30/06/20202336-3
31/07/202024464-66
31/08/20202526-74
30/09/20202608-84

 

!!I have an extra complexity that I should have mentioned, the table above is for a single ID number. In reality there are multiple ID numbers in that data, thus the above calculations should work for each ID number separatelly. The ID number is like an extra column.


I have spent hours on it...can anyone help with the function needed to calculate Y?

Many thanks!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @MrFlox ,

 

Please try :

 

Flag(X<>0) Column = 
var _last0=CALCULATE(MAX('Table'[Date Rank]),FILTER('Table','Table'[Date Rank]<EARLIER('Table'[Date Rank]) && 'Table'[X]=0 && 'Table'[ID]=EARLIER('Table'[ID])))+0
return 
IF('Table'[X]=0,BLANK(),_last0)
FillFlag Column = MAXX(FILTER('Table','Table'[Date Rank]<=EARLIER('Table'[Date Rank]) && 'Table'[ID]=EARLIER('Table'[ID])),[Flag(X<>0) Column])
Z Column = [X]- CALCULATE(SUM('Table'[Y]),FILTER('Table','Table'[Date Rank]<=EARLIER('Table'[Date Rank]) && [FillFlag Column]=EARLIER('Table'[FillFlag Column]) &&'Table'[ID]=EARLIER('Table'[ID]) ) )

 

The final output:

Column.PNG

 

If you just want to extract some records, you could apply some filters. 


Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

Hi @MrFlox ,

 

Please try :

 

Flag(X<>0) Column = 
var _last0=CALCULATE(MAX('Table'[Date Rank]),FILTER('Table','Table'[Date Rank]<EARLIER('Table'[Date Rank]) && 'Table'[X]=0 && 'Table'[ID]=EARLIER('Table'[ID])))+0
return 
IF('Table'[X]=0,BLANK(),_last0)
FillFlag Column = MAXX(FILTER('Table','Table'[Date Rank]<=EARLIER('Table'[Date Rank]) && 'Table'[ID]=EARLIER('Table'[ID])),[Flag(X<>0) Column])
Z Column = [X]- CALCULATE(SUM('Table'[Y]),FILTER('Table','Table'[Date Rank]<=EARLIER('Table'[Date Rank]) && [FillFlag Column]=EARLIER('Table'[FillFlag Column]) &&'Table'[ID]=EARLIER('Table'[ID]) ) )

 

The final output:

Column.PNG

 

If you just want to extract some records, you could apply some filters. 


Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi @MrFlox ,

 

Please add  the following formual to each of the measure I created before:

&& 'Table'[ID]=MAX('Table'[ID])

edited.PNG

The final output is shown below:

output.jpg

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you Eyelyn for the solution! I am very close. I input the formulas and get the same results when compiling the table in the report view.

 

However, because I will be extracting the data (and the data is millions of rows long), I need the extracted to be in my Data View, as columns: i.e Date, Date Rank, ID, X, Y, Flag(X<>0), FillFlag, Z

 

I have tried to employ all the formulas as columns, but failed. I believe some modifications is needed. Could you advise?

 

Thank you in any case!

Anonymous
Not applicable

Hi @MrFlox ,

 

Please follow these steps:

1. Find the latest [Date Rank] value with X=0 for all records where X<> 0:

Flag(X<>0) =
VAR _last0 =
    CALCULATE (
        MAX ( 'Table'[Date Rank] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Date Rank] < MAX ( 'Table'[Date Rank] )
                && 'Table'[X] = 0
        )
    ) + 0
RETURN
    IF ( MAX ( 'Table'[X] ) = 0, BLANK (), _last0 )

2.Fill in the [Flag(X<>0)] value of the previous row where it is blank:

FillFlag =
MAXX (
    FILTER ( ALL ( 'Table' ), 'Table'[Date Rank] <= MAX ( 'Table'[Date Rank] ) ),
    [Flag(X<>0)]
)

3.Now use the following formula to sum of [Y] based on the [Date] and the same [FillFlag] 

Z =
MAX ( 'Table'[X] )
    - CALCULATE (
        SUM ( 'Table'[Y] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Date Rank] <= MAX ( 'Table'[Date Rank] )
                && [FillFlag] = MAXX ( 'Table', [FillFlag] )
        )
    )

The final output is shown below:

Z=X-Y.PNG

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you Eyelyn9, I have an extra complexity that I should have mentioned, the table above is for a single ID number. In reality there are multiple ID numbers in that data, thus the above calculations should work for each ID number separatelly. The ID number is like an extra column.

How can I apply your formulas for a data with multiple ID numbers?

 

Also, why do you put the '+ 0' part after Calculate, in the 1st step? 


Many thanks, and sorry for the inconvenience!

Jihwan_Kim
Super User
Super User

Hi, @MrFlox 

 

Please check the below picture and the sample pbix file's link down below.

All measures are in the sample pbix file.

I believe you can easily follow the steps.

 

Picture4.png

 

https://www.dropbox.com/s/j4mq93jqzwlgdek/flox.pbix?dl=0 

 

 

Hi, My name is Jihwan Kim.

If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.

Linkedin: https://www.linkedin.com/in/jihwankim1975/


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
amitchandak
Super User
Super User

@MrFlox , Try a new measure like

calculate(sum(Table[X]) -Sum(Table[Y]), filter(allselected(Table), Table[Date] <=Max(Table[Date])))

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

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors