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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
William_Moreno
Helper II
Helper II

difference between two rows in the power query (m-language)

Hi everyone,

Today, I have this situation "difference between two rows in the power query (m-language)"

table - 1

dateunitskuvalue
2021-07-01Berlim10025510.5
2021-07-01Rome10025511.5
2021-07-01New York10025512.5

2021-08-01

Berlim10025513.0
2021-08-01Rome10025514.0
2021-08-01New York10025517.0

 

expected results 

dateunitskuvaluediference
2020-07-01Berlim10025510.50
2020-07-01Rome10025511.50
2020-07-01New York10025512.50

2021-08-01

Berlim10025513.02.5
2021-08-01Rome10025515.03.5
2021-08-01New York10025518.05.5

 

Could you help?

Thanks in advanced!

 

William M.

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

Yes, familiarize yourself with how to reference tables (remember that each Power Query step result is a table), columns and rows. Your data needs to be sortable, and ideally (although not strictly required) have a zero-based index column. Columns are addressed by [ ], rows by { } .

 

Then use functions like Table.SelectRows() and custom column generators to identify the desired previous rows and pull their column data in. And finally learn about "try...otherwise" to properly handle the errors that you will hit for the rows that don't have a "prior" row.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtQ1MNc1MFTSUXJKLcrJzAMyDA0MjExNwQw9U6VYHTR1Qfm5qSiqDLGp8kstV4jML8pGUWmEotICp73GegaY6jDsNcGmCqu95iCVsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [date = _t, unit = _t, sku = _t, value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"unit", type text}, {"sku", type text}, {"value", type number}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "difference", each try [value]-Table.SelectRows(#"Changed Type",(k)=> [unit]=k[unit] and [date]>k[date])[value]{0} otherwise 0)
in
    #"Added Custom"

Note:  This code doesn't do proper sorting, but your sample data is not sufficent to test that anyway.

 

 

View solution in original post

1 REPLY 1
lbendlin
Super User
Super User

Yes, familiarize yourself with how to reference tables (remember that each Power Query step result is a table), columns and rows. Your data needs to be sortable, and ideally (although not strictly required) have a zero-based index column. Columns are addressed by [ ], rows by { } .

 

Then use functions like Table.SelectRows() and custom column generators to identify the desired previous rows and pull their column data in. And finally learn about "try...otherwise" to properly handle the errors that you will hit for the rows that don't have a "prior" row.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtQ1MNc1MFTSUXJKLcrJzAMyDA0MjExNwQw9U6VYHTR1Qfm5qSiqDLGp8kstV4jML8pGUWmEotICp73GegaY6jDsNcGmCqu95iCVsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [date = _t, unit = _t, sku = _t, value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"unit", type text}, {"sku", type text}, {"value", type number}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "difference", each try [value]-Table.SelectRows(#"Changed Type",(k)=> [unit]=k[unit] and [date]>k[date])[value]{0} otherwise 0)
in
    #"Added Custom"

Note:  This code doesn't do proper sorting, but your sample data is not sufficent to test that anyway.

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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