<?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-use of VAR between two MEASURES in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Re-use-of-VAR-between-two-MEASURES/m-p/4735119#M181369</link>
    <description>&lt;P&gt;I am having to duplicate the calculation of a Summarize table between two measures. I tried putting the VAR outside of both, then I loose the Filter context when using the Measure. Only when the VAR is in both measures does this work properly. It would be nice to simplify this.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;DEFINE
	MEASURE 'Measure Table'[TokenCount] =
		VAR cTable =
		FILTER(
			ADDCOLUMNS(
				SUMMARIZE(
					'Traces',
					'Traces'[CorrID],
					'Traces'[DateStamp]
				),
				"X", CALCULATE(
					AVERAGE('Traces'[ResponseTime]),
					'Traces'[AppName] = "Microsoft Outlook"
				),
				"Y", CALCULATE(
					AVERAGE('Traces'[ResponseTime]),
					'Traces'[AppName] = "AAD Token Broker Plugin"
				)
			),
			NOT (ISBLANK([X])) &amp;amp;&amp;amp; NOT (ISBLANK([Y]))
		)
		RETURN
			COUNTROWS(cTable)
	MEASURE 'Measure Table'[rawCorrToken] =
		VAR cTable =
		FILTER(
			ADDCOLUMNS(
				SUMMARIZE(
					'Traces',
					'Traces'[CorrID],
					'Traces'[DateStamp]
				),
				"X", CALCULATE(
					AVERAGE('Traces'[ResponseTime]),
					'Traces'[AppName] = "Microsoft Outlook"
				),
				"Y", CALCULATE(
					AVERAGE('Traces'[ResponseTime]),
					'Traces'[AppName] = "AAD Token Broker Plugin"
				)
			),
			NOT (ISBLANK([X])) &amp;amp;&amp;amp; NOT (ISBLANK([Y]))
		)
		VAR Count_Items = COUNTROWS(cTable)
		VAR Sum_X = SUMX(
			cTable,
			[X]
		)
		VAR Sum_X2 = SUMX(
			cTable,
			[X] ^ 2
		)
		VAR Sum_Y = SUMX(
			cTable,
			[Y]
		)
		VAR Sum_Y2 = SUMX(
			cTable,
			[Y] ^ 2
		)
		VAR Sum_XY = SUMX(
			cTable,
			[X] * [Y]
		)
		VAR Pearson_Numerator = Count_Items * Sum_XY - Sum_X * Sum_Y
		VAR Pearson_Denominator_X = Count_Items * Sum_X2 - Sum_X ^ 2
		VAR Pearson_Denominator_Y = Count_Items * Sum_Y2 - Sum_Y ^ 2
		VAR Pearson_Denominator = SQRT(Pearson_Denominator_X * Pearson_Denominator_Y)
		VAR TokenCorr =
		DIVIDE(
			Pearson_Numerator,
			Pearson_Denominator
		)
		RETURN
			TokenCorr
EVALUATE
	ADDCOLUMNS(
		SUMMARIZE(
			'Traces',
			'Traces'[BaseName]
		),
		"Correlation", [rawCorrToken],
		"Count", [TokenCount]
	)&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 17 Jun 2025 18:43:23 GMT</pubDate>
    <dc:creator>DoctorYSG</dc:creator>
    <dc:date>2025-06-17T18:43:23Z</dc:date>
    <item>
      <title>Re-use of VAR between two MEASURES</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Re-use-of-VAR-between-two-MEASURES/m-p/4735119#M181369</link>
      <description>&lt;P&gt;I am having to duplicate the calculation of a Summarize table between two measures. I tried putting the VAR outside of both, then I loose the Filter context when using the Measure. Only when the VAR is in both measures does this work properly. It would be nice to simplify this.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;DEFINE
	MEASURE 'Measure Table'[TokenCount] =
		VAR cTable =
		FILTER(
			ADDCOLUMNS(
				SUMMARIZE(
					'Traces',
					'Traces'[CorrID],
					'Traces'[DateStamp]
				),
				"X", CALCULATE(
					AVERAGE('Traces'[ResponseTime]),
					'Traces'[AppName] = "Microsoft Outlook"
				),
				"Y", CALCULATE(
					AVERAGE('Traces'[ResponseTime]),
					'Traces'[AppName] = "AAD Token Broker Plugin"
				)
			),
			NOT (ISBLANK([X])) &amp;amp;&amp;amp; NOT (ISBLANK([Y]))
		)
		RETURN
			COUNTROWS(cTable)
	MEASURE 'Measure Table'[rawCorrToken] =
		VAR cTable =
		FILTER(
			ADDCOLUMNS(
				SUMMARIZE(
					'Traces',
					'Traces'[CorrID],
					'Traces'[DateStamp]
				),
				"X", CALCULATE(
					AVERAGE('Traces'[ResponseTime]),
					'Traces'[AppName] = "Microsoft Outlook"
				),
				"Y", CALCULATE(
					AVERAGE('Traces'[ResponseTime]),
					'Traces'[AppName] = "AAD Token Broker Plugin"
				)
			),
			NOT (ISBLANK([X])) &amp;amp;&amp;amp; NOT (ISBLANK([Y]))
		)
		VAR Count_Items = COUNTROWS(cTable)
		VAR Sum_X = SUMX(
			cTable,
			[X]
		)
		VAR Sum_X2 = SUMX(
			cTable,
			[X] ^ 2
		)
		VAR Sum_Y = SUMX(
			cTable,
			[Y]
		)
		VAR Sum_Y2 = SUMX(
			cTable,
			[Y] ^ 2
		)
		VAR Sum_XY = SUMX(
			cTable,
			[X] * [Y]
		)
		VAR Pearson_Numerator = Count_Items * Sum_XY - Sum_X * Sum_Y
		VAR Pearson_Denominator_X = Count_Items * Sum_X2 - Sum_X ^ 2
		VAR Pearson_Denominator_Y = Count_Items * Sum_Y2 - Sum_Y ^ 2
		VAR Pearson_Denominator = SQRT(Pearson_Denominator_X * Pearson_Denominator_Y)
		VAR TokenCorr =
		DIVIDE(
			Pearson_Numerator,
			Pearson_Denominator
		)
		RETURN
			TokenCorr
EVALUATE
	ADDCOLUMNS(
		SUMMARIZE(
			'Traces',
			'Traces'[BaseName]
		),
		"Correlation", [rawCorrToken],
		"Count", [TokenCount]
	)&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jun 2025 18:43:23 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Re-use-of-VAR-between-two-MEASURES/m-p/4735119#M181369</guid>
      <dc:creator>DoctorYSG</dc:creator>
      <dc:date>2025-06-17T18:43:23Z</dc:date>
    </item>
    <item>
      <title>Re: Re-use of VAR between two MEASURES</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Re-use-of-VAR-between-two-MEASURES/m-p/4735219#M181379</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="641031" data-lia-user-login="DoctorYSG" class="lia-mention lia-mention-user"&gt;DoctorYSG&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm assuming you require a self-contained DAX query.&lt;/P&gt;
&lt;P&gt;You don't necessarily need to define measures within the query. You could use &lt;CODE&gt;GENERATE&lt;/CODE&gt; instead of &lt;CODE&gt;ADDCOLUMNS&lt;/CODE&gt;, which allows the extension columns to reference common variables. You can then evaluate &lt;CODE&gt;cTable&lt;/CODE&gt; as one of those variables, and make use of &lt;CODE&gt;LINESTX&lt;/CODE&gt; to simplify the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, you could write this query:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;EVALUATE
GENERATE (
    SUMMARIZE ( 'Traces', 'Traces'[BaseName] ),
    VAR cTable =
        CALCULATETABLE (
            FILTER (
                SELECTCOLUMNS (
                    SUMMARIZE ( 'Traces', 'Traces'[CorrID], 'Traces'[DateStamp] ),
                    "X",
                        CALCULATE (
                            AVERAGE ( 'Traces'[ResponseTime] ),
                            'Traces'[AppName] = "Microsoft Outlook"
                        ),
                    "Y",
                        CALCULATE (
                            AVERAGE ( 'Traces'[ResponseTime] ),
                            'Traces'[AppName] = "AAD Token Broker Plugin"
                        )
                ),
                NOT ( ISBLANK ( [X] ) ) &amp;amp;&amp;amp; NOT ( ISBLANK ( [Y] ) )
            )
        )
    VAR TokenCount =
        COUNTROWS ( cTable )
    VAR LeastSquaresResult =
        LINESTX ( cTable, [Y], [X] )
    VAR rawCorrToken =
        SQRT ( SELECTCOLUMNS ( LeastSquaresResult, [CoefficientOfDetermination] ) )
    RETURN
        ROW ( "Correlation", rawCorrToken, "Count", TokenCount )
)&lt;/LI-CODE&gt;
&lt;P&gt;Would a query like this work for you?&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jun 2025 21:37:51 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Re-use-of-VAR-between-two-MEASURES/m-p/4735219#M181379</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2025-06-17T21:37:51Z</dc:date>
    </item>
    <item>
      <title>Re: Re-use of VAR between two MEASURES</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Re-use-of-VAR-between-two-MEASURES/m-p/4735225#M181380</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="6799" data-lia-user-login="OwenAuger" class="lia-mention lia-mention-user"&gt;OwenAuger&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A very reasonable assumption. I should have given more context for the problem I am trying to solve. I am trying to compare multiple time series (streams) for correlation. So, no, it is not a self-contained measure, but rather one that can be embedded in different filter contexts. I want both the Pearson's Correlation and the Count (I will work on Spearman's later which I am told is better for non-linear).&lt;BR /&gt;&lt;BR /&gt;In any case when the measure is in chart, it looks like this:&lt;BR /&gt;&lt;BR /&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;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jun 2025 22:00:08 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Re-use-of-VAR-between-two-MEASURES/m-p/4735225#M181380</guid>
      <dc:creator>DoctorYSG</dc:creator>
      <dc:date>2025-06-17T22:00:08Z</dc:date>
    </item>
    <item>
      <title>Re: Re-use of VAR between two MEASURES</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Re-use-of-VAR-between-two-MEASURES/m-p/4735305#M181381</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="641031" data-lia-user-login="DoctorYSG" class="lia-mention lia-mention-user"&gt;DoctorYSG&lt;/a&gt;, understood &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;If the objective is to avoid repeating the code for the table expression across multiple measures, the only method I am aware of is by using the &lt;CODE&gt;DETAILROWS&lt;/CODE&gt; function as described in this article:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.sqlbi.com/articles/creating-table-functions-in-dax-using-detailrows/" target="_blank" rel="noopener"&gt;https://www.sqlbi.com/articles/creating-table-functions-in-dax-using-detailrows/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Notes:&lt;/STRONG&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;While this technique works, it uses &lt;CODE&gt;DETAILROWS&lt;/CODE&gt; beyond what it is designed for.&lt;/LI&gt;
&lt;LI&gt;The upcoming &lt;A href="https://www.sqlbi.com/blog/marco/2025/04/04/dax-and-semantic-models-announcements-at-the-fabric-conference-2025/#:~:text=time%20intelligence%20measures.-,User%2Ddefined%20functions%20(UDF)%20in%20DAX,-The%20last%20announcement" target="_blank" rel="noopener"&gt;DAX user-defined functions feature&lt;/A&gt; will allow table-valued functions and would be a preferable technique once available &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The basic idea with &lt;CODE&gt;DETAILROWS&lt;/CODE&gt; is to:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a dummy measure such as &lt;CODE&gt;Dummy = BLANK ( )&lt;/CODE&gt;&lt;/LI&gt;
&lt;LI&gt;Using Tabular Editor (or TMDL view) set the &lt;CODE&gt;Dummy&lt;/CODE&gt; measure's Detail Rows Expression to the desired table expression.&lt;/LI&gt;
&lt;LI&gt;Where required in other measures, use &lt;CODE&gt;DETAILROWS ( [Dummy] )&lt;/CODE&gt; to return the table.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Sample Detail Rows Expression:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;FILTER (
    SELECTCOLUMNS (
        SUMMARIZE ( 'Traces', 'Traces'[CorrID], 'Traces'[DateStamp] ),
        "X",
            CALCULATE (
                AVERAGE ( 'Traces'[ResponseTime] ),
                'Traces'[AppName] = "Microsoft Outlook"
            ),
        "Y",
            CALCULATE (
                AVERAGE ( 'Traces'[ResponseTime] ),
                'Traces'[AppName] = "AAD Token Broker Plugin"
            )
    ),
    NOT ( ISBLANK ( [X] ) ) &amp;amp;&amp;amp; NOT ( ISBLANK ( [Y] ) )
)&lt;/LI-CODE&gt;
&lt;P&gt;Would this work for you with your set of measures?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jun 2025 02:29:04 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Re-use-of-VAR-between-two-MEASURES/m-p/4735305#M181381</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2025-06-18T02:29:04Z</dc:date>
    </item>
  </channel>
</rss>

