Join us for an expert-led overview of the tools and concepts you'll need to pass exam PL-300. The first session starts on June 11th. See you there!
Get registeredPower BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.
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.
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])) && 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])) && 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]
)
Solved! Go to Solution.
Thanks @DoctorYSG, understood 🙂
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 DETAILROWS
function as described in this article:
https://www.sqlbi.com/articles/creating-table-functions-in-dax-using-detailrows/
Notes:
DETAILROWS
beyond what it is designed for.The basic idea with DETAILROWS
is to:
Dummy = BLANK ( )
Dummy
measure's Detail Rows Expression to the desired table expression.DETAILROWS ( [Dummy] )
to return the table.Sample Detail Rows Expression:
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] ) ) && NOT ( ISBLANK ( [Y] ) )
)
Would this work for you with your set of measures?
Hi @DoctorYSG
I'm assuming you require a self-contained DAX query.
You don't necessarily need to define measures within the query. You could use GENERATE
instead of ADDCOLUMNS
, which allows the extension columns to reference common variables. You can then evaluate cTable
as one of those variables, and make use of LINESTX
to simplify the code.
For example, you could write this query:
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] ) ) && 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 )
)
Would a query like this work for you?
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).
In any case when the measure is in chart, it looks like this:
Thanks @DoctorYSG, understood 🙂
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 DETAILROWS
function as described in this article:
https://www.sqlbi.com/articles/creating-table-functions-in-dax-using-detailrows/
Notes:
DETAILROWS
beyond what it is designed for.The basic idea with DETAILROWS
is to:
Dummy = BLANK ( )
Dummy
measure's Detail Rows Expression to the desired table expression.DETAILROWS ( [Dummy] )
to return the table.Sample Detail Rows Expression:
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] ) ) && NOT ( ISBLANK ( [Y] ) )
)
Would this work for you with your set of measures?
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
16 | |
13 | |
12 | |
11 | |
11 |
User | Count |
---|---|
19 | |
14 | |
14 | |
11 | |
9 |