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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
touseef2660
Helper I
Helper I

Add a column value with max of previous rows of current column

Hello, I want to write a calculated column dax in which I need to add other column  value with current column previous rows max value. Check this example in excel: 

touseef2660_0-1699608872689.png

In this, I am adding column A (Gross) value with the max of previous rows of column B.  Is this possible in Power BI calculated column?
I tried a formula given by @Dangar332 but it is not giving correct values. Check below:

touseef2660_1-1699609300262.png

 

 You can find the sample files here: Power BI

Please help me with this tricky problem. Thanks in advance

@amitchandak @Ashish_Mathur 

1 ACCEPTED SOLUTION

Your Excel formula is wrong.  Should say $B$2.

 

I don't think you can do this in DAX as this would require a self referencing MAXX condition inside a SUMX.

 

It is relatively trivial in Power Query as List.Accumulate allows for the manipulation of intermediate results.

 

 

 

let
  Source = Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText("i45WMlCK1YlWMgaTpmDSCEzqGoIpZNIEpjQWAA==", BinaryEncoding.Base64), 
        Compression.Deflate
      )
    ), 
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [Gross = _t]
  ), 
  #"Changed Type" = Table.TransformColumnTypes(Source, {{"Gross", Int64.Type}}), 
  #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type), 
  #"Added Custom" = Table.AddColumn(
    #"Added Index", 
    "Calculation", 
    each List.Accumulate(
      {0 .. [Index]}, 
      [s = 0, m = 0], 
      (state, current) => [
        s = #"Added Index"[Gross]{current} + state[m], 
        m = List.Max({#"Added Index"[Gross]{current} + state[m], state[m]})
      ]
    )
  ), 
  #"Expanded Calculation" = Table.ExpandRecordColumn(
    #"Added Custom", 
    "Calculation", 
    {"s"}, 
    {"Calculation"}
  ), 
  #"Removed Other Columns" = Table.SelectColumns(#"Expanded Calculation", {"Gross", "Calculation"}), 
  #"Changed Type1" = Table.TransformColumnTypes(
    #"Removed Other Columns", 
    {{"Calculation", Int64.Type}}
  )
in
  #"Changed Type1"

 

 

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

View solution in original post

9 REPLIES 9
touseef2660
Helper I
Helper I

@Greg_Deckler  Can you help me in this problem please?

Pretty certain @lbendlin is correct on this one. What you have here is something that requires a "previous value" of a calculation and that's just not possible in DAX as DAX despises anything that even smells like recursion. 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

I got it. Thank you for responding. I really appreciate it. 

touseef2660
Helper I
Helper I

@amitchandak  Can you please help in this problem?

Ahmedx
Super User
Super User

add a second column like this

Screenshot_1.png

But this is also not giving correct output. If you check in excel the output should be:

touseef2660_0-1699613319325.png

 

Your Excel formula is wrong.  Should say $B$2.

 

I don't think you can do this in DAX as this would require a self referencing MAXX condition inside a SUMX.

 

It is relatively trivial in Power Query as List.Accumulate allows for the manipulation of intermediate results.

 

 

 

let
  Source = Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText("i45WMlCK1YlWMgaTpmDSCEzqGoIpZNIEpjQWAA==", BinaryEncoding.Base64), 
        Compression.Deflate
      )
    ), 
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [Gross = _t]
  ), 
  #"Changed Type" = Table.TransformColumnTypes(Source, {{"Gross", Int64.Type}}), 
  #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type), 
  #"Added Custom" = Table.AddColumn(
    #"Added Index", 
    "Calculation", 
    each List.Accumulate(
      {0 .. [Index]}, 
      [s = 0, m = 0], 
      (state, current) => [
        s = #"Added Index"[Gross]{current} + state[m], 
        m = List.Max({#"Added Index"[Gross]{current} + state[m], state[m]})
      ]
    )
  ), 
  #"Expanded Calculation" = Table.ExpandRecordColumn(
    #"Added Custom", 
    "Calculation", 
    {"s"}, 
    {"Calculation"}
  ), 
  #"Removed Other Columns" = Table.SelectColumns(#"Expanded Calculation", {"Gross", "Calculation"}), 
  #"Changed Type1" = Table.TransformColumnTypes(
    #"Removed Other Columns", 
    {{"Calculation", Int64.Type}}
  )
in
  #"Changed Type1"

 

 

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

Thank you for your reply but if it could be possible to solve in dax that would be really helpful. So you are saying this is not possible through Dax right?

To the best of my knowledge this is not possible in DAX. You would need to be able to self reference a calculated table while it is being constructed. Happy to be proven wrong.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors