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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
onape
Helper I
Helper I

Distinct Count of non-zero records

Hello, I'm hoping I could get some help with something I'm working on. I have a dataset that has values of customers investment per day. some days may be missing from the dataset. My visual has a date slicer (mm-yyyy). I need to do a distinct count of customer ID by Type in my table visual such that only last non-zero value amount on or before the selected date are counted.

onape_2-1737042317827.png

 

The right hand side shows records that will be counted on not based on the filter

onape_3-1737042341464.png

 

so for example, if April 2024 is selected in my filter , the result will be

onape_4-1737042380695.png

 

I have tried a numberof DAX but none is working correctly . Most of them do not count a record if the value date is not in the month of the date filter.

Any help with this will be highly appreciated. 

Thank you

1 ACCEPTED SOLUTION

I think this should do it

Num Customers = 
Var ReferenceDate = MAX('Calendar Table'[Date])
VAR TypesToUse = CALCULATETABLE(
	ALLSELECTED( Sheet1[Type] ),
	REMOVEFILTERS( 'Calendar Table' )
)
VAR Result = SUMX( KEEPFILTERS( TypesToUse ),
    Var BaseTable = 
    ADDCOLUMNS(
        CALCULATETABLE( 
            SUMMARIZE( 
                Sheet1, 
                Sheet1[Account Number],
                Sheet1[Customer ID]
            ), 
            'Calendar Table'[Date] <= ReferenceDate 
        ),
        "@Amount",
        VAR MaxDate = CALCULATE( MAX( Sheet1[Value Date] ), 'Calendar Table'[Date] <= ReferenceDate )
        VAR Amount = CALCULATE( SUM( Sheet1[Value Amount] ), Sheet1[Value Date] = MaxDate, REMOVEFILTERS( 'Calendar Table' ) )
        RETURN Amount
    )

    VAR SummaryTable = FILTER( 
        BaseTable,
        [@Amount] > 0
    )
        
    VAR Result = 
        COUNTROWS(
            GROUPBY( SummaryTable, Sheet1[Customer ID] )
        )
        

    RETURN
        Result
)
RETURN Result

View solution in original post

27 REPLIES 27

Thank you @johnt75 .I think that the code still need a bit of modification please. For example, in January the result should be

onape_0-1737639329902.png

because although there are 3 counts for Blossom by the end of Jan, 2 of them are for the same customer ID (A) so that's 1 count and the one for customer C is the 2nd count. Please see extract below

onape_2-1737639713144.png


For Aug for example, it should look like

onape_1-1737639544816.png

based on

onape_3-1737639868682.png

 

That doesn't match the data. Blossom in January has 3 unique customers

johnt75_0-1737640375777.png

These 3 would obviously also count in August, as we are looking at all values before the specified date.

Apologies if I didn't explain well initially. Using the screenshot you pasted, 

onape_0-1737644162291.png

Considering January, the latest values for account numbers 111, 113 & 130 were non-zero  by the end of the month of january. however, 118 went from £15 to £0 so it is meant to be excluded from the count. Based on that, we'll just the counting A & C.

The below shows the latest values of the accounts by the end of August

 

onape_1-1737644915779.png

For unique count of non-zero values by Aug ending, Blossom = 2 (CustomerIDs A & C), Archi = 1 (CustomerID B), Rowa = 1 (CustomerID A)

I hope it's clearer. 

 

Thanks.



Ah, that needs a different approach

Num Customers = 
Var ReferenceDate = MAX('Calendar Table'[Date])

Var BaseTable = 
ADDCOLUMNS(
	CALCULATETABLE( 
		SUMMARIZE( 
			Sheet1, 
			Sheet1[Account Number],
			Sheet1[Customer ID]
		), 
		'Calendar Table'[Date] <= ReferenceDate 
	),
	"@Amount",
	VAR MaxDate = CALCULATE( MAX( Sheet1[Value Date] ), 'Calendar Table'[Date] <= ReferenceDate )
	VAR Amount = CALCULATE( SUM( Sheet1[Value Amount] ), Sheet1[Value Date] = MaxDate, REMOVEFILTERS( 'Calendar Table' ) )
	RETURN Amount
)

VAR SummaryTable = FILTER( 
	BaseTable,
	[@Amount] > 0
)
	
VAR Result = 
    // COUNTROWS(SummaryTable)
	//COUNTROWS( BaseTable )
	COUNTROWS(
		GROUPBY( SummaryTable, Sheet1[Customer ID] )
	)
	

RETURN
    Result

	

Thank you so much. This is amazing! Is it possible to always have the correct total or I just have to live with that?😊

onape_0-1737650729948.png

 

I think this should do it

Num Customers = 
Var ReferenceDate = MAX('Calendar Table'[Date])
VAR TypesToUse = CALCULATETABLE(
	ALLSELECTED( Sheet1[Type] ),
	REMOVEFILTERS( 'Calendar Table' )
)
VAR Result = SUMX( KEEPFILTERS( TypesToUse ),
    Var BaseTable = 
    ADDCOLUMNS(
        CALCULATETABLE( 
            SUMMARIZE( 
                Sheet1, 
                Sheet1[Account Number],
                Sheet1[Customer ID]
            ), 
            'Calendar Table'[Date] <= ReferenceDate 
        ),
        "@Amount",
        VAR MaxDate = CALCULATE( MAX( Sheet1[Value Date] ), 'Calendar Table'[Date] <= ReferenceDate )
        VAR Amount = CALCULATE( SUM( Sheet1[Value Amount] ), Sheet1[Value Date] = MaxDate, REMOVEFILTERS( 'Calendar Table' ) )
        RETURN Amount
    )

    VAR SummaryTable = FILTER( 
        BaseTable,
        [@Amount] > 0
    )
        
    VAR Result = 
        COUNTROWS(
            GROUPBY( SummaryTable, Sheet1[Customer ID] )
        )
        

    RETURN
        Result
)
RETURN Result

Thank you very much, @johnt75 . This works perfectly.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.