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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
alessiomissio
Frequent Visitor

Cumulative Subtraction using Criteria

Hi all,

 

I'll try to be clear as much as I can 😀.

I've a tough one and I cannot find any way to do this.
I've 2 tables T1 and T2.
This is T1:

KeyMonthAmount
K1Jan15
K1Feb0
K1Mar10
K1Apr0
K1May0

and this is T2:

KeyMonthAmount
K1Jan0
K1Feb0
K1Mar5
K1Apr5
K1May10

What I need to do is to SUBTRACT T2[Amount] from T1[Amount] desregarding the month and ignoring where T2[Amount] is 0, but the amount needs to be cumulated until T1[Amount] is zero.
The final result should be:

KeyMonthResultLogic
K1Jan015-(5+5+5)
K1Feb0 
K1Mar510-5
K1Apr0 
K1May0 

For Jan in T1 (5+5+5) is T2[Amount] of Mar + T2[Amount] of Apr + part of T2[Amount] of May.
For Mar in T1 (5) is the remaining part of T2[Amount] of May.

 

Of course I've created 2 measure to do subtract the 2 amounts, but by doing this I'm losing the month detail.

 

Any idea please?

 

Many thanks

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @alessiomissio 

There isn't that much data to test on but here it is based on what's available. Please note that a proper date column had to be used as cumulative can't be meaningfully applied to text (your month column)

Cumulative Amount t1 = 
CALCULATE (
    SUM ( T1[T1 Amount] ),
    FILTER (
        ALL ( Months ),
        Months[Start of Month] <= MAX ( Months[Start of Month] )
    )
)
Balance = 
SUMX (
    ADDCOLUMNS (
        ADDCOLUMNS (
            SUMMARIZE (
                FILTER ( T1, T1[T1 Amount] <> 0 ),
                'Key'[Key],
                Months[Start of Month]
            ),
            "@Cumulative Amount", [Cumulative Amount t1],
            "@T2 Amt", CALCULATE ( SUM ( T2[T2 Amount] ) )
        ),
        "@balance",
            VAR _bal = [@T2 Amt] - [@Cumulative Amount]
            RETURN
                IF ( _bal > 0, 0, _bal )
    ),
    ABS ( [@balance] )
)

danextian_0-1733222314436.png

danextian_1-1733222421147.png

danextian_2-1733222443368.png

 

 

 

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

15 REPLIES 15
danextian
Super User
Super User

Hi @alessiomissio 

There isn't that much data to test on but here it is based on what's available. Please note that a proper date column had to be used as cumulative can't be meaningfully applied to text (your month column)

Cumulative Amount t1 = 
CALCULATE (
    SUM ( T1[T1 Amount] ),
    FILTER (
        ALL ( Months ),
        Months[Start of Month] <= MAX ( Months[Start of Month] )
    )
)
Balance = 
SUMX (
    ADDCOLUMNS (
        ADDCOLUMNS (
            SUMMARIZE (
                FILTER ( T1, T1[T1 Amount] <> 0 ),
                'Key'[Key],
                Months[Start of Month]
            ),
            "@Cumulative Amount", [Cumulative Amount t1],
            "@T2 Amt", CALCULATE ( SUM ( T2[T2 Amount] ) )
        ),
        "@balance",
            VAR _bal = [@T2 Amt] - [@Cumulative Amount]
            RETURN
                IF ( _bal > 0, 0, _bal )
    ),
    ABS ( [@balance] )
)

danextian_0-1733222314436.png

danextian_1-1733222421147.png

danextian_2-1733222443368.png

 

 

 

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

With some tweaks I've achieved what I was looking for.

Thanks to @danextian that guided me to the correct solution.

Hi @danextian ,

 

thank you for your reply and thank you for this!

It works, but I need an additional requirement, which invalidates your current solution.

The user should have the ability to use the Month as slicer.

Using the slicer and selecting all the months from January to March, would return this:

KeyMonthT1 AmountT2 AmountBalance
K1Jan1005
K1Feb000
K1Mar15515


I've tried to play around with your solution, but I cannot get the desired result.

 

Can you please help me?

 

Thanks

This is what I am getting...

danextian_0-1733266855807.png

Did you follow the sample model to the tee or other information in your data being used that is not in your sample?










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Forgot to  attach a sample pbix.










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Hi @danextian ,

 

thanks a lot for your reply.

Please find below the link to my PBIX. (sadly I cannot upload files yet).

LINK 

 

By the way if I try to use the slicer of the Month, reducing data, this is not reflected in the final Balance.

 

Screenshot 2024-12-04 at 09.05.35.png

I hope I've explained myself clearly.

Thank you for your time.

 

danextian_1-1733486240556.png

 

danextian_0-1733486126644.png

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

I am confused. Are you supposed to show the remaining revenue balance or what's been deducted for every month? Because if what's been deducted, your table is supposed to show 10 and 10 respectively for Jan and Mar, totalling to 20 because T2 is supposed to be deducted regardless of the month in it.

danextian_0-1733318964917.png

danextian_1-1733318974980.png

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Hi @danextian ,

 

when you select all the months between January and February, you should obtain:

Screenshot 2024-12-04 at 14.49.51.png

This because March amount from T2 is subtracted from the first available amount (January) from T1.
I've attached the PBIX above, please rename it from txt to pbix, since Dropbox doesn't allow to download PBIX files.

 

 

So it isnt regardless of the month from T2 but from all the months selected in the slicer?

danextian_0-1733374002720.png

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Correct!

 

I wrote desregarding the month in T2 because I need to subtract the first not zero amount of March of T2 to the first not zero amount of T1.

Hi @alessiomissio ,

As danextian mentioned, the balance on 3/1/2025, should be 20 instead of 15 according to your calculation logic. Could you please provide a detailed explanation of your calculation method based on the sample data? This will help us offer the appropriate solution. Thank you.

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Bibiano_Geraldo
Community Champion
Community Champion

Hi @alessiomissio ,

What determine the part of amount in may?

 

 

I hope this helps! 
If you found this answer helpful:
✔️ Mark it as the solution to help others find it faster.
 Give it a like to show your appreciation!

Thank you for contributing to our amazing Power BI community! 
Kedar_Pande
Community Champion
Community Champion

@alessiomissio 

Ensure both tables (T1 and T2) have a relationship on the Key column.

 

Create Measures

Cumulative Amount T2 =
CALCULATE(
SUM(T2[Amount]),
FILTER(
T2,
T2[Amount] > 0
),
REMOVEFILTERS(T2[Month])
)
Cumulative Balance T1 =
VAR CurrentMonth = MAX(T1[Month])
VAR T1Amount = SUM(T1[Amount])
VAR PreviousBalance =
CALCULATE(
SUM(T1[Amount]) - [Cumulative Amount T2],
FILTER(T1, T1[Month] <= CurrentMonth)
)
RETURN
MAX(0, PreviousBalance)
Final Result =
VAR T1Amount = SUM(T1[Amount])
VAR CumulativeUsedT2 =
SUMX(
FILTER(
T2,
T2[Amount] > 0
),
T2[Amount]
)
RETURN
IF(
T1Amount > 0,
MAX(0, T1Amount - CumulativeUsedT2),
0
)

Add Key, Month, and the Final Result measure to your visualization.
Confirm the results match your expectations.

 

💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
Cheers,
Kedar
Connect on LinkedIn

Hi @Kedar_Pande 

 

Thanks a lot for your reply!

 

I confirm that both tables currently have a relation (Many to Many) using Key field.

 

I've tried the solution but it doesn't work. 

I've understand the measure Cumulative Amount T2.

But I don't understand Cumulative Balance T1. Where is it used?

By the way below is the table with the Final Result measure.

Final_Result.png

Am I missing something?

 

 

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.