Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now
Can anybody please help with a DAX question. I have a report in Excel that counts the items that make up an invoice. I have atached a screenshot with the Excel formula highlighted. I am trying to recreate this report in Power BI but struggling with the DAX for this measure.
Hi @bidataservices
Please refer to attached sample file with the solution
Item Count =
IF (
'Table'[InvoiceType] <> "Item",
COUNTROWS (
CALCULATETABLE (
'Table',
'Table'[InvoiceType] = "Item",
ALLEXCEPT ( 'Table', 'Table'[Document], 'Table'[Item], 'Table'[City] )
)
) + 0
)
Thanks, that worked well.
What if I wanted to do a SUMIFS on sales?
@bidataservices
You mean like this?
Item Sales =
IF (
'Table'[InvoiceType] <> "Item",
SUMX (
CALCULATETABLE (
'Table',
'Table'[InvoiceType] = "Invoice",
ALLEXCEPT ( 'Table', 'Table'[Document], 'Table'[Item], 'Table'[City] )
),
'Table'[Sales]
) + 0
)
You could consider creating a calculated column as well which then you could build a measure on...
Items =
VAR CountV = CALCULATE(COUNTA(Inv[InvoiceNo ]),FILTER('Inv','Inv'[Category] = EARLIER('Inv'[Category]) && 'Inv'[Document Item] = EARLIER('Inv'[Document Item]) && Inv[Subcategory]=EARLIER('Inv'[Subcategory])))
RETURN IF('Inv'[InvoiceType ]= "Invoice",CountV,BLANK())
If you don't want to create a calculated column you could use the same formula to create a Summarize Table variable with SUMX to bring directly into a measure.
Items Total =
VAR tTable = SUMMARIZE('Inv',Inv[Cutomer ],Inv[Category],Inv[Subcategory],Inv[Document Item],"Items",SUMX('Inv',IF(Inv[InvoiceType ]="Item",1,0)))
RETURN SUMX(tTable,[Items])
Thanks, I have tried the calculated column and it is nearly there but not quite right. As you can see from the results below. It should be counting 3 items, as one of the locations is Manchester. How does the EARLIER function work?
Just add another condition,
&& Inv[City]=EARLIER('Inv'[City])You will want to add any conditions that make the record the same as the previous. If my anyswer helped, please mark it.
Have you considered pulling the data in and using Power Query to Group and Sum. Steps would be in Power Query:
try like:
try like:
column =
VAR _count =
COUNTROWS(
FILTER(
TableName,
TableName[InvoiceType]="Item"
&&TableName[Document]=EARLIER(TableName[Document])
&&TableName[Item]=EARLIER(TableName[Item])
&&TableName[City]=EARLIER(TableName[City])
)
)
RETURN
IF(
[InvoiceType]<>"Item",
_count
)
it worked like:
please try
Item Count =
IF (
'Table'[Invoice Type] <> "Item",
COUNTROWS (
CALCULATETABLE (
'Table',
ALLEXCEPT ( 'Table', 'Table'[Document], 'Table'[Item], 'Table'[City] )
)
)
)
tried to verify tamerj's code, it worked like:
Item Count =
IF (
TableName[InvoiceType] <> "Item",
COUNTROWS (
CALCULATETABLE (
TableName,
TableName[InvoiceType] = "Item",
ALLEXCEPT ( TableName, TableName[Document], TableName[Item], TableName[City] )
)
)
)
With CALCULATETABLE+ALLEXCEPT, the code is much more concise.
Check out the November 2025 Power BI update to learn about new features.
Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!
| User | Count |
|---|---|
| 11 | |
| 10 | |
| 9 | |
| 7 | |
| 6 |
| User | Count |
|---|---|
| 26 | |
| 22 | |
| 19 | |
| 17 | |
| 10 |