<?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: Power Query - conditional cumulative sum in Power Query</title>
    <link>https://community.fabric.microsoft.com/t5/Power-Query/Power-Query-conditional-cumulative-sum/m-p/3761713#M123789</link>
    <description>&lt;P&gt;Please provide sample data that covers your issue or question &lt;STRONG&gt;completely&lt;/STRONG&gt;, in a &lt;STRONG&gt;usable&lt;/STRONG&gt; format (not as a screenshot).&lt;BR /&gt;&lt;BR /&gt;Do not include sensitive information or anything not related to the issue or question. &lt;BR /&gt;&lt;BR /&gt;If you are unsure how to upload data please refer to &lt;A href="https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216" target="_blank"&gt;https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216&lt;/A&gt; &lt;BR /&gt;&lt;BR /&gt;Please show the expected outcome based on the sample data you provided. &lt;BR /&gt;&lt;BR /&gt;Want faster answers? &lt;A href="https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523" target="_blank"&gt;https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 13 Mar 2024 21:03:39 GMT</pubDate>
    <dc:creator>lbendlin</dc:creator>
    <dc:date>2024-03-13T21:03:39Z</dc:date>
    <item>
      <title>Power Query - conditional cumulative sum</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Power-Query-conditional-cumulative-sum/m-p/3759002#M123714</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am asking for help in creating a cumulative sum in Power Query based on conditions. I am attaching a screenshot with sample data. In PowerQuery I have columns user_id, quantity, main_flag, continue_flag, new_flag. To illustrate this better, I created auxiliary columns sum_index and sum_range.&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;The excel condition for sum_index:&lt;BR /&gt;=IF([@[main_flag]]=0;0;&lt;BR /&gt;IF([@[new_flag]]=1;1;&lt;BR /&gt;IF([@[continue_flag]]=1;F1+1)))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The cumulative total is calculated based on these indexes and the value from the quantity column. If main_flag = 0, then cumulative sum = 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2024 22:19:51 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Power-Query-conditional-cumulative-sum/m-p/3759002#M123714</guid>
      <dc:creator>Mateusz_</dc:creator>
      <dc:date>2024-03-12T22:19:51Z</dc:date>
    </item>
    <item>
      <title>Re: Power Query - conditional cumulative sum</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Power-Query-conditional-cumulative-sum/m-p/3761713#M123789</link>
      <description>&lt;P&gt;Please provide sample data that covers your issue or question &lt;STRONG&gt;completely&lt;/STRONG&gt;, in a &lt;STRONG&gt;usable&lt;/STRONG&gt; format (not as a screenshot).&lt;BR /&gt;&lt;BR /&gt;Do not include sensitive information or anything not related to the issue or question. &lt;BR /&gt;&lt;BR /&gt;If you are unsure how to upload data please refer to &lt;A href="https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216" target="_blank"&gt;https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216&lt;/A&gt; &lt;BR /&gt;&lt;BR /&gt;Please show the expected outcome based on the sample data you provided. &lt;BR /&gt;&lt;BR /&gt;Want faster answers? &lt;A href="https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523" target="_blank"&gt;https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2024 21:03:39 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Power-Query-conditional-cumulative-sum/m-p/3761713#M123789</guid>
      <dc:creator>lbendlin</dc:creator>
      <dc:date>2024-03-13T21:03:39Z</dc:date>
    </item>
    <item>
      <title>Re: Power Query - conditional cumulative sum</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Power-Query-conditional-cumulative-sum/m-p/3790634#M125069</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="704682" data-lia-user-login="Mateusz_" class="lia-mention lia-mention-user"&gt;Mateusz_&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;Have you solved your problem? If not, you can try this.&lt;BR /&gt;&lt;BR /&gt;Here is my sample data:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;Do not perform any operations after the original table is imported. First I add a new table following the next steps:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;Put all of the M function into the &lt;STRONG&gt;Advanced Editor&lt;/STRONG&gt;:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwVNJRAmEDOI7VISRuiiRniCRuDBU3RFNvgkMcp71GGBLm5kZYNSDEUR0EETcyxLCZsEHEiVsYmgPZlhgWQ8TNcIjjMgenOKrlJBsUCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, quantity = _t, main_flag = _t, continue_flag = _t, new_flag = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"quantity", Int64.Type}, {"main_flag", Int64.Type}, {"continue_flag", Int64.Type}, {"new_flag", Int64.Type}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([continue_flag] = 1)),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"ID"}, {{"Index", each Table.AddIndexColumn(_, "Index", 1, 1, Int64.Type)}}),
    #"Expanded Index" = Table.ExpandTableColumn(#"Grouped Rows", "Index", {"quantity", "main_flag", "continue_flag", "new_flag", "Index"}, {"Index.quantity", "Index.main_flag", "Index.continue_flag", "Index.new_flag", "Index.Index"})
in
    #"Expanded Index"&lt;/LI-CODE&gt;
&lt;P&gt;And we will get such a new table:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Still select &lt;STRONG&gt;Blank Query&lt;/STRONG&gt;:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;And now let put another M function into the &lt;STRONG&gt;Advanced Editor&lt;/STRONG&gt;:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;let
    Source = Table.NestedJoin(Origin, {"ID", "quantity", "main_flag", "continue_flag", "new_flag"}, Table, {"ID", "Index.quantity", "Index.main_flag", "Index.continue_flag", "Index.new_flag"}, "Table (2)", JoinKind.LeftOuter),
    #"Expanded Table (2)" = Table.ExpandTableColumn(Source, "Table (2)", {"Index.Index"}, {"Table (2).Index.Index"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Table (2)", "sum_index", each if [new_flag] = 1 then 1 else [#"Table (2).Index.Index"] + 1),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Table (2).Index.Index"}),
    #"Added Custom1" = Table.AddColumn(#"Removed Columns", "Custom", each if [sum_index] = 1 then [quantity] 
else List.Sum(
    Table.SelectRows(
        #"Added Custom",
        (row) =&amp;gt; row[ID] = [ID] and row[sum_index] &amp;lt;= [sum_index]
    )[quantity]
)),
    #"Replaced Value" = Table.ReplaceValue(#"Added Custom1",null,0,Replacer.ReplaceValue,{"ID", "sum_index", "Custom"})
in
    #"Replaced Value"&lt;/LI-CODE&gt;
&lt;P&gt;Then we will get the final output as below ( The results are in a different order than your screenshot, but the data results are the same ) :&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Best Regards,&lt;BR /&gt;Dino Tao&lt;BR /&gt;If this post &lt;EM&gt;&lt;STRONG&gt;helps&lt;/STRONG&gt;&lt;/EM&gt;, then please consider &lt;EM&gt;&lt;STRONG&gt;Accept it as the solution&lt;/STRONG&gt;&lt;/EM&gt; to help the other members find it more quickly.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2024 05:41:03 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Power-Query-conditional-cumulative-sum/m-p/3790634#M125069</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-03-26T05:41:03Z</dc:date>
    </item>
  </channel>
</rss>

