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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
PBIUWO
Helper III
Helper III

How to calculate count of repeating Sales Orders with conditions?

Hi, 

 

I have a table of Sales Order data. Order Number, Item Code, and a Flag.

It contains a flag with 0 or 1 value.

 

OrderNumberItem CodeFlag
123ABC1
123CDE0
456CDE0
456GHJ0
789GHJ0
111ABC1

 

The Order Numbers can be repeated, but I want to distinct count the Order Numbers with the following conditions.

1. How many Orders Numbers contain both the 0 & 1 flag. (Answer from the Example is 1)  

2. How many Orders contain "0" Flag, (Answer from the Example is 3)

3, How many Orders contain "1" Flag. (Answer from the example is 2)

4. How many Orders contain ONLY the "1" Flag (Answer from the example is 1)

5. How many Orders contain ONLY the "0" Flag (Answer from the example is 2) 

 

It'll be nice to also have a column beside the flag, where it says "Mixed" (has both 0 & 1), "1" (Has ONLY 1), "0" (Has ONLY 0) for the row.  

 

I'm able to create a column where I can show if an order contains a 1, but I realize that there can be Order Numbers where it only contains the 1 and not a mix (1 and 0) 

I'm able to think that a "virtual table" can solve this but am running into the problem of populating the value with the filters.

 

Thank you in Advance! 

1 ACCEPTED SOLUTION
jdbuchanan71
Super User
Super User

@PBIUWO 
I have attached a sample .pbix with the measures for each of the results and the calculated column.

The measures are just different compares for the counts of 0's and 1's

0 & 1 = 
VAR _0 = CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 0 )
VAR _1 = CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 1 )
RETURN 
    COUNTROWS( INTERSECT ( _0 , _1 ) )
0 = 
VAR _0 = CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 0 )
VAR _1 = CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 1 )
RETURN 
    COUNTROWS( _0 )
1 Only = 
VAR _0 = CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 0 )
VAR _1 = CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 1 )
RETURN 
    COUNTROWS( EXCEPT ( _1, _0 ) )

jdbuchanan71_0-1634190631361.png

 


The calculated column is like this.

Check = 
VAR _0 = COUNTROWS ( CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 0, ALLEXCEPT ( 'Table', 'Table'[OrderNumber] ) ) )
VAR _1 = COUNTROWS ( CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 1, ALLEXCEPT ( 'Table', 'Table'[OrderNumber] ) ) )
RETURN 
    SWITCH (
        TRUE(),
        _0 > 0 && _1 > 0, "Has Both",
        _0 > 0 && _1 = 0, "0 Only",
        _0 = 0 && _1 > 0, "1 Only"
    )

 

View solution in original post

3 REPLIES 3
jdbuchanan71
Super User
Super User

@PBIUWO 
I have attached a sample .pbix with the measures for each of the results and the calculated column.

The measures are just different compares for the counts of 0's and 1's

0 & 1 = 
VAR _0 = CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 0 )
VAR _1 = CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 1 )
RETURN 
    COUNTROWS( INTERSECT ( _0 , _1 ) )
0 = 
VAR _0 = CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 0 )
VAR _1 = CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 1 )
RETURN 
    COUNTROWS( _0 )
1 Only = 
VAR _0 = CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 0 )
VAR _1 = CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 1 )
RETURN 
    COUNTROWS( EXCEPT ( _1, _0 ) )

jdbuchanan71_0-1634190631361.png

 


The calculated column is like this.

Check = 
VAR _0 = COUNTROWS ( CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 0, ALLEXCEPT ( 'Table', 'Table'[OrderNumber] ) ) )
VAR _1 = COUNTROWS ( CALCULATETABLE ( DISTINCT ( 'Table'[OrderNumber] ), 'Table'[Flag] = 1, ALLEXCEPT ( 'Table', 'Table'[OrderNumber] ) ) )
RETURN 
    SWITCH (
        TRUE(),
        _0 > 0 && _1 > 0, "Has Both",
        _0 > 0 && _1 = 0, "0 Only",
        _0 = 0 && _1 > 0, "1 Only"
    )

 

Thank you! 

 

I totally forgot about the Calculatetable(). 

amitchandak
Super User
Super User

@PBIUWO , try measure like

 

contain both the 0 & 1 =
countx(filter(summarize(Table, Table[OrderNumber], "_1", calculate(distinctcount(Table[Flag]),filter(Table, Table[Flag] in {0,1}))) , [_1]=2),[OrderNumber])

 

contain only 0 =
countx(filter(summarize(Table, Table[OrderNumber], "_1", calculate(distinctcount(Table[Flag]),filter(Table, Table[Flag] in {0})) , "_2",distinctcount(Table[Flag]) ) , [_1]=[_2]),[OrderNumber])

 


contain only 1 =
countx(filter(summarize(Table, Table[OrderNumber], "_1", calculate(distinctcount(Table[Flag]),filter(Table, Table[Flag] in {1})) , "_2",distinctcount(Table[Flag]) ) , [_1]=[_2]),[OrderNumber])

 

contain 0 =calculate(distinctcount(Table[Flag]),filter(Table, Table[Flag] in {0}))

 

contain 1 =calculate(distinctcount(Table[Flag]),filter(Table, Table[Flag] in {1}))

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
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! Prices go up Feb. 11th.

Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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