Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Hi All
In my data, I have both sales and refunds and would like to count the number of items sold on a monthly basis. If it was bought and refunded in the same month, it should be 0 items in that month, If it was bought in month 1 then refund in month 2, month 1 should still be 1 item, and month 2 should be -1.
I have temporarily used a calculated column to say if sales amount is >0 then 1, otherwise -1 but I know this isn't good practice.
Is there a COUNTIF equivalent in Dax?
https://www.dropbox.com/s/xaqe1gd5zhud1ag/Count%20Number%20of%20Transactions.pbix?dl=0
Many thanks!
Solved! Go to Solution.
Hi @swwong1 ,
You could calculate the sum of sales group by month and [Invoice No].
sum_sales =
CALCULATE (
SUM ( SalesTable[Sales] ),
FILTER (
SalesTable,
SalesTable[Invoice No.] = EARLIER ( SalesTable[Invoice No.] )
&& MONTH ( SalesTable[Date] ) = MONTH ( EARLIER ( SalesTable[Date] ) )
)
)
Then count [Invoice No] with sum_sales > 0 minus count [Invoice No] with sum_sales < 0 and group by month.
_count =
VAR a =
CALCULATE (
DISTINCTCOUNT ( SalesTable[Invoice No.] ),
FILTER (
ALLSELECTED ( SalesTable ),
SalesTable[sum_sales] > 0
&& FORMAT ( SalesTable[Date], "MMMM" ) = SELECTEDVALUE ( 'Date'[Month Name] )
)
)
VAR b =
CALCULATE (
DISTINCTCOUNT ( SalesTable[Invoice No.] ),
FILTER (
ALLSELECTED ( SalesTable ),
SalesTable[sum_sales] < 0
&& FORMAT ( SalesTable[Date], "MMMM" ) = SELECTEDVALUE ( 'Date'[Month Name] )
)
)
RETURN
a - b
Best Regards,
Jay
Hi @swwong1 ,
You could calculate the sum of sales group by month and [Invoice No].
sum_sales =
CALCULATE (
SUM ( SalesTable[Sales] ),
FILTER (
SalesTable,
SalesTable[Invoice No.] = EARLIER ( SalesTable[Invoice No.] )
&& MONTH ( SalesTable[Date] ) = MONTH ( EARLIER ( SalesTable[Date] ) )
)
)
Then count [Invoice No] with sum_sales > 0 minus count [Invoice No] with sum_sales < 0 and group by month.
_count =
VAR a =
CALCULATE (
DISTINCTCOUNT ( SalesTable[Invoice No.] ),
FILTER (
ALLSELECTED ( SalesTable ),
SalesTable[sum_sales] > 0
&& FORMAT ( SalesTable[Date], "MMMM" ) = SELECTEDVALUE ( 'Date'[Month Name] )
)
)
VAR b =
CALCULATE (
DISTINCTCOUNT ( SalesTable[Invoice No.] ),
FILTER (
ALLSELECTED ( SalesTable ),
SalesTable[sum_sales] < 0
&& FORMAT ( SalesTable[Date], "MMMM" ) = SELECTEDVALUE ( 'Date'[Month Name] )
)
)
RETURN
a - b
Best Regards,
Jay
@swwong1 , Try a new column like
Column = var _mon = EOMONTH([Date],0)
var _cnt = SUMX(filter(SalesTable, [Invoice No.] =EARLIER([Invoice No.]) && EOMONTH([Date],0) = _mon), sign([Sales]))
return if(_cnt =0,0, SIGN([Sales]))
or
Column = var _mon = EOMONTH([Date],0)
var _cnt = SUMX(filter(SalesTable, [Invoice No.] =EARLIER([Invoice No.]) && EOMONTH([Date],0) = _mon), sign([Sales]))
return if(_cnt =0,0, blank() ))
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
49 | |
26 | |
21 | |
16 | |
12 |
User | Count |
---|---|
57 | |
49 | |
44 | |
19 | |
18 |