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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Kargap7
Frequent Visitor

Previous and next value by category, specific type and date

Hello,

 

I want to have two columns: one for finding previous "REIS" value and other for next "REIS" value for those rows whose type is not "REIS" and when there are different categories.

 

Let's say category 12B have four "REIS" on the 9th of May. Each row after "REIS" by date should return me the previous or next row value with "REIS" type. 

Idk if the explanation is clear, the attached picture shows what result I need in Previous and Next columns.
Please help.

Screen1.png

CategoryTypeNumberDatePreviousNext
12BRYT45B2023-05-09 07:11 65T
12BREIS65T2023-05-09 07:11  
12BPAS56T2023-05-09 07:5665T56C
12BPAS35G2023-05-09 08:2465T56C
12BREIS56C2023-05-09 08:42  
12BPAS43V2023-05-09 11:4556C78K
12BREIS78K2023-05-09 13:04  
12BPAS52B2023-05-09 13:0478K23S
12BPAS56H2023-05-09 13:0578K23S
12BPAS45P2023-05-09 16:4278K23S
12BKUR89L2023-05-09 19:5278K23S
12BREIS23S2023-05-09 20:03  
12FPAS56H2023-05-09 07:56 89L
12FPAS45P2023-05-09 08:24 89L
12FREIS89L2023-05-09 08:42  
12FPAB23S2023-05-09 16:4289L65W
12FREIS65W2023-05-09 13:05  
12FPAS56H2023-05-09 13:0565W 
12FPAS45P2023-05-09 16:4265W 
12FKUR89L2023-05-09 20:0365W 



4 REPLIES 4
Ahmedx
Super User
Super User

OK,  pls again try

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZHBDoIwEER/xfRsSLvtLtCbGKsGDwRQYwj//xtCJNp2KfGy7eFldmZ2GISCSuxF++qnaXD+yzKTmIEEvcutUmLcf6nTtZsewn4Taw4zhcQoJEZpPIdUYcHwlUjHGDPAxIx+hJRS1iBXy4s64rSVhieA6i+MLhxDbg6bCKMwQ31vp1mUtwgrLQLPALoLOZBW6oVzaXPeFVzam3cF99vJvHlX+IhVa878oJ4a4TPd20aCdWyzXpeud6ltfAM=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category = _t, Type = _t, Number = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"Type", type text}, {"Number", type text}, {"Date", type datetime}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Category"}, {{"Count", 
    (x)=>  Table.ReplaceValue(
   Table.FillDown(
     Table.AddColumn(
        Table.AddIndexColumn(x,"Index",1,1),"Previous", each if [Type] ="REIS" then [Number] else null),
       {"Previous"}) ,each [Previous] ,each if [Type] = "REIS" then null else [Previous]  ,Replacer.ReplaceValue,{"Type", "Previous"})
}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Type", "Number", "Date", "Index", "Previous"}, {"Type", "Number", "Date", "Index", "Previous"}),
    #"Grouped Rows1" = Table.Group(#"Expanded Count", {"Category"}, 
   {{"Count", 
    (x)=> 
Table.ReplaceValue(
      Table.FillUp(
       Table.ReplaceValue(
        Table.FillUp(Table.DuplicateColumn(x, "Previous", "Next"),{"Next"}),
       each [Next] ,each if [Type] <> "REIS" then null else [Next]  ,Replacer.ReplaceValue,{"Type", "Next"}),
       {"Next"}),each [Next] ,each if [Type] = "REIS" then null else [Next]  ,Replacer.ReplaceValue,{"Type", "Next"})
}}),
    #"Removed Other Columns" = Table.Combine( Table.SelectColumns(#"Grouped Rows1",{"Count"})[Count])
in
    #"Removed Other Columns"

 

Still have issues.. @Ahmedx , first formula almost works with "Next" ones, and second formula almost works with "Previous" ones. 
I attach a picture below of what is wrong in both formulas. It is important to pay attention to the date, category and "REIS".

 

@Ahmedx could you please check it again?

 

Kargap7_0-1687872429383.png

 

Ahmedx
Super User
Super User

plse try this

Screen Capture #1338.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZHBDoIwEER/xfRsSLvtLtCbGKsGDwRQYwj//xtCJNp2KfGy7eFldmZ2GISCSuxF++qnaXD+yzKTmIEEvcutUmLcf6nTtZsewn4Taw4zhcQoJEZpPIdUYcHwlUjHGDPAxIx+hJRS1iBXy4s64rSVhieA6i+MLhxDbg6bCKMwQ31vp1mUtwgrLQLPALoLOZBW6oVzaXPeFVzam3cF99vJvHlX+IhVa878oJ4a4TPd20aCdWyzXpeud6ltfAM=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category = _t, Type = _t, Number = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"Type", type text}, {"Number", type text}, {"Date", type datetime}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "step1", each if [Type] ="REIS" then [Number] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"step1"}),
    from1 = Table.AddColumn(#"Filled Down", "Previous", each if [Type] = "REIS" then null else [step1]),
    #"Removed Columns" = Table.RemoveColumns(from1,{"step1"}),
    from2 = Table.AddColumn(#"Removed Columns", "N", each [Previous]),
    from3 = Table.FillUp(from2,{"N"}),
    #"Added Custom1" = Table.AddColumn(from3, "Custom", each [ t = 
Text.Combine(
List.Range( from3[N] &{null},[Index],1)),
to= if [Type] = "REIS" then null else if [Previous] =null or [N]<> t then t else null][to]),
    #"Filled Up" = Table.FillUp(#"Added Custom1",{"Custom"}),
    #"Added Custom2" = Table.AddColumn(#"Filled Up", "Next", each if [Type] = "REIS" then null else [Custom]),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom2",{"Index", "N", "Custom"})
in
    #"Removed Columns1"

 

Hi @Ahmedx  thank you so much for your response.
It almost works, it just ignores Category. It should return values from the same Category, could you please help with that?

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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