Forum Discussion

mogugu_84's avatar
mogugu_84
Helper I
1 year ago
Solved

replicate Excel SLOPE calculation

Hi Community

 

I need to re-create this Excel formula calculation (Slope) in Power BI. 

So basically the known x is number from 1 to 24 represent 24 months of data, from month 12, the slope calculation is known y = the unit value in row 3 for the past 12 columns and the known x = the sequence number in row 1 for the past 12 columns

as shown in the below Excel formula bar, any idea how to calculation this figure using DAX? Thanks

 

 

 

 

 

  • Hi, Please check the below picture and the attached pbix file.

     

     

     

     

     

    Volume total: = 
    SUM( Data[Volume] )

     

     

    WINDOW function (DAX) - DAX | Microsoft Learn

     

     

    Slope: = 
    VAR _calendar =
        ALL ( 'Calendar'[Year-Month], 'Calendar'[Year-Month sort] )
    VAR _window =
        WINDOW (
            -11,
            REL,
            0,
            REL,
            _calendar,
            ORDERBY ( 'Calendar'[Year-Month sort], ASC )
        )
    VAR _condition =
        COUNTROWS ( FILTER ( _window, [Volume total:] <> BLANK () ) ) = 12
    VAR _t =
        ADDCOLUMNS (
            _window,
            "@x", RANK ( SKIP, _calendar, ORDERBY ( 'Calendar'[Year-Month sort], ASC ) ),
            "@y", [Volume total:]
        )
    VAR _linestx =
        LINESTX ( _t, [@y], [@x] )
    RETURN
        IF ( _condition, MAXX ( _linestx, [Slope1] ) )
    

     

     

9 Replies

    • Sdnf's avatar
      Sdnf
      Regular Visitor

      Hi,

      I'd also need exactly how to recreate this slope formula in power BI, could you please write the complete DAX formula for this request?:

       

       I need to re-create this Excel formula calculation (Slope) in Power BI. 

      So basically the known x is number from 1 to 24 represent 24 months of data, from month 12, the slope calculation is known y = the unit value in row 3 for the past 12 columns and the known x = the sequence number in row 1 for the past 12 columns

      as shown in the below Excel formula bar, any idea how to calculation this figure using DAX? Thanks

       

       

  • Hi,

    The image is too small for me. Even if I click the image to make it larger, it is still too small.

    Could you please provide your sample pbix file's link & how the expected result looks like? And then, I can try to look into it.

    Thank you.

    • mogugu_84's avatar
      mogugu_84
      Helper I

      thank you Kim. I realise i can't upload a  file or store somewhere online with link...i have pasted the actual figures as below, if this is not handy, you can use dummy numbers as well, doesnt matter. 

       

      the key desired the outcome is to create a measure in Power BI called slope and the calculation of Slope in Sep 22 should be 35.8, as the Excel forumla spit out. and Oct 22 slope should be 20.2 ....

       

      i have repasted the bigger photo with what's the Excel forumla looks like

       

      if you use dummy data, as long as it match same data using the Excel "Slope" formula" calculation, it would be all perfect to adapt. Thank you

       

       123456789101112131415161718192021222324    
      MonthOct.21Nov.21Dec.21Jan.22Feb.22Mar.22Apr.22May.22Jun.22Jul.22Aug.22Sep.22Oct.22Nov.22Dec.22Jan.23Feb.23Mar.23Apr.23May.23Jun.23Jul.23Aug.23Sep.23    
      Volume2,8283,2473,3132,7752,9123,2992,9253,1493,1933,1723,5243,4023,1143,3733,5603,0323,1643,3843,2273,7193,3233,5783,6693,490    

       

       

       

       

      • Jihwan_Kim's avatar
        Jihwan_Kim
        Super User

        Hi, Please check the below picture and the attached pbix file.

         

         

         

         

         

        Volume total: = 
        SUM( Data[Volume] )

         

         

        WINDOW function (DAX) - DAX | Microsoft Learn

         

         

        Slope: = 
        VAR _calendar =
            ALL ( 'Calendar'[Year-Month], 'Calendar'[Year-Month sort] )
        VAR _window =
            WINDOW (
                -11,
                REL,
                0,
                REL,
                _calendar,
                ORDERBY ( 'Calendar'[Year-Month sort], ASC )
            )
        VAR _condition =
            COUNTROWS ( FILTER ( _window, [Volume total:] <> BLANK () ) ) = 12
        VAR _t =
            ADDCOLUMNS (
                _window,
                "@x", RANK ( SKIP, _calendar, ORDERBY ( 'Calendar'[Year-Month sort], ASC ) ),
                "@y", [Volume total:]
            )
        VAR _linestx =
            LINESTX ( _t, [@y], [@x] )
        RETURN
            IF ( _condition, MAXX ( _linestx, [Slope1] ) )
        

         

         

  • Sdnf's avatar
    Sdnf
    Regular Visitor

    Thanks a lot Jihwan Kim, your solution for the slope works perfectly!