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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Anonymous
Not applicable

Distinct count of values when there are corresponding multiple lines per value

I am trying to create a measure that count up the the number of Late  or   On Time orders.    The challenge is that each order can have multiple lines, some late, some on time.    I already have a calculated column that defines whether the line is on time or not.  

 

But our business logic states that if one line is late...the whole order is late.   So if an order has 20 lines, and even one line is late, then the Order is defined as late.    So, essentially I need help creating two measures I think.   

 

One that defines any order that is late if it has at least one late line associated with it.  

 

And second, a measure that I could use in a table or matrix that counts the distinct number of Late orders and the distinct number of On Time orders.  

 

texmexdragon_0-1632235481894.png

 

2 ACCEPTED SOLUTIONS
AlexisOlson
Super User
Super User

How about a calculated column like this?

 

OrderStatus =
VAR AllStatus =
    CALCULATETABLE (
        VALUES ( Orders[On Time or Late] ),
        ALLEXCEPT ( Orders, Orders[Order] )
    )
RETURN
    IF ( "Late" IN AllStatus, "Late", "On Time" )

 

Once you have this, it should be straightforward to write measures to count distinct orders. E.g.

LateOrderCount =
CALCULATE ( DISTINCTCOUNT ( Orders[Order] ), Orders[OrderStatus] = "Late" )

View solution in original post

Anonymous
Not applicable

Thanks @AlexisOlson  !      Your solution worked just fine....it was my error.   In my OrderCount measures I had accidentally picked "orderstatus" instead of just "order"  for the first expression.   Corrected measure is below.   

 

OnTimeOrderCount =
CALCULATE ( DISTINCTCOUNT ( ShippedOrdersALL_Query[Order] ), ShippedOrdersALL_Query[OrderStatus] = "On Time" )

texmexdragon_0-1632245415243.png

 

View solution in original post

6 REPLIES 6
AlexisOlson
Super User
Super User

How about a calculated column like this?

 

OrderStatus =
VAR AllStatus =
    CALCULATETABLE (
        VALUES ( Orders[On Time or Late] ),
        ALLEXCEPT ( Orders, Orders[Order] )
    )
RETURN
    IF ( "Late" IN AllStatus, "Late", "On Time" )

 

Once you have this, it should be straightforward to write measures to count distinct orders. E.g.

LateOrderCount =
CALCULATE ( DISTINCTCOUNT ( Orders[Order] ), Orders[OrderStatus] = "Late" )
Anonymous
Not applicable

@AlexisOlson   This looks promising but I am getting a count of "1" for Late.   Seems it is counting the distinct value  "Late"  as just one value.     Rather than the distinct count of Orders that are late. 

texmexdragon_0-1632240727717.png

 

 

That's because you have Line in your table visual creating a filter context. You'll need to add something like ALLEXCEPT ( Orders, Orders[Order] ) to the CALCULATE in the measure if you want to remove that filter context.

Anonymous
Not applicable

@AlexisOlson    Yes, understood.   Your measure works, but for the LastOrderCount, if put on a card visual, it just returns the number  "1".    This is what I was referring to.   I was expecting the number "50"  

texmexdragon_0-1632242552632.png

 

Hmm. I would expect that too if you don't have anything filtering the card.

Anonymous
Not applicable

Thanks @AlexisOlson  !      Your solution worked just fine....it was my error.   In my OrderCount measures I had accidentally picked "orderstatus" instead of just "order"  for the first expression.   Corrected measure is below.   

 

OnTimeOrderCount =
CALCULATE ( DISTINCTCOUNT ( ShippedOrdersALL_Query[Order] ), ShippedOrdersALL_Query[OrderStatus] = "On Time" )

texmexdragon_0-1632245415243.png

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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