Forum Discussion

Nerf_Herder's avatar
Nerf_Herder
Frequent Visitor
3 years ago
Solved

Using DIVIDE() with LAG()

I'm hoping you good folk can help as I feel like I've hit a wall.

 

I've created a calculated table with two columns 'Date' & 'Costs'. I'm trying to create a new column for the growth, from the previous row to the current row. In excel, it's an easy ([CURRENT]-[PREV])/[PREV].

 

I've tried the following, but neither work and I can't find anything on this at all:

  • Growth = DIVIDE('Table'[Costs] - LAG('Table'[Costs]), LAG('Table'[Costs]))
      Error: The syntax for 'LAG' is incorrect. (DAX(DIVIDE('Table'[Costs] - LAG('Table'[Costs]), LAG('Table'[Costs])))).
  • Growth = DIVIDE('Table'[Costs] - LAG('Table'[Costs],1), LAG('Table'[Costs],1))
      Error: The syntax for 'LAG' is incorrect. (DAX(DIVIDE('Table'[Costs] - LAG('Table'[Costs],1), LAG('Table'[Costs],1)))).
The table I'm trying to create should look like this, but it's just not happening (screenshot below).
DateCostsGrowth
2022-04£316,742.580.00%
2022-05£339,554.797.20%
2022-06£318,563.10-6.18%
2022-07£328,800.013.21%
2022-08£336,192.012.25%
2022-09£335,042.44-0.34%
2022-10£364,598.188.82%
2022-11£347,276.07-4.75%
2022-12£349,602.020.67%
2023-01£353,896.811.23%
2023-02£329,560.58-6.88%
2023-03£326,710.33-0.86%

 

 

Any help would be greatly appreciated.

Thank you in advance!

  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi Nerf_Herder ,

     

    According to your description, here are my steps you can follow as a solution.

    (1) My test data is the same as yours.

    (2) Click "transform data" to enter the power query editor, change the data type of the [Costs] column to "Decimal number" type, sort the date column in ascending order, and then add the index column.

    (3)Click "Close and Apply" to go back to desktop and create a calculated column.

    Column = 
    var _current='Table'[Costs]
    var _prev=CALCULATE(SUM('Table'[Costs]),FILTER('Table','Table'[Index]=EARLIER('Table'[Index])-1))
    return DIVIDE(_current-_prev,_prev,0)

    (4) Then the result is as follows.

    If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. Thank you.

     

    Best Regards,

    Neeko Tang

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

2 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Nerf_Herder ,

     

    According to your description, here are my steps you can follow as a solution.

    (1) My test data is the same as yours.

    (2) Click "transform data" to enter the power query editor, change the data type of the [Costs] column to "Decimal number" type, sort the date column in ascending order, and then add the index column.

    (3)Click "Close and Apply" to go back to desktop and create a calculated column.

    Column = 
    var _current='Table'[Costs]
    var _prev=CALCULATE(SUM('Table'[Costs]),FILTER('Table','Table'[Index]=EARLIER('Table'[Index])-1))
    return DIVIDE(_current-_prev,_prev,0)

    (4) Then the result is as follows.

    If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. Thank you.

     

    Best Regards,

    Neeko Tang

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

    • Nerf_Herder's avatar
      Nerf_Herder
      Frequent Visitor

      Hi Anonymous,

       

      That worked a treat, thank you so much for your help!

       

      Cheers,

      Ian.