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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Anonymous
Not applicable

How to count rows of items with zero quantity where not all rows are present in fact table

Hello,

 

The issue with the fact table I have to use is that when an item is no longer in stock (0 quantity), after a few consecutive days of zero rows, the table stops recording the other zeros. If the fact table is complete, I could just use something like this

COUNTROWS( FILTER(WhsInvTable, SUM(Quantity) = 0 && WhsInvTable[DateUpdated] = MAX(WhsInvTable[DateUpdated]) ) )

 

to get the count of items with zero quantity on the latest date.

 

But in this situation, the fact table looks like this:

Assume latest date is 5/17/2023

DateUpdatedItemQuantity
5/13/2023123442
5/14/2023123435
5/15/2023123434
5/16/2023123428
5/17/2023123422
5/13/202398765
5/14/202398762
5/15/202398760
5/16/202398760

 

Notice there is no row for item 9876 for 5/17/2023. What I came up with is to use a measure to find the last date an item has quantity. If the last date with quantity does not equal the max date, then that item has zero quantity.

 

ItemLast date with quantityMax DateLast Date <> Max Date
12345/17/2023 (22)5/17/2023no
98765/14/2023 (2)5/17/2023yes

 

Get Latest Qty =
var getlatestqty = CALCULATEMAX(WhsInvTbl[DateUpdated]), FILTER(WhsInvTbl, SUM(Quantity) > 0) )
return
IF(DATEDIFF(getlatestqty, MAX(WhsInvTbl[DateUpdated]), DAY) <> 0, "OOS", "In Stock")
 
Everything looks okay but how do I take that measure and do a count to see the total number of OOS (Out of Stock) items? Ideally, I want to place a visual which shows the number of OOS items. For example, on 5/17/2023, 1 item is OOS (item 9876).
1 ACCEPTED SOLUTION
johnt75
Super User
Super User

I think you can create a quantity measure like

Qty =
VAR CurrentDate =
    MAX ( 'Date'[Date] )
VAR Result =
    CALCULATE (
        SUM ( 'Table'[Quantity] ),
        'Table'[Date] = CurrentDate,
        REMOVEFILTERS ( 'Date'[Date] )
    )
RETURN
    Result

and then count the number of items with no stock as 

Out of stock =
COUNTROWS ( FILTER ( VALUES ( Items[Item ID] ), [Qty] = 0 ) )

The [Qty] measure will return blank if there are no rows, but blank = 0 so should get picked up by the filter statement.

If you wanted the [Qty] measure to return 0 instead of blank for some reason you could simply return Result + 0.

View solution in original post

3 REPLIES 3
johnt75
Super User
Super User

I think you can create a quantity measure like

Qty =
VAR CurrentDate =
    MAX ( 'Date'[Date] )
VAR Result =
    CALCULATE (
        SUM ( 'Table'[Quantity] ),
        'Table'[Date] = CurrentDate,
        REMOVEFILTERS ( 'Date'[Date] )
    )
RETURN
    Result

and then count the number of items with no stock as 

Out of stock =
COUNTROWS ( FILTER ( VALUES ( Items[Item ID] ), [Qty] = 0 ) )

The [Qty] measure will return blank if there are no rows, but blank = 0 so should get picked up by the filter statement.

If you wanted the [Qty] measure to return 0 instead of blank for some reason you could simply return Result + 0.

Anonymous
Not applicable

This seems like what I need. But I can't wrap my head around how that REMOVEFILTERS function work. Can you explain a little to me? I've seen other examples using sales which make sense to me but I'm confused with it using on dates.

Its only really necessary if you have a relationship from your date table to your fact table. If you did have that relationship then when you include columns from the date table in a chart, e.g. year month, then by default it would aggregate all the values for that year month, rather than just showing the value for the last date. By removing the filters from the date table and explicitly placing a filter to show the value for the last date you are overriding that behaviour.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.