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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
needshelp20
New Member

DAX to Power Query - Offset Functions

Does anybody know how to convert the following code from DAX to Power Query? I'm not sure what the equivalent function for Calculate and Offset is in M Query. There's an example of what the table should look like at the bottom of the image.

needshelp20_0-1717692879092.png

 

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

Does it need to work with the year column or is it acceptable to add an index column?  Remember that in Power Query you can address any row  via {x}  where x can be a row number or a condition.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtFTSUTI2NQUyTPTMzZRidUCiRgZAUXMzSwtjUz0LA6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t, #"Sales Amount" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}, {"Sales Amount", Currency.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Prev Year Sales", (k)=> try #"Changed Type"{[Year = k[Year]-1]}[Sales Amount] otherwise null,Currency.Type)
in
    #"Added Custom"

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". Once you examined the code, replace the Source step with your own source.

View solution in original post

1 REPLY 1
lbendlin
Super User
Super User

Does it need to work with the year column or is it acceptable to add an index column?  Remember that in Power Query you can address any row  via {x}  where x can be a row number or a condition.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtFTSUTI2NQUyTPTMzZRidUCiRgZAUXMzSwtjUz0LA6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t, #"Sales Amount" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}, {"Sales Amount", Currency.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Prev Year Sales", (k)=> try #"Changed Type"{[Year = k[Year]-1]}[Sales Amount] otherwise null,Currency.Type)
in
    #"Added Custom"

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". Once you examined the code, replace the Source step with your own source.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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