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
Uzi2019
Super User
Super User

how to shift row value to next row

Hi Community,

I have two column one is date another is values. I have tried a lot many approaches but not getting any complete result. 

So basicaly, value should be shifted to next row as per date and if same date is repeated so same values should be there. If i am not clear so below is the expected ouput.

Input is like below: 

ValuesDate
1381/1/2020
1201/2/2020
801/3/2020
901/4/2020
1101/5/2020
1241/1/2020
1001/3/2020

 

Expected Output:

DateValuesOutput
1/1/2020138138
1/1/2020124124
1/2/2020120124
1/3/20208080
1/3/2020100100
1/4/202090100
1/5/202011090

 

TIA!

Don't forget to give thumbs up and accept this as a solution if it helped you!!!
1 ACCEPTED SOLUTION
MFelix
Super User
Super User

Hi @Uzi2019 ,

 

Not really sure how you are getting the sorting done but to get the same sorting I did the following steps in the query editor:

 

  • Added an Index column
  • Sorted by:
    • Ascending Dates
    • Ascending Index
  • Deleted the Index column
  • Added a new Index column
  • Add the following calculated column
  if [Index] = 0
  then [Values]
  else if [Index] = List.Max(#"Added Index1"[Index]) then #"Added Index1"{[Index] - 1}[Values] else 
    if (#"Added Index1"{[Index]
      - 1}[Date]
      = [Date]
      = false and #"Added Index1"{[Index] + 1}[Date]
      = [Date]
      = false)
    then #"Added Index1"{[Index] - 1}[Values]
    else [Values]

 

Check full code below:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjS2UNJRMjDUByIjAyMDpVgdoCCQBgoaoQhagMWMUcQswWImqJoNwYKmaCaaYLHGAN3IWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Values = _t, Date = _t]),
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1),
    #"Changed Type" = Table.TransformColumnTypes(#"Added Index",{{"Values", Int64.Type}, {"Date", type date}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Date", Order.Ascending}, {"Index", Order.Ascending}}),
    #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Index"}),
    #"Added Index1" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1),
    #"Added Custom" = Table.AddColumn(#"Added Index1", "Custom", each if [Index] = 0
  then [Values]
  else if [Index] = List.Max(#"Added Index1"[Index]) then #"Added Index1"{[Index] - 1}[Values] else 
    if (#"Added Index1"{[Index]
      - 1}[Date]
      = [Date]
      = false and #"Added Index1"{[Index] + 1}[Date]
      = [Date]
      = false)
    then #"Added Index1"{[Index] - 1}[Values]
    else [Values])
in
    #"Added Custom"

 

This may need adjustments based on the sorting of your data.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

1 REPLY 1
MFelix
Super User
Super User

Hi @Uzi2019 ,

 

Not really sure how you are getting the sorting done but to get the same sorting I did the following steps in the query editor:

 

  • Added an Index column
  • Sorted by:
    • Ascending Dates
    • Ascending Index
  • Deleted the Index column
  • Added a new Index column
  • Add the following calculated column
  if [Index] = 0
  then [Values]
  else if [Index] = List.Max(#"Added Index1"[Index]) then #"Added Index1"{[Index] - 1}[Values] else 
    if (#"Added Index1"{[Index]
      - 1}[Date]
      = [Date]
      = false and #"Added Index1"{[Index] + 1}[Date]
      = [Date]
      = false)
    then #"Added Index1"{[Index] - 1}[Values]
    else [Values]

 

Check full code below:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjS2UNJRMjDUByIjAyMDpVgdoCCQBgoaoQhagMWMUcQswWImqJoNwYKmaCaaYLHGAN3IWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Values = _t, Date = _t]),
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1),
    #"Changed Type" = Table.TransformColumnTypes(#"Added Index",{{"Values", Int64.Type}, {"Date", type date}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Date", Order.Ascending}, {"Index", Order.Ascending}}),
    #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Index"}),
    #"Added Index1" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1),
    #"Added Custom" = Table.AddColumn(#"Added Index1", "Custom", each if [Index] = 0
  then [Values]
  else if [Index] = List.Max(#"Added Index1"[Index]) then #"Added Index1"{[Index] - 1}[Values] else 
    if (#"Added Index1"{[Index]
      - 1}[Date]
      = [Date]
      = false and #"Added Index1"{[Index] + 1}[Date]
      = [Date]
      = false)
    then #"Added Index1"{[Index] - 1}[Values]
    else [Values])
in
    #"Added Custom"

 

This may need adjustments based on the sorting of your data.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



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