<?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 Calculate Cumulative Sum in between dates (2 queries) in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Cumulative-Sum-in-between-dates-2-queries/m-p/3532234#M135730</link>
    <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i am trying to create a Measure in Power BI Desktop that does the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 2 Queries:&lt;/P&gt;&lt;P&gt;1) Cleaning Data with Column [Date] and Column [Amount of cleanings] - here we see how much was cleaned on a day, the Dates have the whole year except Sundays&lt;/P&gt;&lt;P&gt;2) Change of Waste with Column [Date] and an Index Column - here we see on which dates big waste containers were exchanged&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am interested now in calculated how many Cleanings there were inbetween the exchange of a waste container.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tables to simplify:&amp;nbsp;&lt;/P&gt;&lt;P&gt;1)&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;TD&gt;Amount of Cleanings&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;02.01.2023&lt;/TD&gt;&lt;TD&gt;40&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;03.01.2023&lt;/TD&gt;&lt;TD&gt;37&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;04.01.2023&lt;/TD&gt;&lt;TD&gt;68&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;05.01.2023&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;06.01.2023&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2)&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;TD&gt;Index&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;04.01.2023&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;06.01.2023&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The result should be that depending on the Date of Query 2 there should be the cumulate Sum of Cleanings for this year,&lt;/P&gt;&lt;P&gt;in this example:&amp;nbsp;&lt;/P&gt;&lt;P&gt;04.01.2023 - 145&lt;/P&gt;&lt;P&gt;06.01.2023 - 62&lt;/P&gt;&lt;P&gt;This is want to plot with date of query 2.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My problem is that i cannot make it work to lookup the cumulative Sum of the date before the actual date. Hence, i get only a cumulative Sum for every date entry of query 2.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to make it simpler by adding a column with a cumulated sum in my query 1 to simply to simply subtract the cumulate value of the corresponding date with the date before. Also, with this attempt there will be a point where there is no date "before", in this case the date "01.01.2023" shoud be used to lookup the cumulated sum.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my attempt, but it failed since it did not subtract any values - probably because the PreviousDate Variable is not correct.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Waste Exchange = 
VAR CurrentID = SELECTEDVALUE('Waste Exchange'[ID])
VAR CurrentDate = SELECTEDVALUE('Waste Exchange'[Date])
VAR PreviousDate =
    IF(
        CurrentID = 1,
        DATE(2023, 1, 1),
        CALCULATE(
        MAX('Waste Exchange'[Date]),
        'Waste Exchange'[ID] = CurrentID - 1
        )
    )
RETURN
    IF(
        ISBLANK(CurrentDate),
        BLANK(),
        CALCULATE(
        SUMX(
            FILTER(
            ‘Cleaning Data’,
            ‘Cleaning Data’[Date] = CurrentDate
            ),
        ‘Cleaning Data’[Cumulative Sum]
        )
    ) -
    CALCULATE(
        SUMX(
            FILTER(
            ‘Cleaning Data’,
            ‘Cleaning Data’[Date] = PreviousDate
            ),
        ‘Cleaning Data’[Cumulative Sum]
        )
    )
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope you guys can help me out with this!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Seb&lt;/P&gt;</description>
    <pubDate>Tue, 14 Nov 2023 05:42:37 GMT</pubDate>
    <dc:creator>seppel123</dc:creator>
    <dc:date>2023-11-14T05:42:37Z</dc:date>
    <item>
      <title>Calculate Cumulative Sum in between dates (2 queries)</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Cumulative-Sum-in-between-dates-2-queries/m-p/3532234#M135730</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i am trying to create a Measure in Power BI Desktop that does the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 2 Queries:&lt;/P&gt;&lt;P&gt;1) Cleaning Data with Column [Date] and Column [Amount of cleanings] - here we see how much was cleaned on a day, the Dates have the whole year except Sundays&lt;/P&gt;&lt;P&gt;2) Change of Waste with Column [Date] and an Index Column - here we see on which dates big waste containers were exchanged&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am interested now in calculated how many Cleanings there were inbetween the exchange of a waste container.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tables to simplify:&amp;nbsp;&lt;/P&gt;&lt;P&gt;1)&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;TD&gt;Amount of Cleanings&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;02.01.2023&lt;/TD&gt;&lt;TD&gt;40&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;03.01.2023&lt;/TD&gt;&lt;TD&gt;37&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;04.01.2023&lt;/TD&gt;&lt;TD&gt;68&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;05.01.2023&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;06.01.2023&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2)&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;TD&gt;Index&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;04.01.2023&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;06.01.2023&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The result should be that depending on the Date of Query 2 there should be the cumulate Sum of Cleanings for this year,&lt;/P&gt;&lt;P&gt;in this example:&amp;nbsp;&lt;/P&gt;&lt;P&gt;04.01.2023 - 145&lt;/P&gt;&lt;P&gt;06.01.2023 - 62&lt;/P&gt;&lt;P&gt;This is want to plot with date of query 2.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My problem is that i cannot make it work to lookup the cumulative Sum of the date before the actual date. Hence, i get only a cumulative Sum for every date entry of query 2.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to make it simpler by adding a column with a cumulated sum in my query 1 to simply to simply subtract the cumulate value of the corresponding date with the date before. Also, with this attempt there will be a point where there is no date "before", in this case the date "01.01.2023" shoud be used to lookup the cumulated sum.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my attempt, but it failed since it did not subtract any values - probably because the PreviousDate Variable is not correct.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Waste Exchange = 
VAR CurrentID = SELECTEDVALUE('Waste Exchange'[ID])
VAR CurrentDate = SELECTEDVALUE('Waste Exchange'[Date])
VAR PreviousDate =
    IF(
        CurrentID = 1,
        DATE(2023, 1, 1),
        CALCULATE(
        MAX('Waste Exchange'[Date]),
        'Waste Exchange'[ID] = CurrentID - 1
        )
    )
RETURN
    IF(
        ISBLANK(CurrentDate),
        BLANK(),
        CALCULATE(
        SUMX(
            FILTER(
            ‘Cleaning Data’,
            ‘Cleaning Data’[Date] = CurrentDate
            ),
        ‘Cleaning Data’[Cumulative Sum]
        )
    ) -
    CALCULATE(
        SUMX(
            FILTER(
            ‘Cleaning Data’,
            ‘Cleaning Data’[Date] = PreviousDate
            ),
        ‘Cleaning Data’[Cumulative Sum]
        )
    )
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope you guys can help me out with this!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Seb&lt;/P&gt;</description>
      <pubDate>Tue, 14 Nov 2023 05:42:37 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Cumulative-Sum-in-between-dates-2-queries/m-p/3532234#M135730</guid>
      <dc:creator>seppel123</dc:creator>
      <dc:date>2023-11-14T05:42:37Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Cumulative Sum in between dates (2 queries)</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Cumulative-Sum-in-between-dates-2-queries/m-p/3532295#M135732</link>
      <description>&lt;P&gt;Hi, I am not sure how your datamodel looks like, but please check the below picture and the attached pbix file.&lt;/P&gt;
&lt;P&gt;It is for creating a new column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Expected result CC =
VAR _currentdate = Change[Date]
VAR _previousdateindex =
    MAXX (
        FILTER ( Change, Change[Index] &amp;lt; EARLIER ( Change[Index] ) ),
        Change[Index]
    )
VAR _previousdate =
    MAXX ( FILTER ( Change, Change[Index] = _previousdateindex ), Change[Date] )
RETURN
    IF (
        NOT ISBLANK ( _previousdate ),
        SUMX (
            FILTER (
                Cleanings,
                Cleanings[Date] &amp;gt; _previousdate
                    &amp;amp;&amp;amp; Cleanings[Date] &amp;lt;= _currentdate
            ),
            Cleanings[Amount of Cleanings]
        ),
        SUMX (
            FILTER ( Cleanings, Cleanings[Date] &amp;lt;= _currentdate ),
            Cleanings[Amount of Cleanings]
        )
    )
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Nov 2023 06:15:44 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Cumulative-Sum-in-between-dates-2-queries/m-p/3532295#M135732</guid>
      <dc:creator>Jihwan_Kim</dc:creator>
      <dc:date>2023-11-14T06:15:44Z</dc:date>
    </item>
  </channel>
</rss>

