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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
smpa01
Super User
Super User

Prev Value not working

I am trying to get the previous row value and this is what I tried which is not working

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc2xCQAxDEPRXVwfyJITLpnFZP81UtopH/qgTCOCkHPa+dIErdLAKEyoI7z0926BHdolOuJVS8nnm6r1XA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Merged = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Merged", type date}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each let 
    v = [Index],
    x = v-1,
    y = #"Added Index"[Merged]
in
if x=0 then null else y{x})
in
    #"Added Custom"

 

The above is returning the following

smpa01_0-1673275192982.png

I was hoping the code to return

 

| Merged     | Index | Custom     |
|------------|-------|------------|
| 1/31/2015  | 1     | null       |
| 2/28/2015  | 2     | 1/31/2015  |
| 4/4/2015   | 3     | 2/28/2015  |
| 5/2/2015   | 4     | 4/4/2015   |
| 5/30/2015  | 5     | 5/2/2015   |
| 7/4/2015   | 6     | 5/30/2015  |
| 8/1/2015   | 7     | 7/4/2015   |
| 8/29/2015  | 8     | 8/1/2015   |
| 10/3/2015  | 9     | 8/29/2015  |
| 10/31/2015 | 10    | 10/3/2015  |
| 11/28/2015 | 11    | 10/31/2015 |
| 12/31/2015 | 12    | 11/28/2015 |

 

@CNENFRNL @AlexisOlson 

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
1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @smpa01 ,

 

My immediate guess is you've forgotten that Power Query rows are zero-base. Therefore, when you apply 'y{x}', you're just rturning back to the same row reference.

The easiest way to fix would be to create your [Index] column starting from zero and amend your result to:

if x < 0 then null else y{x}

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

5 REPLIES 5
CNENFRNL
Community Champion
Community Champion

Give that the original data is already sort, the solution is tricky and simple enough,

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc2xCQAxDEPRXVwfyJITLpnFZP81UtopH/qgTCOCkHPa+dIErdLAKEyoI7z0926BHdolOuJVS8nnm6r1XA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Merged = _t]),

    Rearraged = let col = Source[Merged] in Table.FromColumns({col,{null}&List.RemoveLastN(col)})
in
    Rearraged

CNENFRNL_0-1673305827913.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

AlexisOlson
Super User
Super User

Just FYI, this approach of getting the previous value by index lookup is really inefficient for large tables.

 

@Vijay_A_Verma has a nice blog post related to this:

https://community.powerbi.com/t5/Community-Blog/Power-Query-Methods-to-Refer-to-Previous-Row-Time-Pe...

 

BA_Pete
Super User
Super User

Hi @smpa01 ,

 

My immediate guess is you've forgotten that Power Query rows are zero-base. Therefore, when you apply 'y{x}', you're just rturning back to the same row reference.

The easiest way to fix would be to create your [Index] column starting from zero and amend your result to:

if x < 0 then null else y{x}

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




@BA_Pete  yup , completely forgot.

Sorry for this stupid Q

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

 

Lol, easily done. 🙂👍

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors
Top Kudoed Authors