<?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: For each group find first negative value and replace subsequent years with zero in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/For-each-group-find-first-negative-value-and-replace-subsequent/m-p/2218180#M52553</link>
    <description>&lt;P&gt;Hi&amp;nbsp;Anonymous&lt;/LI-USER&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try this code to add a new column to your table:&lt;/P&gt;&lt;LI-CODE lang="php"&gt;New value =
VAR _CY = [Year]
VAR _LY = _CY - 1
RETURN
    IF(
        [Value] &amp;lt; 0,
        0,
        IF(
            CALCULATE(
                MAX( 'Table'[Value] ),
                FILTER(
                    ALL( 'Table' ),
                    'Table'[Group] = EARLIER( 'Table'[Group] )
                        &amp;amp;&amp;amp; 'Table'[Category] = EARLIER( 'Table'[Category] )
                        &amp;amp;&amp;amp; 'Table'[Country] = EARLIER( 'Table'[Country] )
                        &amp;amp;&amp;amp; 'Table'[Year] = _LY
                )
            ) &amp;lt; 0,
            0,
            [Value]
        )
    )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output:&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&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;&lt;P&gt;&lt;SPAN&gt;If this post&amp;nbsp;&lt;STRONG&gt;&lt;SPAN&gt;helps&lt;/SPAN&gt;&lt;/STRONG&gt;, please consider&amp;nbsp;&lt;SPAN&gt;&lt;STRONG&gt;&lt;SPAN&gt;accepting&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;EM&gt;&lt;STRONG&gt;&lt;SPAN&gt;&lt;SPAN&gt;&amp;nbsp;it as the solution&lt;/SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/EM&gt;to help the other members find it more quickly.&lt;BR /&gt;&lt;STRONG&gt;&lt;SPAN&gt;Appreciate your Kudos!!&lt;BR /&gt;&lt;STRONG&gt;LinkedIn:&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;SPAN&gt;&lt;A href="https://www.linkedin.com/in/vahid-dm/" target="_blank"&gt;www.linkedin.com/in/vahid-dm/&lt;/A&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 02 Dec 2021 12:54:50 GMT</pubDate>
    <dc:creator>VahidDM</dc:creator>
    <dc:date>2021-12-02T12:54:50Z</dc:date>
    <item>
      <title>For each group find first negative value and replace subsequent years with zero</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/For-each-group-find-first-negative-value-and-replace-subsequent/m-p/2217801#M52536</link>
      <description>&lt;P&gt;Hello, how can I get the first negative number for each Group, Category and Country and then make the new value for that year and years after value zero?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Group&lt;/TD&gt;&lt;TD&gt;Category&lt;/TD&gt;&lt;TD&gt;Country&lt;/TD&gt;&lt;TD&gt;Year&lt;/TD&gt;&lt;TD&gt;Value&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;New value&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2018&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;50&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2019&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;20&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2020&lt;/TD&gt;&lt;TD&gt;-10&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2021&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PS: It concerns a table with about 840.000 rows. (Groups (3) x categories (20) x countries (100) x years&amp;nbsp; (140). In terms of performance, it it best to do this in Power Query or in DAX? If I use a DAX measure I need to make sure the transformation is done on the group level so not aggregate countries and categories etc.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 09:12:20 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/For-each-group-find-first-negative-value-and-replace-subsequent/m-p/2217801#M52536</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-12-02T09:12:20Z</dc:date>
    </item>
    <item>
      <title>Re: For each group find first negative value and replace subsequent years with zero</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/For-each-group-find-first-negative-value-and-replace-subsequent/m-p/2218180#M52553</link>
      <description>&lt;P&gt;Hi&amp;nbsp;Anonymous&lt;/LI-USER&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try this code to add a new column to your table:&lt;/P&gt;&lt;LI-CODE lang="php"&gt;New value =
VAR _CY = [Year]
VAR _LY = _CY - 1
RETURN
    IF(
        [Value] &amp;lt; 0,
        0,
        IF(
            CALCULATE(
                MAX( 'Table'[Value] ),
                FILTER(
                    ALL( 'Table' ),
                    'Table'[Group] = EARLIER( 'Table'[Group] )
                        &amp;amp;&amp;amp; 'Table'[Category] = EARLIER( 'Table'[Category] )
                        &amp;amp;&amp;amp; 'Table'[Country] = EARLIER( 'Table'[Country] )
                        &amp;amp;&amp;amp; 'Table'[Year] = _LY
                )
            ) &amp;lt; 0,
            0,
            [Value]
        )
    )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output:&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&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;&lt;P&gt;&lt;SPAN&gt;If this post&amp;nbsp;&lt;STRONG&gt;&lt;SPAN&gt;helps&lt;/SPAN&gt;&lt;/STRONG&gt;, please consider&amp;nbsp;&lt;SPAN&gt;&lt;STRONG&gt;&lt;SPAN&gt;accepting&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;EM&gt;&lt;STRONG&gt;&lt;SPAN&gt;&lt;SPAN&gt;&amp;nbsp;it as the solution&lt;/SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/EM&gt;to help the other members find it more quickly.&lt;BR /&gt;&lt;STRONG&gt;&lt;SPAN&gt;Appreciate your Kudos!!&lt;BR /&gt;&lt;STRONG&gt;LinkedIn:&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;SPAN&gt;&lt;A href="https://www.linkedin.com/in/vahid-dm/" target="_blank"&gt;www.linkedin.com/in/vahid-dm/&lt;/A&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 12:54:50 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/For-each-group-find-first-negative-value-and-replace-subsequent/m-p/2218180#M52553</guid>
      <dc:creator>VahidDM</dc:creator>
      <dc:date>2021-12-02T12:54:50Z</dc:date>
    </item>
    <item>
      <title>Re: For each group find first negative value and replace subsequent years with zero</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/For-each-group-find-first-negative-value-and-replace-subsequent/m-p/2218266#M52557</link>
      <description>&lt;P&gt;Aside from calculated column with DAX, Table.MinN() in PQ easily does the trick in your scenario. Since the large dataset is logically sliced into many small ones, I expect the performance is satisfactory.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddBJDsAgCAXQu7DGBBw6LHsOw/2vUaBNuuh3Yb74VBLmJCWmy1dkFT08hpDxT84MIH7IVBSSfo/qK+P5TvISAF1BlB1By/5Iem4BRJSGZPOAsC+7xMwqmd0=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Group = _t, Category = _t, Country = _t, Year = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}, {"Value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Group", "Category", "Country"}, {{"ar", each let tb = Table.MinN(_, "Year", each [Value] &amp;gt; 0), tb0 = Table.ReplaceValue(Table.Skip(_, Table.RowCount(tb)), each [Value], 0, Replacer.ReplaceValue, {"Value"}) in tb &amp;amp; tb0}}),
    #"Combined Tables" = Table.Combine(#"Grouped Rows"[ar])
in
    #"Combined Tables"&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 13:57:55 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/For-each-group-find-first-negative-value-and-replace-subsequent/m-p/2218266#M52557</guid>
      <dc:creator>CNENFRNL</dc:creator>
      <dc:date>2021-12-02T13:57:55Z</dc:date>
    </item>
    <item>
      <title>Re: For each group find first negative value and replace subsequent years with zero</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/For-each-group-find-first-negative-value-and-replace-subsequent/m-p/2218667#M52590</link>
      <description>&lt;P&gt;Anonymous&lt;/LI-USER&gt;&amp;nbsp; are you getting it from a SQL server? If there is no filter context involved for this task, doing the transformation server side will give you the optimimum performnace.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, in TSQL&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;declare @t1 as table (grp int,cat varchar(10), country int, year int, value int, row int) 
insert into @t1 
select 
  * 
from 
  (
    values 
      (1, 'A', 1, 2018, 50, 1), 
      (1, 'A', 1, 2019, 20, 2), 
      (1, 'A', 1, 2020,-10, 3), 
      (1, 'A', 1, 2021, 20, 4), 
      (1, 'B', 1, 2018, 50, 5), 
      (1, 'B', 1, 2019,-20, 6), 
      (1, 'B', 1, 2020,-10, 7), 
      (1, 'B', 1, 2021, 20, 8)
  ) t(a, b, c, d, e, f);
with cte1 as(
  select 
    grp, cat, country, year, value, row, 
    CASE when sum(value) over ( partition by grp, cat, country, year )&amp;lt; 0 then 0 else null end as _helper 
  from  @t1
) 
select 
  grp, cat, country, year, value, row, 
  CASE WHEN max(_helper) over(partition by grp, cat, country order by year rows unbounded preceding ) IS NULL then value 
      else max(_helper) over (partition by grp, cat, country order by year rows unbounded preceding ) end as Desired 
from cte1 
order by row
&lt;/LI-CODE&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;</description>
      <pubDate>Thu, 02 Dec 2021 17:51:37 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/For-each-group-find-first-negative-value-and-replace-subsequent/m-p/2218667#M52590</guid>
      <dc:creator>smpa01</dc:creator>
      <dc:date>2021-12-02T17:51:37Z</dc:date>
    </item>
  </channel>
</rss>

