count invoices with multiple AND filters

MrAnderson

New Member
Joined
Mar 6, 2014
Messages
6
Hi,

I’m trying to count the number of instances where a customer has bought 2 particular products in the same invoice. Let’s assume I have the following table called "invoices.


ABCDE
1InvoiceIdInvoiceDateProductIDPriceProductName
2101/01/20121100a
3101/01/2012250b
4202/02/20121100a
5202/02/2012425d
6303/03/20121100a
7303/03/2012250b
8303/03/2012525e

<colgroup><col style="width: 25pxpx"><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Facturas



The result I want is the following

HI
2Jan1
3Feb0
4March1

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Facturas



I have tried with the CALCULATE function with these formulae:
=calculate(distinctcount(Invoices[InvoiceId],Invoices[ProdId]=1;Invoices[ProdId]=2)
Or
=calculate(distinctcount(Invoices[InvoiceId],Invoices[ProdId]=1&&Invoices[ProdId]=2)

but I get no results, can anyone give a hand?

thanks a lot!.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Your problem is that you do NOT want an AND, you want an OR. You want Name="Scott" or Name="Anderson"... cuz nobody has the name Scott AND Anderson.

=calculate(distinctcount(Invoices[InvoiceId]), FILTER(ALL(Invoices), Invoices[ProdId]=1 || Invoices[ProdId]=2))
 
Upvote 0
thanks Scott!

it doesn't seem to work though, if I follow your formula the result I get is "3" when it should be 2, maybe I didn't present the problem accurately, what I want is "check every invoice and for each invoice check if there are prodID 1 and prodID 2, if so, count that invoice"

thanks again.

Lucas.
 
Upvote 0
Below should give you what you want. Probably this is not the most elegant solution, but it's what I could come up with at the moment


-- removed inline image ---
 
Last edited:
Upvote 0
CALCULATE(DISTINCTCOUNT(Table1[InvoiceId]),FILTER(ALL(Table1[InvoiceId]),AND(CALCULATE(COUNT(Table1[InvoiceId]),Table1[ProductID]=1)>0,CALCULATE(COUNT(Table1[InvoiceId]),Table1[ProductID]=2)>0)))

can someone tell me how to paste the DAX Format beautified formula



 
Upvote 0
I just went to Dax Formatter by SQLBI and pasted it in, ... then copied the html version here...

=<br><span class="Keyword" style="color:#0070FF">CALCULATE</span><span class="Parenthesis" style="color:#D0D0D0"> (</span><br><span class="indent4">    </span><span class="Keyword" style="color:#0070FF">DISTINCTCOUNT</span><span class="Parenthesis" style="color:#D0D0D0"> (</span> Table1[InvoiceId] <span class="Parenthesis" style="color:#D0D0D0">)</span>,<br><span class="indent4">    </span><span class="Keyword" style="color:#0070FF">FILTER</span><span class="Parenthesis" style="color:#D0D0D0"> (</span><br><span class="indent8">        </span><span class="Keyword" style="color:#0070FF">ALL</span><span class="Parenthesis" style="color:#D0D0D0"> (</span> Table1[InvoiceId] <span class="Parenthesis" style="color:#D0D0D0">)</span>,<br><span class="indent8">        </span><span class="Keyword" style="color:#0070FF">AND</span><span class="Parenthesis" style="color:#D0D0D0"> (</span><br><span class="indent8">        </span><span class="indent4">    </span><span class="Keyword" style="color:#0070FF">CALCULATE</span><span class="Parenthesis" style="color:#D0D0D0"> (</span><br><span class="indent8">        </span><span class="indent8">        </span><span class="Keyword" style="color:#0070FF">COUNT</span><span class="Parenthesis" style="color:#D0D0D0"> (</span> Table1[InvoiceId] <span class="Parenthesis" style="color:#D0D0D0">)</span>,<br><span class="indent8">        </span><span class="indent8">        </span>Table1[ProductID] = <span class="Number" style="color:#EE7F18">1</span><br><span class="indent8">        </span><span class="indent4">    </span><span class="Parenthesis" style="color:#D0D0D0">)</span><br><span class="indent8">        </span><span class="indent8">        </span>> <span class="Number" style="color:#EE7F18">0</span>,<br><span class="indent8">        </span><span class="indent4">    </span><span class="Keyword" style="color:#0070FF">CALCULATE</span><span class="Parenthesis" style="color:#D0D0D0"> (</span><br><span class="indent8">        </span><span class="indent8">        </span><span class="Keyword" style="color:#0070FF">COUNT</span><span class="Parenthesis" style="color:#D0D0D0"> (</span> Table1[InvoiceId] <span class="Parenthesis" style="color:#D0D0D0">)</span>,<br><span class="indent8">        </span><span class="indent8">        </span>Table1[ProductID] = <span class="Number" style="color:#EE7F18">2</span><br><span class="indent8">        </span><span class="indent4">    </span><span class="Parenthesis" style="color:#D0D0D0">)</span><br><span class="indent8">        </span><span class="indent8">        </span>> <span class="Number" style="color:#EE7F18">0</span><br><span class="indent8">        </span><span class="Parenthesis" style="color:#D0D0D0">)</span><br><span class="indent4">    </span><span class="Parenthesis" style="color:#D0D0D0">)</span><br><span class="Parenthesis" style="color:#D0D0D0">)</span><br>
 
Upvote 0
And apologies for mis-reading your question. Badly ;)

Scott's Maybe Overly Verbose Solution:
Code:
Entries In Invoice:=CALCULATE(COUNTROWS(Table1), ALLEXCEPT(Table1, Table1[InvoiceId]))
Product1 Entries:=CALCULATE([Entries In Invoice], FILTER(Table1, Table1[ProductID] = 1))
Product2 Entries:=CALCULATE([Entries In Invoice], FILTER(Table1, Table1[ProductID] = 2))
Invoices with 1 and 2:=IF (CALCULATE([Entries In Invoice], FILTER(VALUES(Table1[InvoiceId]), [Product1 Entries] > 0 && [Product2 Entries] > 0)) > 0, 1, 0)
 
Upvote 0
Hi,

sorry to open this thread again, but I was asked a new requirement with the same dataset, in this case, I need to SUM the price of both the products. I have modified the formula provided by XLBob, changing DISTINCTCOUNT for SUM:
=
CALCULATE (
SUM ( Invoices[Price] ),
FILTER (
ALL ( Invoices[ProductID] ),
AND (
CALCULATE (
COUNT ( Invoices[InvoiceId] ),
Invoices[ProductID] = 1
)
> 0,
CALCULATE (
COUNT ( Invoices[InvoiceId] ),
Invoices[ProductID] = 2
)
> 0
)
)
)


it works only in 2 of the three cases

1. it sums the correct amount when there is an invoice that has only both the target products
2. it sums zero when there is an invoice with one or none of the 2 target products
3. it doesn't sum correctly in the cases where there is an invoice that has the 2 target products and one or more alternative products, in those cases it sums all the amounts in the invoice, including products that are not the target products. So in the example I posted in the first post what I get is:

HI
2Jan$150
3Feb$0
4March$175

<tbody>
</tbody>

and what I'm looking for is

HI
2Jan$150
3Feb$0
4March$150

<tbody>
</tbody>
thanks again for all the help!
 
Upvote 0
This one is tricky. I need to think really hard. In the meantime, see if Scottsen can come up with something. Actually I have also posted a question that I kind of hope Scottsen can offer some help.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top