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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
vpsoini
Helper I
Helper I

Counting and searching rows with common values?

I seem to bump up regurarly with requirements, where I need to search and/or count some values from rows, that share a common values in some column. 

 

I have tried to learn how to use CALCULATE / COUNTROWS / CONTAINSTRING , but combining all those still puzzles me.  Below is a small table where some rows share a common value (here ordernumber) :

 

Table "orders"

Ordernumber ItemDescription

Serial number

QuantityDate
001A1234Machine 11000123110.10.2021
001O1234Option 1234 110.10.2021
001O2345Option 2345 210.10.2021
002A2345Machine 20000234113.10.2021
002O2345Option 2345 113.10.2021
003A1234Machine 11000124115.10.2021
003O2345Option 2345 215.10.2021
003O3456Option 3456 215.10.2021
003O4567Option 4567 115.10.2021
003O5678Option 5678 115.10.2021
004A3456Machine 25000256115.10.2021
004O1234Option 1234 115.10.2021
004O2345Option 2345 215.10.2021
005A2345Machine 20000235119.10.2021
005O2345Option 2345 119.10.2021
006A1234Machine 11000125121.10.2021
006O4567Option 4567 221.10.2021

 

Could you share the correct format for following cases

 

  • How many orders have "Machine 110" with "Option 4567" ?   (answer = 2 , orders 003 and 006)
  • How many "Option 2345" was ordered with 200-series machines (200/250) between 10.-15.10.2021 ? (answer = 3 , 1 in order 002 and 2 in order 004. Order 005 is out of date range) , If you could provide this sample with using CONTAINSTRING to find strings "200" and "250" , instead of exact full machine names...

And bit more complicated case: If we have a related table e.g. customer info linked with serial number:

 

Table "Customers"

NameAddressCountrySerial numberOrder paid
Customer 1Street 1 , City 1FI00123OK
Customer 2Street 2, Cisty 2SE00234 
Customer 3Street 3, City 3UK00124OK
Customer 4Street 4, City 4FI00256OK
Customer 5Street 5, City 5DE00235 
Customr 6Street 6, City 6DE00125OK

 

Sample question could be:

 

  • How many Option 1234 has bee sold to FI ?  (answer = 2, one in order 001 and another in order 004)

Those samples would give me enough variants to play with and adapt to.  Some text around solution, why it was made would also be much appreciated (for learning purposes)

 

Thanks a 10^6 in advance 🙂

 

ps. sorry, but I cannot seem to get tables to maintain their correct look, but this always puts them in funny format...

1 ACCEPTED SOLUTION

Hi @vpsoini ,

 

For the first one,create 4 measures as below:

Machine 110" with "Option 4567 =
VAR _table =
    CALCULATETABLE (
        VALUES ( 'Table'[Description] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Ordernumber] = MAX ( 'Table'[Ordernumber] ) )
    )
RETURN
    IF ( "Machine 110" IN _table && "Option 4567" IN _table, 1, 0 )
Measure =
CALCULATE (
    DISTINCTCOUNT ( 'Table'[Ordernumber] ),
    FILTER ( VALUES ( 'Table'[Ordernumber] ), [Machine 110" with "Option 4567] = 1 )
)
Option 2345 with 200-series machines (200/250) =
VAR _table =
    CALCULATETABLE (
        VALUES ( 'Table'[Description] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Ordernumber] = MAX ( 'Table'[Ordernumber] ) )
    )
RETURN
    IF (
        ( "Machine 200"
            IN _table
            || "Machine 250" IN _table )
            && "Option 2345"
                IN _table
                    && (
                        MAX ( 'Table'[Date] ) >= DATE ( 2021, 10, 10 )
                            && MAX ( 'Table'[Date] ) <= DATE ( 2021, 10, 15 )
                    ),
        1,
        0
    )
Measure2 =
CALCULATE (
    DISTINCTCOUNT ( 'Table'[Ordernumber] ),
    FILTER (
        VALUES ( 'Table'[Ordernumber] ),
        'Table'[Option 2345 with 200-series machines (200/250)] = 1
    )
)

And you will see:

vkellymsft_0-1636080571703.png

For the second one:

Create 2 measures as below:

Option 1234 has bee sold to FI =
VAR _Series =
    CALCULATETABLE (
        VALUES ( 'Table (2)'[Serial number] ),
        FILTER ( ALL ( 'Table (2)' ), 'Table (2)'[Country] = "FI" )
    )
VAR _Numbers =
    CALCULATETABLE (
        VALUES ( 'Table'[Ordernumber] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Serial number] IN _Series )
    )
VAR _Description =
    CALCULATETABLE (
        VALUES ( 'Table'[Description] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Ordernumber] = MAX ( 'Table'[Ordernumber] )
                && 'Table'[Ordernumber] IN _Numbers
        )
    )
RETURN
    IF ( "Option 1234" IN _Description, 1, 0 )
Measure3 =
CALCULATE (
    DISTINCTCOUNT ( 'Table'[Ordernumber] ),
    FILTER (
        VALUES ( 'Table'[Ordernumber] ),
        'Table'[Option 1234 has bee sold to FI] = 1
    )
)

And you will see:

vkellymsft_1-1636081296031.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

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

 

View solution in original post

6 REPLIES 6
Greg_Deckler
Community Champion
Community Champion

@vpsoini Third one:

Measure =
  VAR __Table = ADDCOLUMNS('Table',"__Country",RELATED('Table2'[Country])
  VAR __FI = DISTINCT(SELECTCOLUMNS(FILTER(__Table,[Country]="FI"),"__ON",[Ordernumber]))
  VAR __Option1234 = DISTINCT(SELECTCOLUMNS(FILTER(__Table,[Description]="Option 1234"),"__ON",[Ordernumber]))
RETURN
  COUNTROWS(INTERSECT(__FI,__Option1234))


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Greg_Deckler
Community Champion
Community Champion

@vpsoini Second one:

Measure 9 = 
    VAR __Machine2 = DISTINCT(SELECTCOLUMNS(FILTER('Table6',[Date]>=DATE(2021,10,10) && [Date]<=DATE(2021,10,15) && (CONTAINSSTRING([Description],"Machine 200") || CONTAINSSTRING([Description],"Machine 250"),"__ON",[Ordernumber ]))
    VAR __Option2345 = DISTINCT(SELECTCOLUMNS(FILTER('Table6',[Date]>=DATE(2021,10,10) && [Date]<=DATE(2021,10,15) && [Description]="Option 2345"),"__ON",[Ordernumber ]))
RETURN
    COUNTROWS(INTERSECT(__Machine2,__Option2345))


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Greg_Deckler
Community Champion
Community Champion

@vpsoini For the first one, try:

Measure 9 = 
    VAR __Machine110 = DISTINCT(SELECTCOLUMNS(FILTER('Table6',[Description]="Machine 110"),"__ON",[Ordernumber ]))
    VAR __Option4567 = DISTINCT(SELECTCOLUMNS(FILTER('Table6',[Description]="Option 4567"),"__ON",[Ordernumber ]))
RETURN
    COUNTROWS(INTERSECT(__Machine110,__Option4567))


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Hi @Greg_Deckler 

 

Thanks for the suggestion. However, when I test that based on serial number and option (where I should get "1" as result) , the above solution gives me "2"  Does the COUNTROWS count rows with both matches (line with certain serial number + the option line attached to that machine (serial)?  So should I have " -1" in order to get the option only?

What does that "__ON" do in those variables?

 

Thanks again 🙂

Hi @vpsoini ,

 

For the first one,create 4 measures as below:

Machine 110" with "Option 4567 =
VAR _table =
    CALCULATETABLE (
        VALUES ( 'Table'[Description] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Ordernumber] = MAX ( 'Table'[Ordernumber] ) )
    )
RETURN
    IF ( "Machine 110" IN _table && "Option 4567" IN _table, 1, 0 )
Measure =
CALCULATE (
    DISTINCTCOUNT ( 'Table'[Ordernumber] ),
    FILTER ( VALUES ( 'Table'[Ordernumber] ), [Machine 110" with "Option 4567] = 1 )
)
Option 2345 with 200-series machines (200/250) =
VAR _table =
    CALCULATETABLE (
        VALUES ( 'Table'[Description] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Ordernumber] = MAX ( 'Table'[Ordernumber] ) )
    )
RETURN
    IF (
        ( "Machine 200"
            IN _table
            || "Machine 250" IN _table )
            && "Option 2345"
                IN _table
                    && (
                        MAX ( 'Table'[Date] ) >= DATE ( 2021, 10, 10 )
                            && MAX ( 'Table'[Date] ) <= DATE ( 2021, 10, 15 )
                    ),
        1,
        0
    )
Measure2 =
CALCULATE (
    DISTINCTCOUNT ( 'Table'[Ordernumber] ),
    FILTER (
        VALUES ( 'Table'[Ordernumber] ),
        'Table'[Option 2345 with 200-series machines (200/250)] = 1
    )
)

And you will see:

vkellymsft_0-1636080571703.png

For the second one:

Create 2 measures as below:

Option 1234 has bee sold to FI =
VAR _Series =
    CALCULATETABLE (
        VALUES ( 'Table (2)'[Serial number] ),
        FILTER ( ALL ( 'Table (2)' ), 'Table (2)'[Country] = "FI" )
    )
VAR _Numbers =
    CALCULATETABLE (
        VALUES ( 'Table'[Ordernumber] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Serial number] IN _Series )
    )
VAR _Description =
    CALCULATETABLE (
        VALUES ( 'Table'[Description] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Ordernumber] = MAX ( 'Table'[Ordernumber] )
                && 'Table'[Ordernumber] IN _Numbers
        )
    )
RETURN
    IF ( "Option 1234" IN _Description, 1, 0 )
Measure3 =
CALCULATE (
    DISTINCTCOUNT ( 'Table'[Ordernumber] ),
    FILTER (
        VALUES ( 'Table'[Ordernumber] ),
        'Table'[Option 1234 has bee sold to FI] = 1
    )
)

And you will see:

vkellymsft_1-1636081296031.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

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

 

@vpsoini 

  • "How many orders have "Machine 110" with "Option 4567" ?   (answer = 2 , orders 003 and 006)"

"__ON" is simply the name of the column in the single column table created by SELECTCOLUMNS, it can be pretty much anything as long as both are the same.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
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!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

Top Solution Authors