Forum Discussion
Convert transactions to balance
- 9 years ago
Yes, I'd say the measure is the way to go here. It should work as desired in the chart if you take the date-field from your date-table and not from the transactions-table.
Link to file: https://www.dropbox.com/s/dc8budcqd68ogz4/PBI_CumTotalAllDates.pbix?dl=0
But i need to use the numbers in the formulas...
What formula? Could you please describe your expectation? So I could figure out possible approach.
- Tiolan9 years agoFrequent VisitorThanks for helping!
For example, build a diagram, that describes the dynamic of stock of certain sku in certain warehouse. I need to have all this data in a table, not only on visualization.- dkay84_PowerBI9 years ago
Microsoft Employee
Have you considered using "group by" in the query editor? After grouping appropriately you can apply the common cumulative total DAX pattern to get the result - Baskar9 years ago
Resident Rockstar
Cool,
Please provide the formula what u tried and let us know from that formula what u trying get.
That will help us achieve your goal my dear friend.
- MarcelBeug9 years ago
Community Champion
An advanced Power Query solution below, creating a record for each possible combination SKU/Warehouse/Date with quantities and balances.
It is assumed that your data has unique SKU/Warehouse/Date combinations (i.e. max 1 transaction per SKU / Warehouse / Date), otherwise a grouping step must be added.
If your data set is large, you should expect quite some runtime, but I already managed to reduce runtime by adding helper columns PrevSKU and PrevWarehouse with the values from the previous rows: Power Query is much faster when values on 1 row can be compared with each other, rather than values on different rows,
let Source = Excel.CurrentWorkbook(){[Name="Transactions"]}[Content], Typed1 = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Warehouse", type text}, {"SKU", Int64.Type}, {"Qty", Int64.Type}}), UniqueSKUs = Table.Distinct(Typed1, {"SKU"}), RemovedOthers = Table.SelectColumns(UniqueSKUs,{"SKU"}), WarehouseLists = Table.AddColumn(RemovedOthers, "Warehouse", each List.Distinct(Table.Column(Typed1,"Warehouse"))), ExpandedWarehouse = Table.ExpandListColumn(WarehouseLists, "Warehouse"), DateLists = Table.AddColumn(ExpandedWarehouse, "Date", each List.Distinct(Table.Column(Typed1,"Date"))), ExpandedDate = Table.ExpandListColumn(DateLists, "Date"), Merged = Table.NestedJoin(ExpandedDate,{"SKU", "Warehouse", "Date"},Typed1,{"SKU", "Warehouse", "Date"},"NewColumn",JoinKind.LeftOuter), ExpandedQty = Table.ExpandTableColumn(Merged, "NewColumn", {"Qty"}, {"Qty"}), ReplacedNullsWithZero = Table.ReplaceValue(ExpandedQty,null,0,Replacer.ReplaceValue,{"Qty"}), SortedSKUWHSDate = Table.Sort(ReplacedNullsWithZero,{"SKU", "Warehouse", "Date"}), SKUWarehouse = Table.SelectColumns(SortedSKUWHSDate,{"SKU", "Warehouse"}), PrevSKUWarehouse = Table.InsertRows(SKUWarehouse,0,{[SKU = null,Warehouse = null]}), AddedPrevSKUWarehouse = Table.FromColumns({Table.ToRecords(SortedSKUWHSDate),Table.ToRecords(PrevSKUWarehouse)}), Expanded1 = Table.ExpandRecordColumn(AddedPrevSKUWarehouse, "Column1", {"SKU", "Warehouse", "Date", "Qty"}, {"SKU", "Warehouse", "Date", "Qty"}), Expanded2 = Table.ExpandRecordColumn(Expanded1, "Column2", {"SKU", "Warehouse"}, {"Column2.SKU", "Column2.Warehouse"}), RenamedColumns1 = Table.RenameColumns(Expanded2,{{"Column2.SKU", "PrevSKU"}, {"Column2.Warehouse", "PrevWarehouse"}}), RemovedBottomRow = Table.Buffer(Table.RemoveLastN(RenamedColumns1,1)), Balance = List.Generate( () => [Counter = 0, Balance = RemovedBottomRow[Qty]{0}], each [Counter] < Table.RowCount(RemovedBottomRow), each [Counter = [Counter] + 1, Balance = if RemovedBottomRow[SKU]{Counter} = RemovedBottomRow[PrevSKU]{Counter} and RemovedBottomRow[Warehouse]{Counter} = RemovedBottomRow[PrevWarehouse]{Counter} then [Balance] + RemovedBottomRow[Qty]{Counter} else RemovedBottomRow[Qty]{Counter}], each [Balance]), AddedBalance = Table.FromColumns({Table.ToRecords(RemovedBottomRow),Balance}), Expanded3 = Table.ExpandRecordColumn(AddedBalance, "Column1", {"SKU", "Warehouse", "Date", "Qty", "PrevSKU", "PrevWarehouse"}, {"SKU", "Warehouse", "Date", "Qty", "PrevSKU", "PrevWarehouse"}), RenamedColumns2 = Table.RenameColumns(Expanded3,{{"Column2", "Balance"}}), RemovedColumns = Table.RemoveColumns(RenamedColumns2,{"PrevSKU", "PrevWarehouse"}), Typed2 = Table.TransformColumnTypes(RemovedColumns,{{"Warehouse", type text}, {"Date", type date}, {"Balance", Int64.Type}}) in Typed2