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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
Markus_74
Frequent Visitor

Get value from prev column

Hi all

Stock inventory only updates once a year or when item stock level is changed, hence I get this type of table over a selected period

 

Item/Week1234
Item 1320 
Item 2 2  
Item 35 4 
Item 4 3 0

 

 

But what I want is this

 

Item/Week1234
Item 13200
Item 22222
Item 35544
Item 43330

 

So how do I create a measure that checks if value is not there and if so use the previous column value,

The logic would be something like:

IF (Week 3 has value )

   Show week 3 value

Else

 Show week 2 value

 

/Markus

1 ACCEPTED SOLUTION

@Markus_74 

 

I believe we will have to add missing rows for weeks first.

 

Then we can use a DAX measure to get last non blank value or we can directly do it in Power Query As well

 

Please see the attached file and let me know if it helps

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8ixJzVUwVNJRAmFjpVgdqJAxVMgUIQTiGoExXMgIU8gEKmSMqhFkngGq8SBsgqrRBKIqFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Item = _t, Week = _t, StockQty = _t]),
    Custom1 = List.Distinct(Source[Item]),
    Custom2 = List.Distinct(Source[Week]),
    Items=Table.FromColumns({Custom1,List.Repeat({1},List.Count(Custom1))}),
    Weeks=Table.FromColumns({Custom2,List.Repeat({1},List.Count(Custom2))}),
    MergedQueries = Table.NestedJoin(Items,{"Column2"},Weeks,{"Column2"},"mytable",JoinKind.LeftOuter),
    Expandmytable = Table.ExpandTableColumn(MergedQueries, "mytable", {"Column1"}, {"Week"}),
    RemovedColumns = Table.RemoveColumns(Expandmytable,{"Column2"}),
    MergedQueriesAgain = Table.NestedJoin(RemovedColumns,{"Column1", "Week"},Source,{"Item", "Week"},"Table",JoinKind.LeftOuter),
    #"Expanded Table" = Table.ExpandTableColumn(MergedQueriesAgain, "Table", {"StockQty"}, {"StockQty"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Table",{{"Column1", Order.Ascending}}),
    #"Renamed Columns" = Table.RenameColumns(#"Sorted Rows",{{"Column1", "Item"}})
in
    #"Renamed Columns"

Marcus.png

View solution in original post

3 REPLIES 3
Zubair_Muhammad
Community Champion
Community Champion

Hi @Markus_74

 

Please see if this approach helps

 

File attached as well

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8ixJzVUwVNJRMgZiIyA2AGIFpVgdqJQRmAuRUkCVAukwhQqboEqZQIWNobSBUmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Item/Week" = _t, #"1" = _t, #"2" = _t, #"3" = _t, #"4" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item/Week", type text}, {"1", Int64.Type}, {"2", Int64.Type}, {"3", Int64.Type}, {"4", Int64.Type}}),
    #"Transposed Table" = Table.Transpose(#"Changed Type"),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Item 1", Int64.Type}, {"Item 2", Int64.Type}, {"Item 3", Int64.Type}, {"Item 4", Int64.Type}}),
    #"Filled Down" = Table.FillDown(#"Changed Type1",{"Item 1", "Item 2", "Item 3", "Item 4"}),
    #"Filled Up" = Table.FillUp(#"Filled Down",{"Item 1", "Item 2", "Item 3", "Item 4"}),
    #"Demoted Headers" = Table.DemoteHeaders(#"Filled Up"),
    #"Changed Type2" = Table.TransformColumnTypes(#"Demoted Headers",{{"Column1", type any}, {"Column2", type any}, {"Column3", type any}, {"Column4", type any}}),
    #"Transposed Table1" = Table.Transpose(#"Changed Type2")
in
    #"Transposed Table1"

 

 

Interesting solution

You are moving the solution to the data retrieval part

I thougt it would be easier to acheive this in the visualisation rendering

 

Also my raw data table does not look like the one above, that is more how my visualization looks like (I use the matrix visual)

This is the example of my raw data 

 

ItemWeekStockQty
Item 113
Item 315
Item 122
Item 222
Item 423
Item 130
Item 334
Item 440

 

Would that change the solution to the problem?

/Markus

@Markus_74 

 

I believe we will have to add missing rows for weeks first.

 

Then we can use a DAX measure to get last non blank value or we can directly do it in Power Query As well

 

Please see the attached file and let me know if it helps

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8ixJzVUwVNJRAmFjpVgdqJAxVMgUIQTiGoExXMgIU8gEKmSMqhFkngGq8SBsgqrRBKIqFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Item = _t, Week = _t, StockQty = _t]),
    Custom1 = List.Distinct(Source[Item]),
    Custom2 = List.Distinct(Source[Week]),
    Items=Table.FromColumns({Custom1,List.Repeat({1},List.Count(Custom1))}),
    Weeks=Table.FromColumns({Custom2,List.Repeat({1},List.Count(Custom2))}),
    MergedQueries = Table.NestedJoin(Items,{"Column2"},Weeks,{"Column2"},"mytable",JoinKind.LeftOuter),
    Expandmytable = Table.ExpandTableColumn(MergedQueries, "mytable", {"Column1"}, {"Week"}),
    RemovedColumns = Table.RemoveColumns(Expandmytable,{"Column2"}),
    MergedQueriesAgain = Table.NestedJoin(RemovedColumns,{"Column1", "Week"},Source,{"Item", "Week"},"Table",JoinKind.LeftOuter),
    #"Expanded Table" = Table.ExpandTableColumn(MergedQueriesAgain, "Table", {"StockQty"}, {"StockQty"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Table",{{"Column1", Order.Ascending}}),
    #"Renamed Columns" = Table.RenameColumns(#"Sorted Rows",{{"Column1", "Item"}})
in
    #"Renamed Columns"

Marcus.png

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.