Forum Discussion
Dynamically select column based on slicer for regression/correlation calculation
Hi Anonymous
Here I'm using Field Parameters for dynamic selection of the X-Axis and Y-Axis. However, when trying to calculate the slope and intersection dynamically I hit the wall :-DI have a strong feeling that there should be a much simpler way to accomplish the same but this is what I was able to accomplish so far.
Worth mentioning that with hundreds of columns and thousands of rows this solution might not be feasible in terms of performance.
Also note that I don't have very strong visualization skills and I didn't know how how to add the manual trend line to the scattered plot so I just overlapped it with a line chart. Please do not laugh at that 🙂
Greg_Deckler Appreciate your input on this. Was that over thinking or it is just what it is?
Regression Value =
VAR XColumn = MAX ( 'X-Axis'[Parameter] )
VAR YColumn = MAX ( 'Y_Axis'[Parameter] )
VAR T = ALLSELECTED ( 'Table' )
VAR NumberOfRows = COUNTROWS ( T )
VAR String1 = TOCSV ( T, -1, ",", TRUE )
VAR Items1 = SUBSTITUTE ( String1, UNICHAR ( 10 ), "|" )
VAR T1 = GENERATESERIES ( 1, NumberOfRows, 1 )
VAR T2 = SELECTCOLUMNS ( T1, "@Headers", PATHITEM ( Items1, 1 ), "@Details", PATHITEM ( Items1, [Value] + 1 ) )
VAR T3 =
GENERATE (
T2,
VAR HeaderString = [@Headers]
VAR HeaderItems = SUBSTITUTE ( HeaderString, ",", "|" )
VAR DetailString = [@Details]
VAR DetailItems = SUBSTITUTE ( DetailString, ",", "|" )
VAR Length = PATHLENGTH ( HeaderItems )
VAR T4 = GENERATESERIES ( 1, Length, 1 )
VAR T5 = SELECTCOLUMNS ( T4, "@Header1", PATHITEM ( HeaderItems, [Value] ), "@Detail1", PATHITEM ( DetailItems, [Value] ) )
VAR T6 = SELECTCOLUMNS ( T4, "@Header2", PATHITEM ( HeaderItems, [Value] ), "@Detail2", PATHITEM ( DetailItems, [Value] ) )
VAR T7 = FILTER ( T5, [@Header1] = "'Table'[" & XColumn & "]" )
VAR T8 = FILTER ( T6, [@Header2] = "'Table'[" & YColumn & "]" )
RETURN
CROSSJOIN ( T7, T8 )
)
VAR LinestTable = LINESTX ( T3, [@Detail2], [@Detail1] )
VAR Slope = MAXX ( LinestTable, [Slope1] )
VAR Intercept = MAXX ( LinestTable, [Intercept] )
VAR X =
MAXX (
'Table',
VAR T1 = CALCULATETABLE ( 'Table' )
VAR String1 = TOCSV ( T1, 1, ",", TRUE )
VAR Items1 = SUBSTITUTE ( String1, UNICHAR ( 10 ), "|" )
VAR T2 = SELECTCOLUMNS ( { ( 1, 2 ) }, "@Headers", PATHITEM ( Items1, 1 ), "@Details", PATHITEM ( Items1, 2 ) )
VAR T3 =
GENERATE (
T2,
VAR HeaderString = [@Headers]
VAR HeaderItems = SUBSTITUTE ( HeaderString, ",", "|" )
VAR DetailString = [@Details]
VAR DetailItems = SUBSTITUTE ( DetailString, ",", "|" )
VAR Length = PATHLENGTH ( HeaderItems )
VAR T4 = GENERATESERIES ( 1, Length, 1 )
RETURN
FILTER (
SELECTCOLUMNS (
T4,
"@Header", PATHITEM ( HeaderItems, [Value] ),
"@Detail", PATHITEM ( DetailItems, [Value] )
),
[@Header] = "'Table'[" & XColumn & "]"
)
)
RETURN
MAXX ( T3, VALUE ( [@Detail] ) )
)
RETURN
Slope * X + Intercept
Hi tamerj1,
Great solution that you provided.
Also I need some suggesions. I am explaining what I did.
I used header as x-axis & y-axis. Then created measures using SWITCH DAX for column selection at both axis. Now, Simply I used craydec regression chart which is working fine, showing equation perfect but in pictorial view. So that finally we have to use LINEST/LINESTX dax.
So, can we create LINEST/LINESTX DAX using measures, otherway what you did.
- tamerj12 years ago
Community Champion
Anonymous
Yes probably you can but
1. I cannot 100% confirm without having the sample file. You share a download link of it.
2. Not sure if having a switch statement of 400 columns is practical. What happens if you add or delete columns? The code would break.
- Anonymous2 years agoNot applicable
Hi tamerj1 ,
One more small thing needs to know, is it option in field parameterto to modify/add more field in case of addition more field in data base. Also can we select all column of table at a time or have to select one by one.- tamerj12 years ago
Community Champion
Anonymous
That needs to be done manually with field parameters. The advantage of field parameters is that they are simple to create and and are aboe to dynamicaly change the tye display name as per the selected column or measure. However, I believe other solution that utilizes DAX to create something similar to a parameter table could be possible but way more complicated.