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

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.

Reply
swwong1
Helper III
Helper III

Monthly Transaction Count with Refund/Reversal

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!

 

 

 

 

 

1 ACCEPTED SOLUTION
v-jayw-msft
Community Support
Community Support

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] ) )
    )
)

vjaywmsft_0-1651732997785.png

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

vjaywmsft_1-1651733451684.png

 

Best Regards,

Jay

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

View solution in original post

2 REPLIES 2
v-jayw-msft
Community Support
Community Support

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] ) )
    )
)

vjaywmsft_0-1651732997785.png

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

vjaywmsft_1-1651733451684.png

 

Best Regards,

Jay

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.
amitchandak
Super User
Super User

@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() ))

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors