Forum Discussion
D3K
5 years agoAdvocate II
Stocks redistribution Table
Hello everyone! I have a really compliсated task to make it by myself, believe I've tried 🙂 so asking for help here. We have a database with Stores, Products, Sales History and Current Stocks....
- Anonymous5 years ago
Here's the solution:
You use the Filter Pane of the table above with the [Is True Target Store] measure to obtain the above.
And here's what needs to be created in PQ behind the scenes:
// SalesAndStockBaseTable let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCi7JL0pVMDBU0lFyBGIgMlWK1YGLG0HFjUFySOLGUHEjdA0mSBLI4qYIC8yRxc2AAk4QcWRhc6iwKZq4BUK5oRGyhCVWcwwNEMLGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Store ID" = _t, ProductID = _t, #"Sales Qty" = _t, #"Stock Qty" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Store ID", type text}, {"ProductID", type text}, {"Sales Qty", Int64.Type}, {"Stock Qty", Int64.Type}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Store Type", each let StockQty = if [Stock Qty] = null then 0 else [Stock Qty], SalesQty = if [Sales Qty] = null then 0 else [Sales Qty], Output = if StockQty > 0 and SalesQty = 0 then "Source Store" else if StockQty = 0 and SalesQty > 0 then "Target Store" else "Neutral Store" in Output ) in #"Added Custom" // Stores let Source = SalesAndStockBaseTable, #"Removed Other Columns" = Table.SelectColumns(Source,{"Store ID", "Store Type"}), #"Removed Duplicates" = Table.Distinct(#"Removed Other Columns") in #"Removed Duplicates" // Products let Source = SalesAndStockBaseTable, #"Removed Other Columns" = Table.SelectColumns(Source,{"ProductID"}), #"Removed Duplicates" = Table.Distinct(#"Removed Other Columns") in #"Removed Duplicates" // Target Stores let Source = Stores, #"Filtered Rows" = Table.SelectRows(Source, each ([Store Type] = "Target Store")), #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Store Type"}), #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Store ID", "Target Store ID"}}) in #"Renamed Columns" // Sales and Stock let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCi7JL0pVMDBU0lFyBGIgMlWK1YGLG0HFjUFySOLGUHEjdA0mSBLI4qYIC8yRxc2AAk4QcWRhc6iwKZq4BUK5oRGyhCVWcwwNEMLGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Store ID" = _t, ProductID = _t, #"Sales Qty" = _t, #"Stock Qty" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Store ID", type text}, {"ProductID", type text}, {"Sales Qty", Int64.Type}, {"Stock Qty", Int64.Type}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Store Type", each let StockQty = if [Stock Qty] = null then 0 else [Stock Qty], SalesQty = if [Sales Qty] = null then 0 else [Sales Qty], Output = if StockQty > 0 and SalesQty = 0 then "Source Store" else if StockQty = 0 and SalesQty > 0 then "Target Store" else "Neutral Store" in Output ), #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Store Type"}) in #"Removed Columns" - Anonymous5 years ago
Current Stocks = var StoreInScope = ISINSCOPE( Stores[Store ID] ) var ProductInScope = ISINSCOPE( Products[ProductID] ) var BothInScope = StoreInScope && ProductInScope var Result = if( BothInScope, sum( 'Sales and Stock'[Stock Qty] ) ) return ResultIs True Target Store = var SourceStoreInScope = ISINSCOPE( Stores[Store ID] ) var SourceStoreIsReallySource = SELECTEDVALUE( Stores[Store Type] ) = "Source Store" var TargetStoreInScope = ISINSCOPE( 'Target Stores'[Target Store ID] ) var TargetStoreNotBlank = // It could be blank due to the additional row // generated by DAX in the Target Stores table // after enabling the relationship which has // referential integrity violations (but this is // how it should be since not all stores are // target stores). NOT ISBLANK( SELECTEDVALUE( 'Target Stores'[Target Store ID] ) ) var ProductInScope = ISINSCOPE( Products[ProductID] ) var AllThreeInScope = TRUE() && SourceStoreInScope && SourceStoreIsReallySource && TargetStoreInScope && TargetStoreNotBlank && ProductInScope var IsCurrentTargetStoreForCurrentProduct = if( AllThreeInScope, CALCULATE( NOT ISEMPTY( 'Sales and Stock' ), // Remove filters from Stores ALL( Stores ), // Enable filtering from the Target Stores USERELATIONSHIP( 'Target Stores'[Target Store ID], 'Sales and Stock'[Store ID] ) ) ) return // I multiply by 1 to return an int // so that the Filter Pane can work // with this measure. 1 * IsCurrentTargetStoreForCurrentProduct
Anonymous
5 years agoNot applicable
Here's the solution:
You use the Filter Pane of the table above with the [Is True Target Store] measure to obtain the above.
And here's what needs to be created in PQ behind the scenes:
// SalesAndStockBaseTable
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCi7JL0pVMDBU0lFyBGIgMlWK1YGLG0HFjUFySOLGUHEjdA0mSBLI4qYIC8yRxc2AAk4QcWRhc6iwKZq4BUK5oRGyhCVWcwwNEMLGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Store ID" = _t, ProductID = _t, #"Sales Qty" = _t, #"Stock Qty" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Store ID", type text}, {"ProductID", type text}, {"Sales Qty", Int64.Type}, {"Stock Qty", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Store Type",
each
let
StockQty = if [Stock Qty] = null then 0 else [Stock Qty],
SalesQty = if [Sales Qty] = null then 0 else [Sales Qty],
Output =
if StockQty > 0 and SalesQty = 0 then "Source Store"
else
if StockQty = 0 and SalesQty > 0 then "Target Store"
else
"Neutral Store"
in
Output
)
in
#"Added Custom"
// Stores
let
Source = SalesAndStockBaseTable,
#"Removed Other Columns" = Table.SelectColumns(Source,{"Store ID", "Store Type"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Other Columns")
in
#"Removed Duplicates"
// Products
let
Source = SalesAndStockBaseTable,
#"Removed Other Columns" = Table.SelectColumns(Source,{"ProductID"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Other Columns")
in
#"Removed Duplicates"
// Target Stores
let
Source = Stores,
#"Filtered Rows" = Table.SelectRows(Source, each ([Store Type] = "Target Store")),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Store Type"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Store ID", "Target Store ID"}})
in
#"Renamed Columns"
// Sales and Stock
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCi7JL0pVMDBU0lFyBGIgMlWK1YGLG0HFjUFySOLGUHEjdA0mSBLI4qYIC8yRxc2AAk4QcWRhc6iwKZq4BUK5oRGyhCVWcwwNEMLGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Store ID" = _t, ProductID = _t, #"Sales Qty" = _t, #"Stock Qty" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Store ID", type text}, {"ProductID", type text}, {"Sales Qty", Int64.Type}, {"Stock Qty", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Store Type",
each
let
StockQty = if [Stock Qty] = null then 0 else [Stock Qty],
SalesQty = if [Sales Qty] = null then 0 else [Sales Qty],
Output =
if StockQty > 0 and SalesQty = 0 then "Source Store"
else
if StockQty = 0 and SalesQty > 0 then "Target Store"
else
"Neutral Store"
in
Output
),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Store Type"})
in
#"Removed Columns"D3K
5 years agoAdvocate II
Thanks a lot for this elegant solution!
But I've missed 2 measures, used in this algorithm: "Current Stocks" and "Is True Target Store"
Can you add it here? Thank you