Forum Discussion
Using a virtual table to filter another table in SUMX function
- 5 years ago
hello dear littlemojopuppy ,
Thank you very much for your helping and efforts! I really appreciate this!
This formula doesn't work because it displayes the same value on each day.
However, I was able to workout with subquery and it works as expected now.
So, how it works?
I need to summarize all sales for all periods but only for Products that are in stock on a specific day
Grand total revenue in stock only = Calculate( SUMX( Filter( All(Sales), Sales[ProductId] in <PRODUCTS IN STOCK ON THIS DAY> ) ) )"PRODUCTS IN STOCK ON THIS DAY" - is a one column table or a subquery.
To create this subquery I use SUMMARIZE function and it works good:
var TabStock = SUMMARIZE( Stocks, Stocks[ProductId], "Stock_QTY", MAXX( Filter(Stocks, Stocks[Date] = MAXX( FILTER( Stocks, Stocks[Date] <= d ), Stocks[Date] ) ), Stocks[Stocks]) )The problem is SUMMARIZE returns a table with two columns (ProductID and Stock_QTY) and that is why the table cannot be used in "IN" clouse.
To improve this I have to make a one column table from the two columns table. To do this I use SELECTCOLUMNS function:
var d = MAX('Date'[Date]) var TabStock = SELECTCOLUMNS( FILTER( //tab creation section begin SUMMARIZE( Stocks, Stocks[ProductId], "Stock_QTY", MAXX( Filter(Stocks, Stocks[Date] = MAXX( FILTER( Stocks, Stocks[Date] <= d ), Stocks[Date] ) ), Stocks[Stocks]) ), //tab creation section END [Stock_QTY] > 0 ), "ProductId", [ProductId] )Now my measure with the subquery should work:
Thanks a lot for your assistens and time!
Best regards,
Slava.
Sorry about asking if I was doing your homework...the word "lab" in the file name and the limited amount of data made me wonder. Hope I didn't offend 🙂
Ok...download what I did from here.
First, I changed your data model to look like this. Calendar/Date and Products are dimensions for both Sales and Stocks.
I also created a different date table using the CALENDARAUTO() function instead of what you created in Power Query...I didn't want to screw up importing any of the other data.
Some measures...
Total Revenue = SUM(Sales[Revenue])
Revenue In Stock Only =
VAR InventoryOnHand =
SUMMARIZE(
'Calendar',
'Calendar'[Date],
"InventoryOnHand",
[Inventory On Hand] + 0
)
RETURN
CALCULATE(
[Total Revenue],
FILTER(
InventoryOnHand,
[InventoryOnHand] <> 0
)
)
Revenue Out of Stock Only =
VAR InventoryOnHand =
SUMMARIZE(
'Calendar',
'Calendar'[Date],
"InventoryOnHand",
[Inventory On Hand] + 0
)
RETURN
CALCULATE(
[Total Revenue],
FILTER(
InventoryOnHand,
[InventoryOnHand] = 0
)
)
Grand Total Revenue =
CALCULATE(
[Total Revenue],
ALL('Date')
)
Grand Total Revenue In Stock Only =
CALCULATE(
[Revenue In Stock Only],
ALL('Date')
)
Grand Total Revenue Out of Stock Only =
CALCULATE(
[Revenue Out of Stock Only],
ALL('Date')
)
One question I have for you to clarify is that I'm assuming that if there is a record in Stocks for a given date and product, that means it's inventory on hand, and if not, it's inventory out of stock. Is that true? Reason I'm asking is that if you compare my output of 11/15 and 11/16 I get different results than your expected results posted above...
- SlavaSha75 years agoHelper I
thank you for your quick reply! I really appreciate this.
I understand your concern, These are my real data sets:
"
One question I have for you to clarify is that I'm assuming that if there is a record in Stocks for a given date and product, that means it's inventory on hand, and if not, it's inventory out of stock. Is that true? Reason I'm asking is that if you compare my output of 11/15 and 11/16 I get different results than your expected results posted above...
"
The reason of the difference is I assume if there is no records on the date on the Stocks table then I take the nearest value. So, for 11 / 15 we have a not zero stocks for ProductID = 1 and the next record is on 11 / 20 with zero in stock. You assume that if we have no record on the date it means zero.
To get nearest stock value on the date I use this form
var TabOutOfStock = SUMMARIZE( Stocks, Stocks[ProductId], "Stock_QTY", MAXX( Filter(Stocks, Stocks[Date] = MAXX( FILTER( Stocks, Stocks[Date] <= max('Date'[Date]) ), [Date] ) ), Stocks[Stocks]) )and that is why I didn't join the Stock.Date column with the Date table.
- littlemojopuppy5 years agoCommunity Champion
To make sure I understand this correctly, what you're saying is that in the absence of a value on for a given date/product, you're assuming that the most recent inventory balance is still valid?
Listen...my wife starts her last semester of grad school tonight and she's already taken over the office. Can't answer tonight but will do so tomorrow. Fair?
- littlemojopuppy5 years agoCommunity Champion
By the way...those are really big PBI files. Wondering if there might be another solution for them