Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
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.
The right hand side shows records that will be counted on not based on the filter
so for example, if April 2024 is selected in my filter , the result will be
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
Solved! Go to 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
Hi @onape ,
My approach is to use CROSSJOIN to create an intermediate table that counts whether the Type corresponding to each customer ID has an Amount of 0 at the maximum date, returns 0 if it does, and 1 otherwise, and then uses a matrix and utilizes subtotals on the rows to get the result you want. Here is my test and the pbix file for your reference.
Step1. Custom Table.
Table 3 = CROSSJOIN(VALUES('Table'[Customer ID]),VALUES('Table'[Type]))
Step2. Create measures.
1 =
VAR _selectedMonth = MAX('Table 2'[Date])
VAR _maxdate = CALCULATE(MAX('Table'[Value Date]),ALL('Table'),'Table'[Customer ID]=MAX('Table 3'[Customer ID]),'Table'[Type]=MAX('Table 3'[Type]),'Table'[Value Date]<=_selectedMonth)
VAR _amount = CALCULATE(MAX('Table'[Value Amount]),'Table'[Customer ID]=MAX('Table 3'[Customer ID]),'Table'[Type]=MAX('Table 3'[Type]),'Table'[Value Date]=_maxdate)
VAR _check = IF(_amount>0,1,0)
RETURN
_check
DistCount = SUMX('Table 3',[1])
Best regards,
Mengmeng Li
Thank you, @Anonymous . This worked if the 2 original tables (Dates and the details tables) do not have a relationship. For other purposes of the report, I will need them to have a relationship. Do you think there's something else that can be done?
Hi @onape ,
Yes, this method requires that the date table is not related to the details table, otherwise the slicer will filter the matrix. You can create another date table for this method, and using your original date table (that is related to the details table) elsewhere in your report.
Best regards,
Mengmeng Li
Thanks for your help @Anonymous . This method might not work for me because the users need to see some other metrics on the table visuals which are controlled by the date filter on the page.
I think you can use
Num Customers =
VAR ReferenceDate =
MAX ( 'Date'[Date] )
VAR SummaryTable =
CALCULATETABLE (
INDEX (
1,
'Table',
ORDERBY ( 'Table'[Value Date], DESC ),
PARTITIONBY ( 'Table'[Customer ID] )
),
'Date'[Date] <= ReferenceDate,
'Table'[Value Amount] > 0
)
VAR Result =
COUNTROWS ( SummaryTable )
RETURN
Result
Because the INDEX function will only produce 1 row per customer, per type, you don't need to do a distinct count, COUNTROWS should suffice.
Thank you for the help.
I am currently getting an error though "INDEX's Relation parameter may have duplicate rows. This is not allowed." I'm guessing it's because a customer can have valuations on different or even same account number on the same day. Is there a way to fix this please?
You may be able to get around that by using the MATCHBY function
Num Customers =
VAR ReferenceDate =
MAX ( 'Date'[Date] )
VAR SummaryTable =
CALCULATETABLE (
INDEX (
1,
'Table',
ORDERBY ( 'Table'[Value Date], DESC ),
PARTITIONBY ( 'Table'[Customer ID] ),
MATCHBY ( 'Table'[Customer ID], 'Table'[Value Date] )
),
'Date'[Date] <= ReferenceDate,
'Table'[Value Amount] > 0
)
VAR Result =
COUNTROWS ( SummaryTable )
RETURN
Result
Thank you again.
I'm still getting an error message. Please see below.
Hopefully this will remove the duplicates
Num Customers =
VAR ReferenceDate =
MAX ( 'Date'[Date] )
VAR BaseTable =
CALCULATETABLE (
SUMMARIZE ( 'Table', 'Table'[Customer ID], 'Table'[Value Date] ),
'Date'[Date] <= ReferenceDate,
'Table'[Value Amount] > 0
)
VAR SummaryTable =
INDEX (
1,
BaseTable,
ORDERBY ( 'Table'[Value Date], DESC ),
PARTITIONBY ( 'Table'[Customer ID] ),
MATCHBY ( 'Table'[Customer ID], 'Table'[Value Date] )
)
VAR Result =
COUNTROWS ( SummaryTable )
RETURN
Result
Thank you - This works but if a "Type" has no value at all in the selected month, that type is filtered off the table visual
Can you use Performance Analyzer to get a copy of the query used for the table visual, and post that.
Is your date table marked as a date table ?
Is there a relationship between your date table and Sheet1?
Yes, my calendar table is marked as a date table and there's a relationship between it and Sheet1.
Try unticking "Show items with no data"
Unfortunately, It doesn't make a difference in the results.
Can you post a sample PBIX with any confidential data removed, or a spreadsheet with the raw data? You can share from OneDrive or Google Drive or similar.
Not quite sure of how attachments work here but I've included the link below. Please let me know if that works okay for you. Thanks.
PBIX File
The relationship with the date table was adding a filter which was messing up the INDEX function. We need to add REMOVEFILTERS
Num Customers =
Var ReferenceDate = MAX('Calendar Table'[Date])
Var BaseTable =
CALCULATETABLE(
SUMMARIZE(Sheet1,Sheet1[Customer ID],Sheet1[Value Date]),
'Calendar Table'[Date] <=ReferenceDate,
Sheet1[Value Amount] >0
)
VAR SummaryTable =
CALCULATETABLE(
INDEX(
1,
BaseTable,
ORDERBY(Sheet1[Value Date],DESC),
PARTITIONBY(Sheet1[Customer ID]),
MATCHBY(Sheet1[Customer ID],Sheet1[Value Date])
),
REMOVEFILTERS( 'Calendar Table' )
)
VAR Result =
COUNTROWS(SummaryTable)
RETURN
Result
Check out the July 2025 Power BI update to learn about new features.
User | Count |
---|---|
24 | |
9 | |
7 | |
6 | |
6 |
User | Count |
---|---|
29 | |
11 | |
11 | |
10 | |
6 |