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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
marko24
New Member

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
New Member

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
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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

Top Solution Authors