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
Anonymous
Not applicable

Fill Down only when the limit values are the same

Hello everyone, 

 

I'm working with some sales data and I wanted to estimate the number of days an item remained at the same price. What I have at this point is a table with the sku's names in the columns, the dates as row's ID, and the price as values. Something like this:

Date SKU 1SKU 2SKU3SKU 4
10/10/21null755485
10/11/2125null54null
10/12/21nullnull

null

87
10/13/21null71nullnull
10/14/2125null54null
10/15/21nullnullnull87
10/16/2121nullnull85
10/17/21null71nullnull

 

I only want to fill down where the values between the null cells are the same, so the table should look like this:

 

Date SKU 1SKU 2SKU3SKU 4
10/10/21null755485
10/11/2125null54null
10/12/2125null

54

87
10/13/2125715487
10/14/2125715487
10/15/21null71null87
10/16/212171null85
10/17/21null71nullnull

 

Is there any way to do it? 

 

I would really appreciate the help 🙂 

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

This is a bit of a frankenscript but it does what you ask for.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTQByIjQyUdJSAyNwUSpiZAwsJUKVYHIm0IkTYyhagBS8MljeB6IcjCHC5ljDAWpgQmZYLPSFPcRppB9SHkkdxpjsW+WAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, SKU1 = _t, SKU2 = _t, SKU3 = _t, SKU4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date"}, "SKU", "Value"),
    #"Sorted Rows" = Table.Sort(#"Unpivoted Other Columns",{{"SKU", Order.Ascending}, {"Date", Order.Ascending}}),
    #"Added Custom" = Table.AddColumn(#"Sorted Rows", "Custom", 
        each Table.Combine({ Table.FromRecords({Table.Last(Table.SelectRows(#"Sorted Rows",(k)=> k[SKU]=[SKU] and k[Value]>"" and k[Date]<[Date] ))}) , 
        Table.FromRecords({ Table.First(Table.SelectRows(#"Sorted Rows",(k)=> k[SKU]=[SKU] and k[Value]>"" and k[Date]>[Date] ))})})),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "NewValue", each if [Value]>"" then [Value] else try
 if [Custom]{0}[Value]=[Custom]{1}[Value] then [Custom]{0}[Value] else ""
 otherwise ""),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Date", "SKU", "NewValue"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Other Columns", List.Distinct(#"Removed Other Columns"[SKU]), "SKU", "NewValue")
in
    #"Pivoted Column"

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".

 

lbendlin_0-1635304749003.png

 

View solution in original post

3 REPLIES 3
wdx223_Daniel
Super User
Super User

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Custom1 = Table.UnpivotOtherColumns(Source,{"Date"},"SKU","Value"),
    Custom2 = Table.Combine(Table.Group(Custom1,"SKU",{"n",each Table.Combine(Table.Group(Table.Sort(_,"Date"),"Value",{"n",each #table(Table.ColumnNames(_),List.Transform({Number.From(List.Min([Date]))..Number.From(List.Max([Date]))},(x)=>{Date.From(x),[SKU]{0},[Value]{0}}))},0,(x,y)=>Byte.From(x<>y))[n])})[n]),
    Custom3 = Table.Pivot(Custom2,List.Distinct(Custom2[SKU]),"SKU","Value")
in
    Custom3
Syndicate_Admin
Administrator
Administrator

This is a bit of a frankenscript but it does what you ask for.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTQByIjQyUdJSAyNwUSpiZAwsJUKVYHIm0IkTYyhagBS8MljeB6IcjCHC5ljDAWpgQmZYLPSFPcRppB9SHkkdxpjsW+WAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, SKU1 = _t, SKU2 = _t, SKU3 = _t, SKU4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date"}, "SKU", "Value"),
    #"Sorted Rows" = Table.Sort(#"Unpivoted Other Columns",{{"SKU", Order.Ascending}, {"Date", Order.Ascending}}),
    #"Added Custom" = Table.AddColumn(#"Sorted Rows", "Custom", 
        each Table.Combine({ Table.FromRecords({Table.Last(Table.SelectRows(#"Sorted Rows",(k)=> k[SKU]=[SKU] and k[Value]>"" and k[Date]<[Date] ))}) , 
        Table.FromRecords({ Table.First(Table.SelectRows(#"Sorted Rows",(k)=> k[SKU]=[SKU] and k[Value]>"" and k[Date]>[Date] ))})})),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "NewValue", each if [Value]>"" then [Value] else try
 if [Custom]{0}[Value]=[Custom]{1}[Value] then [Custom]{0}[Value] else ""
 otherwise ""),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Date", "SKU", "NewValue"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Other Columns", List.Distinct(#"Removed Other Columns"[SKU]), "SKU", "NewValue")
in
    #"Pivoted Column"

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".

 

lbendlin_0-1635304749003.png

 

lbendlin
Super User
Super User

This is a bit of a frankenscript but it does what you ask for.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTQByIjQyUdJSAyNwUSpiZAwsJUKVYHIm0IkTYyhagBS8MljeB6IcjCHC5ljDAWpgQmZYLPSFPcRppB9SHkkdxpjsW+WAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, SKU1 = _t, SKU2 = _t, SKU3 = _t, SKU4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date"}, "SKU", "Value"),
    #"Sorted Rows" = Table.Sort(#"Unpivoted Other Columns",{{"SKU", Order.Ascending}, {"Date", Order.Ascending}}),
    #"Added Custom" = Table.AddColumn(#"Sorted Rows", "Custom", 
        each Table.Combine({ Table.FromRecords({Table.Last(Table.SelectRows(#"Sorted Rows",(k)=> k[SKU]=[SKU] and k[Value]>"" and k[Date]<[Date] ))}) , 
        Table.FromRecords({ Table.First(Table.SelectRows(#"Sorted Rows",(k)=> k[SKU]=[SKU] and k[Value]>"" and k[Date]>[Date] ))})})),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "NewValue", each if [Value]>"" then [Value] else try
 if [Custom]{0}[Value]=[Custom]{1}[Value] then [Custom]{0}[Value] else ""
 otherwise ""),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Date", "SKU", "NewValue"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Other Columns", List.Distinct(#"Removed Other Columns"[SKU]), "SKU", "NewValue")
in
    #"Pivoted Column"

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".

 

lbendlin_0-1635304749003.png

 

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