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
CheenuSing
Community Champion
Community Champion

Retrieve previous row value based on 2 columns

Hi,

I have a table consisting of columns 

ProductTypeMetricYear

Each product undergoes checking twice Review and FInal. A Metric is given to it. 

Sample data

ProductTypeMetricYear
AFinal32014
AFinal32015
AFinal32016
AFinal32017
AFinal42018
AFinal42019
AFinal42020
AFinal42023
AReview42018
AReview42019
AReview42020
AReview42023
CHFinal42014
CHFinal42016
CHFinal42018
CHFinal42020
CHFinal52022
CHReview42018
CHReview42020
IAFinal32014
IAFinal42016
IAFinal42018
IAFinal42020
IAFinal52022
IAReview52018
IAReview52020
ISFinal42014
ISFinal42015
ISFinal42016
ISFinal42017
ISFinal42018
ISFinal42019
ISFinal42020
ISFinal42023
ISReview42018
ISReview42019
ISReview52020
ISReview42023

 

I need help in power query to retrieve the previous row  value of metric by Product/Type/Year .

Expected trasnformed output in the column previous metric

ProductTypeMetricYearPrevious Metric
AFinal32014 
AFinal320153
AFinal320163
AFinal320173
AFinal420183
AFinal420194
AFinal420204
AFinal420234
AReview42018 
AReview420194
AReview420204
AReview420234
CHFinal42014 
CHFinal420164
CHFinal420184
CHFinal420204
CHFinal520224
CHReview42018 
CHReview420204
IAFinal32014 
IAFinal420163
IAFinal420184
IAFinal420204
IAFinal520224
IAReview52018 
IAReview520205
ISFinal42014 
ISFinal420154
ISFinal420164
ISFinal420174
ISFinal420184
ISFinal420194
ISFinal420204
ISFinal420234
ISReview42018 
ISReview420194
ISReview520204
ISReview420235

Any help will be appreciated.

 

Cheers

Cheenusing

Did I answer your question? Mark my post as a solution and also give KUDOS !

Proud to be a Datanaut!
2 ACCEPTED SOLUTIONS
BA_Pete
Super User
Super User

Hi @CheenuSing ,

 

Here's one way to do it using two offset Index columns and merging the table on itself:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddK9DoQgEATgd6G28BAQSmNijvauNBZXWJhcLPX1NcL9ADMFofiSnWWXcRSdqMSwrK/3eTfnkfVNiamCoBkYBm0BKoBl4AjImkHzhce8LfOOQnJxTP5icgk5/b3sWVExVCyT2EEiOoj8CX4qoljP00V7sAVDxTJBOUnfPhmqLspl9Kn3ZPNGoqkYKi0VS8Ux4V3H/3MJ3h4kB6gcEPir0wE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Type = _t, Metric = _t, Year = _t]),

    sortProductTypeYear = Table.Sort(Source,{{"Product", Order.Ascending}, {"Type", Order.Ascending}, {"Year", Order.Ascending}}),
    addIndex1 = Table.AddIndexColumn(sortProductTypeYear, "Index1", 1, 1, Int64.Type),
    addIndex0 = Table.AddIndexColumn(addIndex1, "Index0", 0, 1, Int64.Type),
    mergeOnSelf = Table.NestedJoin(addIndex0, {"Product", "Type", "Index0"}, addIndex0, {"Product", "Type", "Index1"}, "addIndex", JoinKind.LeftOuter),
    expandPreviousMetric = Table.ExpandTableColumn(mergeOnSelf, "addIndex", {"Metric"}, {"PreviousMetric"}),
    remOthCols = Table.SelectColumns(expandPreviousMetric,{"Product", "Type", "Metric", "Year", "PreviousMetric"})
in
    remOthCols

 

To get this output:

BA_Pete_0-1701759344881.png

 

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

AlienSx
Super User
Super User

hello, @CheenuSing 

    s = your_table,
    f = (x) => 
        [a = Table.ToColumns(x),
        b = Table.FromColumns(a & {{null} & List.RemoveLastN(x[Metric], 1)}, Table.ColumnNames(x) & {"Previous Metric"})][b],
    g = Table.Group(s, {"Product", "Type"}, {{"pm", (x) => f(Table.Sort(x, "Year"))}}),
    z = Table.ExpandTableColumn(g, "pm", {"Metric", "Year", "Previous Metric"})

View solution in original post

4 REPLIES 4
AlienSx
Super User
Super User

hello, @CheenuSing 

    s = your_table,
    f = (x) => 
        [a = Table.ToColumns(x),
        b = Table.FromColumns(a & {{null} & List.RemoveLastN(x[Metric], 1)}, Table.ColumnNames(x) & {"Previous Metric"})][b],
    g = Table.Group(s, {"Product", "Type"}, {{"pm", (x) => f(Table.Sort(x, "Year"))}}),
    z = Table.ExpandTableColumn(g, "pm", {"Metric", "Year", "Previous Metric"})

Hi @AlienSx 

 

Can you please explain the steps in simple english.

 

Cheers

 

CheenuSing

Did I answer your question? Mark my post as a solution and also give KUDOS !

Proud to be a Datanaut!

@CheenuSing Can't do step by step. I'll explain the idea.

f = (x) => is custom function (it's not a step) that takes a table as an argument. It takes a column with metric, "shifts it down" by one item (cuts the tail of the list and adds null as first item: {null} & List.RemoveLastN(x[Metric], 1)) so that we have a list with previous metrics values. Then we simply split table x to a list of columns (Table.ToColumns), add a list with shifted metrics values to it and transform this updated list of columns back to table (Table.FromColumns).

Function f must be applied to a group of rows with the same  values of Product and Type. That what Table.Group does. Then we simply create new aggregated column pm, sort each table by year and apply our transformation f . Expand some columns from transformed table in the end (Table.ExpandTableColumn).

BA_Pete
Super User
Super User

Hi @CheenuSing ,

 

Here's one way to do it using two offset Index columns and merging the table on itself:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddK9DoQgEATgd6G28BAQSmNijvauNBZXWJhcLPX1NcL9ADMFofiSnWWXcRSdqMSwrK/3eTfnkfVNiamCoBkYBm0BKoBl4AjImkHzhce8LfOOQnJxTP5icgk5/b3sWVExVCyT2EEiOoj8CX4qoljP00V7sAVDxTJBOUnfPhmqLspl9Kn3ZPNGoqkYKi0VS8Ux4V3H/3MJ3h4kB6gcEPir0wE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Type = _t, Metric = _t, Year = _t]),

    sortProductTypeYear = Table.Sort(Source,{{"Product", Order.Ascending}, {"Type", Order.Ascending}, {"Year", Order.Ascending}}),
    addIndex1 = Table.AddIndexColumn(sortProductTypeYear, "Index1", 1, 1, Int64.Type),
    addIndex0 = Table.AddIndexColumn(addIndex1, "Index0", 0, 1, Int64.Type),
    mergeOnSelf = Table.NestedJoin(addIndex0, {"Product", "Type", "Index0"}, addIndex0, {"Product", "Type", "Index1"}, "addIndex", JoinKind.LeftOuter),
    expandPreviousMetric = Table.ExpandTableColumn(mergeOnSelf, "addIndex", {"Metric"}, {"PreviousMetric"}),
    remOthCols = Table.SelectColumns(expandPreviousMetric,{"Product", "Type", "Metric", "Year", "PreviousMetric"})
in
    remOthCols

 

To get this output:

BA_Pete_0-1701759344881.png

 

Pete

 



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

Proud to be a Datanaut!




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.

Top Solution Authors
Top Kudoed Authors