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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
SlavaSha7
Helper I
Helper I

Using a virtual table to filter another table in SUMX function

Hello dears,

I have a sales table with fields:

* Date

* ProductID

* Revenue

 

and a Stocks table with fields:

* Stock Date

* ProductID

* InStock_Quantity

 

I need to calculate year revenue of products that in stock by day, for example,

DateRevenue In StockRevenue Out of Stock
12/01/20200.7M0.3M
12/02/20200.69M0.31M
12/03/20200.8M0.2M

 

i.e. the total revenue of 2020 year is 1M, on 1st Dec we had some products that made 0.7M in our yearly revenue. We had another set of products on the 2th Dec that made 0.69M in our yearly revenue.

 

I have created a virtual table to get stocks by products by day

 

var TabStock = 
SUMMARIZE(
    Stocks,
    Stocks[ProductId],
    "Stock_QTY",
    MAXX(
        Filter(Stocks,
        Stocks[Stock Date] = MAXX(
                        FILTER(
                            Stocks,
                            Stocks[Stock Date] <= max('Date'[Date])
                            ),
                        [Stock Date]
                        )
                ), 
        Stocks[Max In Stock])
    )

 

 

Now I would like to filter the Sales table by products that are in stock using the above virtual  table, however I cannot write a right DAX expression for IN condition. Perhaps my issue is I must create a physical table and it's not possible to use a virtual table in the expression to filter anoter table.

 

So my result query is:

 

return
Calculate(
    SUMX(
        FILTER(Sales,
            Sales[ProductId] in VALUES(
                                                Filter(Allexcept(
                                                    TabStock, 
                                                    [ProductID]), 
                                                    TabStock[Stock_QTY] > 0
                                                )
                                                )
        ),
    Sales[Revenue]
    )
    )

 

 

I cannot work out with the right part of the IN section or perhaps I should use completely another approach.

If simplify, my measure should be like this:

 

Select
  Date,
  sum(revenue) as RevenueInStock
from Sales S
Where S.ProductID in
      (
        select distinct(S.ProductID) from Stocks S where S.Stock_Quantity > 0
      )

 

 

Thank you for advance.

 

Best regards,

Slava

 

1 ACCEPTED SOLUTION

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.

SlavaSha7_0-1609853860042.png

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.

View solution in original post

25 REPLIES 25

Is this your homework???  🤔

@littlemojopuppy no  🙂

I have a big PBX file with ~2M rows and to simplify my queries I have created a very simple file then I am going to copy formulas to my real file.

littlemojopuppy
Community Champion
Community Champion

Hi!  Try this instead...

VAR	InventoryInStock =
	FILTER(
		ALLEXCEPT(
			TabStock, 
			[ProductID]
		), 
		TabStock[Stock_QTY] > 0
	)
RETURN
				
CALCULATE(
	[Total Sales],
	InventoryInStock
)

Hi @littlemojopuppy ,

Thank you for your reply,

It seams it doesn't work.

 

At least [ProductId] should be TabStock[ProductId]

and what is the [Total Sales] in your CALCULATE clause? Is it a measure that sum sales up?

 

Slava

I wrote some DAX that if you adapt it to your data model, it should work.

 

[Total Sales] would be SUM([the revenue field].  And try TabStock[ProductId] in place of where I put [Product ID]

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors