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

View all the Fabric Data Days sessions on demand. View schedule

Reply
marko24
Frequent Visitor

How to measure a stock (using transaction date AND close date)

Hi all,

I've an issue regarding measure calculations for this table named Tableau1_transaction (yes it's a sample, the real tables includes millions of rows) :

Transaction_IDTransaction_dateLatest_StatusMatching_Status_date
11101/03/2025Match05/03/2025 18:00
22204/03/2025Open 
33304/03/2025Open 
44410/03/2025Open 
55512/03/2025Match13/03/2025 15:00

My objective is to count for each day, the stock which is the number of open transactions (i.e status =open OR status = Match with a future date )


I have created 3 measures : 

Items_created_at_date =

    CALCULATE (
        COUNTA ( Tableau1_transaction[Transaction_date] ),
        FILTER (
            ALL ( 'Calendar'[Date] ),
            ISONORAFTER (  'Calendar'[Date] , MAX (  'Calendar'[Date]  ), DESC )
        )
    )

Items_closed_at_date =
    CALCULATE (
        COUNTA ( Tableau1_transaction[Matching_Status_date] ),
        FILTER (
            ALLSELECTED ( 'Calendar2'),
            ISONORAFTER (  'Calendar2'[Date] , MAX (  'Calendar2'[Date]  ), DESC )
        )
    )
OpenItems_stock = [Items_created_at_date] - [Items_closed_at_date]


(I have created two calendar tables using CALENDARAUTO() and create several relationships :

marko24_0-1741966209878.png

 



Sadly, the result is not the expected one : the measure Items_closed_at_date is incorrect (but the Items_created_at_date is correct)
(X axis : Date from 'Calendar')

marko24_1-1741966237290.png

Indeed, my expectations are : 
marko24_2-1741966336809.png

 

any idea ??

regards,
Marc

1 ACCEPTED SOLUTION
jgeddes
Super User
Super User

Maybe something like this would work for you?

Open = 
var _vTable =
ADDCOLUMNS(
	d_date,
	"_created", COUNTX(FILTER(ALL('Table'), 'Table'[Transaction_date] <= d_date[Date]), [Transaction_date]),
	"_closed", COUNTX(FILTER(ALL('Table'), DATE(YEAR('Table'[Matching_Status_Date]),MONTH('Table'[Matching_Status_Date]), DAY('Table'[Matching_Status_Date])) <= d_date[Date]),  [Matching_Status_Date]),
	"_openItems", COUNTX(FILTER(ALL('Table'), 'Table'[Transaction_date] <= d_date[Date]), [Transaction_date]) - COUNTX(FILTER(ALL('Table'), DATE(YEAR('Table'[Matching_Status_Date]),MONTH('Table'[Matching_Status_Date]), DAY('Table'[Matching_Status_Date])) <= d_date[Date]),  [Matching_Status_Date])
)
Return
MINX(
    FILTER(_vTable, [Date] = MAX(d_date[Date])),
    [_openItems]
)

This leverages a single date table with no relationships. The variable creates a virtual table with the Open Items calculated by date. (It is also calculating created and closed as I used the same table in all three measures.)
The result would look like this...

jgeddes_0-1741972167216.png

I attached the sample pbix for you to review.
Hope this helps.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

2 REPLIES 2
marko24
Frequent Visitor

Many thanks jgeddes !!!!

jgeddes
Super User
Super User

Maybe something like this would work for you?

Open = 
var _vTable =
ADDCOLUMNS(
	d_date,
	"_created", COUNTX(FILTER(ALL('Table'), 'Table'[Transaction_date] <= d_date[Date]), [Transaction_date]),
	"_closed", COUNTX(FILTER(ALL('Table'), DATE(YEAR('Table'[Matching_Status_Date]),MONTH('Table'[Matching_Status_Date]), DAY('Table'[Matching_Status_Date])) <= d_date[Date]),  [Matching_Status_Date]),
	"_openItems", COUNTX(FILTER(ALL('Table'), 'Table'[Transaction_date] <= d_date[Date]), [Transaction_date]) - COUNTX(FILTER(ALL('Table'), DATE(YEAR('Table'[Matching_Status_Date]),MONTH('Table'[Matching_Status_Date]), DAY('Table'[Matching_Status_Date])) <= d_date[Date]),  [Matching_Status_Date])
)
Return
MINX(
    FILTER(_vTable, [Date] = MAX(d_date[Date])),
    [_openItems]
)

This leverages a single date table with no relationships. The variable creates a virtual table with the Open Items calculated by date. (It is also calculating created and closed as I used the same table in all three measures.)
The result would look like this...

jgeddes_0-1741972167216.png

I attached the sample pbix for you to review.
Hope this helps.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.

Top Solution Authors
Top Kudoed Authors