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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Anonymous
Not applicable

Cumulative substraction over categories

I have a matrix table in PBI and I would like to calculate a kind of cumulative substraction over the categories. The main goal is to achieve the "Expected W-1" field, so in a nutshell: for Category 1, the Expected W-1 calculation calculated in this way 10119 - 7427 = 2692, 

Further information that I am using a simple DAX measure = 
Count = Count(fact_table) (this is required because additonal filtering can be applied in the future. 

CATEGORIESCurrent W-1Expected W-1How it is calculated? 
Category 1 101192692Category 1 - Category 2
Category 274273312Category 2 - Category 3 
Category 341152127Category 3 - Category 4
Category 4 1988109Category 4 - Category 5
Category 5 187997Category 5 - Category 6
Category 6 1782383Category 6 - Category 7
Category 713991399Category 7
Total2870910119 


Is there any way to solve this?

1 ACCEPTED SOLUTION

@Anonymous 

Please try

Count.AllOpportunities.New =
VAR CategoriesAndCounts =
ADDCOLUMNS (
ALLSELECTED ( Category_Table[Category_Order] ),
"@Count",
CALCULATE (
[Count.AllOpportunities],
ALLEXCEPT ( Category_Table, Category_Table[Category_Order] )
)
)
RETURN
SUMX (
VALUES ( Category_Table[Category_Order] ),
VAR CurrentOrder = Category_Table[Category_Order]
VAR CurrentCount = [Count.AllOpportunities]
VAR NextCount =
MAXX (
TOPN (
1,
FILTER ( CategoriesAndCounts, Category_Table[Category_Order] > CurrentOrder ),
Category_Table[Category_Order], ASC
),
[@Count]
)
RETURN
CurrentCount - NextCount
)

View solution in original post

6 REPLIES 6
ahmedashour
Regular Visitor

Just create the following visual calculation

Expected W-1 = [Current W-1] - NEXT([Current W-1])
tamerj1
Super User
Super User

Hi @Anonymous 

what determines the order of CATEGORIES? What is the DAX code of the Current W1 measure?

Anonymous
Not applicable

The simplified model looks like this: 

mpataki32_0-1741717870183.png


In a nutshell I am ordering the "Category_label" field with the "Category_order" field with the built in function (under the "Column tools"), the "Category_order" field is integer. 

The Current W1 measure looks like this: 

 

Count.AllOpportunities = 
VAR previousweek = [Date.8to14Days]
RETURN
CALCULATE(COUNT('fact_table'[UniqueID]),FILTER('fact_table','fact_table'[Event_date]> previousweek))

 

 
The previousweek variable returns a single date to determine what is the cut off date. 

@Anonymous 

Please try

Count.AllOpportunities.New =
VAR CategoriesAndCounts =
ADDCOLUMNS (
ALLSELECTED ( Category_Table[Category_Order] ),
"@Count",
CALCULATE (
[Count.AllOpportunities],
ALLEXCEPT ( Category_Table, Category_Table[Category_Order] )
)
)
RETURN
SUMX (
VALUES ( Category_Table[Category_Order] ),
VAR CurrentOrder = Category_Table[Category_Order]
VAR CurrentCount = [Count.AllOpportunities]
VAR NextCount =
MAXX (
TOPN (
1,
FILTER ( CategoriesAndCounts, Category_Table[Category_Order] > CurrentOrder ),
Category_Table[Category_Order], ASC
),
[@Count]
)
RETURN
CurrentCount - NextCount
)

Anonymous
Not applicable

and it worked 😮 thank you so much! but I really need to ask that did you use any kind of help, because that was outstanding

@Anonymous 

Definitely no AI. Not even a laptop 😂 I just typed the code using my phone

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.

Top Solution Authors