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.
Can you explain how you're calculating In Stock and Out of Stock in that Excel workbook?
littlemojopuppy wrote:Can you explain how you're calculating In Stock and Out of Stock in that Excel workbook?
Grand Total Revenue In Stock Only = Grand Total Revenue filtered by products that are in stock on the date.
We take Grand Total Revenue as revenue for the year (or all to simplify) and exclude revenue of products that are out of stock on the date
Particularly:
We have Grand Total Revenue (your measure)
= $5870
Grand Total Revenue =
CALCULATE(
[Total Revenue],
ALL('Calendar')
)
Then we have my formula that calculates quantity in stock by products for the date
var TabOutOfStock =
SUMMARIZE(
Stocks,
Stocks[ProductId],
"Stock_QTY",
MAXX(
Filter(Stocks,
Stocks[Date] = MAXX(
FILTER(
Stocks,
Stocks[Date] <= max('Date'[Date])
),
[Date]
)
),
Stocks[Stocks])
)
According to our test data set we have:
02 Jan 2020 we have no products in stock at all
Grand Total Revenue In Stock Only = $0
Grand Total Revenue Out of Stock Only = $5870 (grand total revenue)
The same on 3 Jan and 30 Jan
15 Nov 2020 we have in stock only Product ID = 1. Grand total revenue of ProductID=1 is $870
Grand Total Revenue In Stock Only = $870
Grand Total Revenue Out of Stock Only = $5000 (grand total revenue of product that are our of stock on 15 Nov).
16 Nov is the same
3 Dec 2020 we have no products in stock at all
Grand Total Revenue In Stock Only = $0
Grand Total Revenue Out of Stock Only = $5870 (grand total revenue)
16 Dec 2020 we have in stock only Product ID = 2. Grand total revenue of ProductID=2 is $5000
Grand Total Revenue In Stock Only = $5000
Grand Total Revenue Out of Stock Only = $870 (grand total revenue of products that are our of stock on 16 Dec).
So what we need is:
Grand total revenue in stock only =
Calculate(
SUMX(
Filter(
All(Sales),
Sales[ProductId] in <PRODUCTS IN STOCK ON THIS DAY>
)
)
)
Please let me know if I should clarify something
Thanks.