Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
smpa01
Super User
Super User

LINEST forEach

@AlexisOlson @bcdobbs @CNENFRNL 

 

I am trying to crerate a measure using LINEST that would give me the slope but I have not been successful.

 

PFA what I have tried.

 

Following is what I desire

| Country | x | y | slope |
|---------|---|---|-------|
| C1      | 1 | 3 | 1.75  |
| C1      | 2 | 5 | 1.75  |
| C1      | 3 | 7 | 1.75  |
| C2      | 2 | 3 | 2.25  |
| C2      | 3 | 6 | 2.25  |
| C2      | 4 | 8 | 2.25  |

 

To the best of my knowledge, I am passing on the correct filtered table to LINEST

smpa01_0-1685116021845.png

 

 

Thank you in advance

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
2 ACCEPTED SOLUTIONS
v-jianboli-msft
Community Support
Community Support

Hi @smpa01 ,

 

Please try:

Measure = 
var _a = FILTER(ALL('fact'),'fact'[Country]=MAX('fact'[Country]))
return MAXX(CALCULATETABLE(LINEST('fact'[y],'fact'[x]),_a),[Slope1])

Final output:

vjianbolimsft_0-1685329524047.png

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

@smpa01 You could create a measure that returns a text table using TOCSV and parse that.

 

For example:

LINEST Output = 
VAR _filteredTable_ = FILTER ( ALL ( 'fact' ), 'fact'[Country] = MAX ( 'fact'[Country] ) )
VAR _linEst_ = LINESTX ( _filteredTable_, 'fact'[y], 'fact'[x] )
VAR _toText = TOCSV ( _linEst_, , "|", FALSE() )
RETURN
    _toText

 

Then you can define the following:

Slope1 = PATHITEM ( [LINEST Output], 1 )
ResidualSumOfSquares = PATHITEM ( [LINEST Output], 10 )

View solution in original post

5 REPLIES 5
v-jianboli-msft
Community Support
Community Support

Hi @smpa01 ,

 

Please try:

Measure = 
var _a = FILTER(ALL('fact'),'fact'[Country]=MAX('fact'[Country]))
return MAXX(CALCULATETABLE(LINEST('fact'[y],'fact'[x]),_a),[Slope1])

Final output:

vjianbolimsft_0-1685329524047.png

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-jianboli-msft  just being curious here. So if I need Total Sum of Square, Residual Sum of Square and R Square, I need to create 3 seperate measures (TSS, RSS and RSquare) and then create subsequent measures for each  ( incorect TSS, RSS, RSquare) immediately created measures like you did above to derive at the correct TSS, RSS, RSquare value for the filter context?

 

@jeffrey_wang 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

You have to create different measures, one for each statistic you need. There is currently no way to create a DAX function that returns a table value to be shared by multiple measures.

@smpa01 You could create a measure that returns a text table using TOCSV and parse that.

 

For example:

LINEST Output = 
VAR _filteredTable_ = FILTER ( ALL ( 'fact' ), 'fact'[Country] = MAX ( 'fact'[Country] ) )
VAR _linEst_ = LINESTX ( _filteredTable_, 'fact'[y], 'fact'[x] )
VAR _toText = TOCSV ( _linEst_, , "|", FALSE() )
RETURN
    _toText

 

Then you can define the following:

Slope1 = PATHITEM ( [LINEST Output], 1 )
ResidualSumOfSquares = PATHITEM ( [LINEST Output], 10 )

Thanks @AlexisOlson , applied here 

If a linear regreesion is required but only for viz purpose there are tons (I literally mean tons) of javascript library that can do the job at the front-end while developing cust viz and there won't be any DAX dependency.

 

However, if the linear regression is required as part of the SSAS dataset then DAX is the only option and this is a really nice trick here.

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.