<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: filter? Cumulative subtraction doesn't work in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/filter-Cumulative-subtraction-doesn-t-work/m-p/4391403#M174338</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="778360" data-lia-user-login="ohnothimagain" class="lia-mention lia-mention-user"&gt;ohnothimagain&lt;/a&gt;,&lt;BR /&gt;&lt;BR /&gt;Accoridng to my knwoledge (which is still limited) there is a solution for your problem.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Because you want run cumulative sum for [Qty] column basing on [PN] column I think that it will be not enough to use date column during this agregation. The &lt;EM&gt;Measure 1 code&lt;/EM&gt;&amp;nbsp;(part of it was used in the previous example), in my opinion will result with the same cumulative result for the same PN value and date value (and different WBS values), check Table 1 .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;    Measure 1 = CALCULATE(
        SUM([Qty]), 
        FILTER(ALLSELECTED('Calendar'), 'Calendar'[Date] &amp;lt;= CurrentDate)
    )​&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table 1.&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;M_Date (DD/MM/YYYY)&lt;/TD&gt;&lt;TD&gt;WBS&lt;/TD&gt;&lt;TD&gt;PN&lt;/TD&gt;&lt;TD&gt;QTY&lt;/TD&gt;&lt;TD&gt;Cumulative QTY (Measure 1)&lt;/TD&gt;&lt;TD&gt;Cumulative QTY (Your expected outcome)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;02/27/2024&lt;/TD&gt;&lt;TD&gt;11253&lt;/TD&gt;&lt;TD&gt;X-0002-000-1000&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;700&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;300&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;02/27/2024&lt;/TD&gt;&lt;TD&gt;29734&lt;/TD&gt;&lt;TD&gt;X-0002-000-1000&lt;/TD&gt;&lt;TD&gt;400&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;700&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;700&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think that the remedy for this issue is using column that will somehow rank each row basing on date value nad WBS value. Then when table is properly sorted you will get desireble result.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;First step is to create&amp;nbsp;&lt;EM&gt;Rank&amp;nbsp;&lt;/EM&gt;calculated column, which code you will find below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Rank = CONVERT('Sheet1'[M_Date],INTEGER) * 1000000 + 'Sheet1'[WBS]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why am I multiplying date by 1 000 000? Your WBS numbers consist of 5 digits, so if I want to be sure that on 28/02/2024 product with WBS 11253 will have lower rank that the same product (same WBS) the day after (29/02/2024) I need to multiply the date by number higher than 99 999. If you would have WBS number built from six digits then I would need to multiply by 10 000 000.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thanks to this column whe can built cumulative aggregation basing not only on date but also on WBS number (If I understood your requirements well it is something that you want to achive).&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Now we can built the measure that will acumulate QTY but will use STK only once per PN number. Here it goes:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Cumulative Result STK - QTY = 

VAR _UniquePN = VALUES('Sheet1'[PN]) -- return unique PN values in the filter context
VAR _MaxRank = MAX('Sheet1'[Rank]) -- return maxmimum rank in the filter context
VAR _STK = SUMX(
                        _UniquePN,
                        CALCULATE(
                                    MAX('Sheet1'[STK]),
                                    REMOVEFILTERS('Sheet1'[WBS],'Sheet1'[Customer])
           ) -- for single row returns STK for single row, for collapsed date sum of STK per 
                PN (counted only once)

VAR _CumulativeQTY =   SUMX(
                        _UniquePN,
                        CALCULATE(
                                    SUM('Sheet1'[Qty]),
                                    'Sheet1'[Rank] &amp;lt;= _MaxRank,                                 
                              REMOVEFILTERS('Sheet1'[WBS],'Sheet1'[Customer],'Sheet1'[M_Date])
                        )
                ) - returns the cumulative QTY 
VAR _Result =  IF(_STK - _CumulativeQTY &amp;gt;=0, BLANK(), _STK - _CumulativeQTY)                   

RETURN
_Result&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The only drawback of this solution is the fact that column [PN] needs to be before column [WBS], I hope it doesn't disqualify this solution.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Please check how does it look like in my file (while expanded and collapsed).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;The only thing I am not sure, whether STK value should be acumulated across diferrent dates or it is unique per date (as in my solution)?&lt;BR /&gt;&lt;BR /&gt;Please let me know what you think about it and if it fits your needs!&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Cheers!&lt;/P&gt;&lt;P&gt;Jan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 02 Feb 2025 20:09:57 GMT</pubDate>
    <dc:creator>PowerBIHelper20</dc:creator>
    <dc:date>2025-02-02T20:09:57Z</dc:date>
    <item>
      <title>filter? Cumulative subtraction doesn't work</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/filter-Cumulative-subtraction-doesn-t-work/m-p/4390708#M174311</link>
      <description>&lt;P&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;Hello everyone &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;I'm working in production plannning control,hope anyone can save me...&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;BR /&gt;I want to create a table that allocates inventory by each PN,but the cumulative calculation is incorrect...&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;-&amp;gt;I need to keep Result's Column layout.but WBS context can't remove&lt;BR /&gt;How can I revise this?&amp;nbsp; &amp;nbsp;Stock value in each row is duplicate.&lt;BR /&gt;&lt;BR /&gt;■Table&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;■Measure&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;cml:=if(max('TEST_'[WBS])="",blank(),
if('TEST_'[stk_]-calculate(sum('TEST_'[Qty]),filter(allselected('Calendar'),'Calendar'[Date]&amp;lt;=max('Calendar'[Date])))&amp;lt;0,
'TEST_'[stk_]-calculate(sum('TEST_'[Qty]),filter(ALLSELECTED('Calendar'),'Calendar'[Date]&amp;lt;=max('Calendar'[Date]))),blank()
))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;■Result&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your continuing support.&lt;BR /&gt;Best Regards,&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Feb 2025 09:12:25 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/filter-Cumulative-subtraction-doesn-t-work/m-p/4390708#M174311</guid>
      <dc:creator>ohnothimagain</dc:creator>
      <dc:date>2025-02-02T09:12:25Z</dc:date>
    </item>
    <item>
      <title>Re: filter? Cumulative subtraction doesn't work</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/filter-Cumulative-subtraction-doesn-t-work/m-p/4390736#M174312</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="778360" data-lia-user-login="ohnothimagain" class="lia-mention lia-mention-user"&gt;ohnothimagain&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looks like you're trying to allocate inventory per PN while keeping the Result column layout intact, but the cumulative calculation isn't working correctly due to the WBS context. The main issue appears to be that stk_ is duplicating across rows, leading to incorrect stock distribution.&lt;/P&gt;
&lt;P&gt;To address this, we need to ensure that stk_ is only counted once per PN, while still maintaining the WBS context. Additionally, we should adjust the cumulative calculation so that it correctly sums up quantities without double-counting.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;bleep :=
VAR CurrentDate = MAX('Calendar'[Date])
VAR TotalStock = MAX('TEST_'[stk_])  -- Ensure stock is only counted once per PN
VAR UsedQty = 
    CALCULATE(
        SUM('TEST_'[Qty]), 
        FILTER(ALLSELECTED('Calendar'), 'Calendar'[Date] &amp;lt;= CurrentDate)
    )
VAR RemainingStock = TotalStock - UsedQty

RETURN
IF(
    MAX('TEST_'[WBS]) = "",
    BLANK(),
    IF(RemainingStock &amp;lt; 0, RemainingStock, BLANK())
)
&lt;/LI-CODE&gt;
&lt;P&gt;This approach ensures that stock is counted only once per PN by using MAX('TEST_'[stk_]), preventing duplication across rows. The use of ALLSELECTED('Calendar') maintains slicer context while allowing for cumulative summation over dates. Finally, the condition IF(RemainingStock &amp;lt; 0, RemainingStock, BLANK()) ensures that only relevant negative values are displayed.&lt;/P&gt;
&lt;P&gt;If the stock is still incorrect and is summing up across different PNs incorrectly, replacing MAX('TEST_'[stk_]) with SUMX(VALUES('TEST_'[PN]), MAX('TEST_'[stk_])) might help to correctly distribute stock at the PN level.&lt;/P&gt;
&lt;P&gt;Let me know if this resolves the issue or if further refinements are needed!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;</description>
      <pubDate>Sat, 01 Feb 2025 07:18:50 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/filter-Cumulative-subtraction-doesn-t-work/m-p/4390736#M174312</guid>
      <dc:creator>DataNinja777</dc:creator>
      <dc:date>2025-02-01T07:18:50Z</dc:date>
    </item>
    <item>
      <title>Re: filter? Cumulative subtraction doesn't work</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/filter-Cumulative-subtraction-doesn-t-work/m-p/4391199#M174327</link>
      <description>&lt;P&gt;Thank you for your replying quickly!&lt;BR /&gt;I just tried two of your remedies ,but unfortunately the result was same.&lt;BR /&gt;I found Qty accumulation only works when [Date] field closed.&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;context is really complicated for me..&lt;BR /&gt;I don't deal with pbix file by now,sorry for inconvinient:X&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Feb 2025 09:11:01 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/filter-Cumulative-subtraction-doesn-t-work/m-p/4391199#M174327</guid>
      <dc:creator>ohnothimagain</dc:creator>
      <dc:date>2025-02-02T09:11:01Z</dc:date>
    </item>
    <item>
      <title>Re: filter? Cumulative subtraction doesn't work</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/filter-Cumulative-subtraction-doesn-t-work/m-p/4391403#M174338</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="778360" data-lia-user-login="ohnothimagain" class="lia-mention lia-mention-user"&gt;ohnothimagain&lt;/a&gt;,&lt;BR /&gt;&lt;BR /&gt;Accoridng to my knwoledge (which is still limited) there is a solution for your problem.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Because you want run cumulative sum for [Qty] column basing on [PN] column I think that it will be not enough to use date column during this agregation. The &lt;EM&gt;Measure 1 code&lt;/EM&gt;&amp;nbsp;(part of it was used in the previous example), in my opinion will result with the same cumulative result for the same PN value and date value (and different WBS values), check Table 1 .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;    Measure 1 = CALCULATE(
        SUM([Qty]), 
        FILTER(ALLSELECTED('Calendar'), 'Calendar'[Date] &amp;lt;= CurrentDate)
    )​&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table 1.&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;M_Date (DD/MM/YYYY)&lt;/TD&gt;&lt;TD&gt;WBS&lt;/TD&gt;&lt;TD&gt;PN&lt;/TD&gt;&lt;TD&gt;QTY&lt;/TD&gt;&lt;TD&gt;Cumulative QTY (Measure 1)&lt;/TD&gt;&lt;TD&gt;Cumulative QTY (Your expected outcome)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;02/27/2024&lt;/TD&gt;&lt;TD&gt;11253&lt;/TD&gt;&lt;TD&gt;X-0002-000-1000&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;700&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;300&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;02/27/2024&lt;/TD&gt;&lt;TD&gt;29734&lt;/TD&gt;&lt;TD&gt;X-0002-000-1000&lt;/TD&gt;&lt;TD&gt;400&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;700&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;700&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think that the remedy for this issue is using column that will somehow rank each row basing on date value nad WBS value. Then when table is properly sorted you will get desireble result.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;First step is to create&amp;nbsp;&lt;EM&gt;Rank&amp;nbsp;&lt;/EM&gt;calculated column, which code you will find below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Rank = CONVERT('Sheet1'[M_Date],INTEGER) * 1000000 + 'Sheet1'[WBS]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why am I multiplying date by 1 000 000? Your WBS numbers consist of 5 digits, so if I want to be sure that on 28/02/2024 product with WBS 11253 will have lower rank that the same product (same WBS) the day after (29/02/2024) I need to multiply the date by number higher than 99 999. If you would have WBS number built from six digits then I would need to multiply by 10 000 000.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thanks to this column whe can built cumulative aggregation basing not only on date but also on WBS number (If I understood your requirements well it is something that you want to achive).&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Now we can built the measure that will acumulate QTY but will use STK only once per PN number. Here it goes:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Cumulative Result STK - QTY = 

VAR _UniquePN = VALUES('Sheet1'[PN]) -- return unique PN values in the filter context
VAR _MaxRank = MAX('Sheet1'[Rank]) -- return maxmimum rank in the filter context
VAR _STK = SUMX(
                        _UniquePN,
                        CALCULATE(
                                    MAX('Sheet1'[STK]),
                                    REMOVEFILTERS('Sheet1'[WBS],'Sheet1'[Customer])
           ) -- for single row returns STK for single row, for collapsed date sum of STK per 
                PN (counted only once)

VAR _CumulativeQTY =   SUMX(
                        _UniquePN,
                        CALCULATE(
                                    SUM('Sheet1'[Qty]),
                                    'Sheet1'[Rank] &amp;lt;= _MaxRank,                                 
                              REMOVEFILTERS('Sheet1'[WBS],'Sheet1'[Customer],'Sheet1'[M_Date])
                        )
                ) - returns the cumulative QTY 
VAR _Result =  IF(_STK - _CumulativeQTY &amp;gt;=0, BLANK(), _STK - _CumulativeQTY)                   

RETURN
_Result&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The only drawback of this solution is the fact that column [PN] needs to be before column [WBS], I hope it doesn't disqualify this solution.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Please check how does it look like in my file (while expanded and collapsed).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;The only thing I am not sure, whether STK value should be acumulated across diferrent dates or it is unique per date (as in my solution)?&lt;BR /&gt;&lt;BR /&gt;Please let me know what you think about it and if it fits your needs!&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Cheers!&lt;/P&gt;&lt;P&gt;Jan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Feb 2025 20:09:57 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/filter-Cumulative-subtraction-doesn-t-work/m-p/4391403#M174338</guid>
      <dc:creator>PowerBIHelper20</dc:creator>
      <dc:date>2025-02-02T20:09:57Z</dc:date>
    </item>
  </channel>
</rss>

