<?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 Optimized DAX code for calculating on different Filter Context in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimized-DAX-code-for-calculating-on-different-Filter-Context/m-p/3425839#M129757</link>
    <description>&lt;P&gt;Hello. I've encountered a Sencario in Which I have to calculate each Value in different FC(Filter Context) based on predefiend Scenarios of A and B.&lt;/P&gt;&lt;P&gt;here is my Data Structure in Excel.&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in above image, [OldCost] and [NewCost] are two &lt;U&gt;Calculated Columns&lt;/U&gt; and are the result of [Last Purchase Rate]*[Standard Consuption Rate] and &lt;EM&gt;A&lt;/EM&gt; and &lt;EM&gt;B&lt;/EM&gt; are two &lt;U&gt;Measures &lt;/U&gt;calculated by DAX by the following Code snippet for &lt;EM&gt;B&lt;/EM&gt;:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;=
VAR test10 =
    IF(
        MAX( BothBOM[ItemCode] ) &amp;lt;&amp;gt; "0501-2020",
        0,
        SUMX(
            SUMMARIZE(
                FILTER(
                    ALLEXCEPT( BothBOM, BothBOM[ProductCode] ),
                    BothBOM[ItemCode] = "0501-2020"
                        || BothBOM[ItemCode] = "0501-2014"
                ),
                "total",
                    CALCULATE(
                        SUMX( BothBOM, BothBOM[NewCost] ),
                        BothBOM[ItemCode] = "0501-2020"
                    )
                        - CALCULATE(
                            SUMX( BothBOM, BothBOM[OldCost] ),
                            BothBOM[ItemCode] = "0501-2014"
                        )
            ),
            [total]
        )
    )
VAR test9 =
    CALCULATE(
        SUMX( BothBOM, BothBOM[OldCost] ),
        KEEPFILTERS( LEFT( BothBOM[ItemCode], 4 ) = "0502" )
    )
VAR test11 =
    SUMX(
        SUMMARIZE(
            FILTER(
                ALLEXCEPT( BothBOM, BothBOM[ProductCode] ),
                BothBOM[ItemCode] = "0501-2020"
                    || BothBOM[ItemCode] = "0501-2014"
            ),
            "total2",
                CALCULATE(
                    SUMX( BothBOM, BothBOM[NewCost] ),
                    BothBOM[ItemCode] = "0501-2020"
                )
                    - CALCULATE(
                        SUMX( BothBOM, BothBOM[OldCost] ),
                        BothBOM[ItemCode] = "0501-2014"
                    )
        ),
        [total2]
    )
RETURN
    IF( NOT ( HASONEVALUE( BothBOM[ItemCode] ) ), test11 + test9, test9 + test10 )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in summary, in senario B (VAR=test10), I have to&amp;nbsp; calculate&amp;nbsp; for ([ItemCode]="0502-2020") as :&lt;/P&gt;&lt;P&gt;([ItemCode]="0502-2020") minus ([ItemCode]="0501-2014") e.i (433.000-146.000=287.000)&lt;/P&gt;&lt;P&gt;&amp;nbsp;and VAR=test11 stands for calculating subtotals in different context.&lt;/P&gt;&lt;P&gt;the outcome of these DAX is as expected, but I am wondering if there might be a better solution and better DAX coding?Additionally , I'm curious about the efficeny of this code. specially for VAR=test11 by which I've handeled subtotals for the Measure.&lt;/P&gt;</description>
    <pubDate>Tue, 12 Sep 2023 08:25:32 GMT</pubDate>
    <dc:creator>HamidRezaSajjad</dc:creator>
    <dc:date>2023-09-12T08:25:32Z</dc:date>
    <item>
      <title>Optimized DAX code for calculating on different Filter Context</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimized-DAX-code-for-calculating-on-different-Filter-Context/m-p/3425839#M129757</link>
      <description>&lt;P&gt;Hello. I've encountered a Sencario in Which I have to calculate each Value in different FC(Filter Context) based on predefiend Scenarios of A and B.&lt;/P&gt;&lt;P&gt;here is my Data Structure in Excel.&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in above image, [OldCost] and [NewCost] are two &lt;U&gt;Calculated Columns&lt;/U&gt; and are the result of [Last Purchase Rate]*[Standard Consuption Rate] and &lt;EM&gt;A&lt;/EM&gt; and &lt;EM&gt;B&lt;/EM&gt; are two &lt;U&gt;Measures &lt;/U&gt;calculated by DAX by the following Code snippet for &lt;EM&gt;B&lt;/EM&gt;:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;=
VAR test10 =
    IF(
        MAX( BothBOM[ItemCode] ) &amp;lt;&amp;gt; "0501-2020",
        0,
        SUMX(
            SUMMARIZE(
                FILTER(
                    ALLEXCEPT( BothBOM, BothBOM[ProductCode] ),
                    BothBOM[ItemCode] = "0501-2020"
                        || BothBOM[ItemCode] = "0501-2014"
                ),
                "total",
                    CALCULATE(
                        SUMX( BothBOM, BothBOM[NewCost] ),
                        BothBOM[ItemCode] = "0501-2020"
                    )
                        - CALCULATE(
                            SUMX( BothBOM, BothBOM[OldCost] ),
                            BothBOM[ItemCode] = "0501-2014"
                        )
            ),
            [total]
        )
    )
VAR test9 =
    CALCULATE(
        SUMX( BothBOM, BothBOM[OldCost] ),
        KEEPFILTERS( LEFT( BothBOM[ItemCode], 4 ) = "0502" )
    )
VAR test11 =
    SUMX(
        SUMMARIZE(
            FILTER(
                ALLEXCEPT( BothBOM, BothBOM[ProductCode] ),
                BothBOM[ItemCode] = "0501-2020"
                    || BothBOM[ItemCode] = "0501-2014"
            ),
            "total2",
                CALCULATE(
                    SUMX( BothBOM, BothBOM[NewCost] ),
                    BothBOM[ItemCode] = "0501-2020"
                )
                    - CALCULATE(
                        SUMX( BothBOM, BothBOM[OldCost] ),
                        BothBOM[ItemCode] = "0501-2014"
                    )
        ),
        [total2]
    )
RETURN
    IF( NOT ( HASONEVALUE( BothBOM[ItemCode] ) ), test11 + test9, test9 + test10 )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in summary, in senario B (VAR=test10), I have to&amp;nbsp; calculate&amp;nbsp; for ([ItemCode]="0502-2020") as :&lt;/P&gt;&lt;P&gt;([ItemCode]="0502-2020") minus ([ItemCode]="0501-2014") e.i (433.000-146.000=287.000)&lt;/P&gt;&lt;P&gt;&amp;nbsp;and VAR=test11 stands for calculating subtotals in different context.&lt;/P&gt;&lt;P&gt;the outcome of these DAX is as expected, but I am wondering if there might be a better solution and better DAX coding?Additionally , I'm curious about the efficeny of this code. specially for VAR=test11 by which I've handeled subtotals for the Measure.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2023 08:25:32 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimized-DAX-code-for-calculating-on-different-Filter-Context/m-p/3425839#M129757</guid>
      <dc:creator>HamidRezaSajjad</dc:creator>
      <dc:date>2023-09-12T08:25:32Z</dc:date>
    </item>
    <item>
      <title>Re: Optimized DAX code for calculating on different Filter Context</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimized-DAX-code-for-calculating-on-different-Filter-Context/m-p/3435449#M130343</link>
      <description>&lt;P&gt;Any Idea?&lt;/P&gt;</description>
      <pubDate>Mon, 18 Sep 2023 07:02:16 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimized-DAX-code-for-calculating-on-different-Filter-Context/m-p/3435449#M130343</guid>
      <dc:creator>HamidRezaSajjad</dc:creator>
      <dc:date>2023-09-18T07:02:16Z</dc:date>
    </item>
  </channel>
</rss>

