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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
joshua1990
Post Prodigy
Post Prodigy

COUNT order below specific state and no records between changes

Hello!

I have a transaction table that shows me the order, state and the date when a state was changed:

Order NumberStateValueDate Changed
999815001.07.2021
999825005.07.2021
999835008.07.2021
999915005.07.2021
999925006.07.2021

 

This table is linked by a date table.

Now I would like to count the number of order for each date where the state is below 3.

As you can see there are no records between some day since the state was not changed.

 

How can this be calculated in dax?

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @joshua1990 
Here is a sample file with the solution https://we.tl/t-FABvxI5sFa

# Of Orders (Status < 3) = 
SUMX ( 
    'Table',
    VAR CurrentDate = VALUES ( 'Date'[Date] )
    VAR CurrentState = 
        'Table'[State]
    VAR StartDate = 
        'Table'[Date Changed]
    VAR NextDate = 
        CALCULATE ( MAX ( 'Table'[Date Changed] ), 'Table'[State] = CurrentState + 1, ALLEXCEPT ( 'Table', 'Table'[Order Number] ) )
    VAR EndDate = 
        IF ( ISBLANK ( NextDate ), StartDate, NextDate - 1 )
    VAR CurrentStatusDates =
        CALENDAR ( StartDate, EndDate )
    VAR Result =
        COUNTROWS ( INTERSECT ( CurrentStatusDates, CurrentDate ) )
    RETURN
        IF ( Result > 0 && CurrentState < 3, 1 )
)

1.png

View solution in original post

6 REPLIES 6
tamerj1
Super User
Super User

Hi @joshua1990 
Here is a sample file with the solution https://we.tl/t-FABvxI5sFa

# Of Orders (Status < 3) = 
SUMX ( 
    'Table',
    VAR CurrentDate = VALUES ( 'Date'[Date] )
    VAR CurrentState = 
        'Table'[State]
    VAR StartDate = 
        'Table'[Date Changed]
    VAR NextDate = 
        CALCULATE ( MAX ( 'Table'[Date Changed] ), 'Table'[State] = CurrentState + 1, ALLEXCEPT ( 'Table', 'Table'[Order Number] ) )
    VAR EndDate = 
        IF ( ISBLANK ( NextDate ), StartDate, NextDate - 1 )
    VAR CurrentStatusDates =
        CALENDAR ( StartDate, EndDate )
    VAR Result =
        COUNTROWS ( INTERSECT ( CurrentStatusDates, CurrentDate ) )
    RETURN
        IF ( Result > 0 && CurrentState < 3, 1 )
)

1.png

Whitewater100
Solution Sage
Solution Sage

Hi:

You can use something like

Count of State < 3 = 

COUNTROWS(FILTER(Table, Table[State] < 3))
 
You would sub in your table name where I have table. I hope this helps..

@Whitewater100 : This will not work. For instance 07.01.2022 I will get now value, right? Because there is no record for 07.01.2022. But order 9998 still has a state below 3, right?

Hi:

Try thiscalculated column and then a measure. I hope this solves your question..

Calc Col 

Count of State < 3 =
IF('Table'[State] <3,1,BLANK())
Measure
No. Orders ❤️ =
SUM('Table'[Count of State < 3])
Whitewater100_0-1651447485898.pngWhitewater100_1-1651447552400.png

 

Hi:

I don't know how the heart snuck in there! 

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!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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.