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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
komaldhabhai
Frequent Visitor

Dax to calculate distinctcount of IDs for below scenario

Hi,
I need help on the below mentioned scenario:

I have a 'Period' filter having values (YTD, Current Month)

I have a 'Date' filter having only end date selection (Before type date filter)

How these two filter work:
When I select YTD and select an end date say 1 Nov 2024 from date filter then my date range for calculations would be 1 Jan 2024 to 1 Nov 2024
When I select Current Month and select date say 1 Nov 2024 from date filter then my date range for calculations would be for one whole month of Nov 2024.

I want to calculate the distinct count of ContactIDs that fall in the period selected , having both Channeltype .i.e Digital and In Person
After calculating this I want to show distribution of these IDs among Channels.

Example:

Period selected : YTD
Date selected : 1 Dec 2024
(Now the date range for calculations would be from 1 Jan 2024 to 1 Dec 2024)

According to data,
ContactIDs A1234 , B0171 and C9856 have both Channeltype(.i.e Digital and In Person) in the period selected

My distinct count of IDs is 3

Now I want to see how these 3 IDs are distributed among Channel

Marketing - 2 (ContactIDs A1234 ,C9856 falls under Marketing so count would be 2)
Accounting - 1 (ContactID B0171 falls under Accounting so count would be 1)
Finance - 3 (ContactIDs A1234 , B0171 and C9856 falls under Finance so count would be 3)
Sales - 0 (No ID which has both Channeltype)
Admin - 1 (ContactID C9856 falls under Accounting so count would be 1)

 

ContactIDDateChannelTypeChannelClientSegment
A123410/1/2024DigitalMarketingHT
B01715/1/2024In PersonAccountingRY
C985611/1/2024In PersonAdminZY
C98565/1/2024DigitalFinanceZY
B01711/1/2024DigitalFinanceRY
D04767/1/2024DigitalFinanceHT
A123410/1/2024In PersonFinanceHT
E053712/1/2024DigitalSalesRY
C985610/1/2024DigitalMarketingZY

 

1 ACCEPTED SOLUTION
Bibiano_Geraldo
Super User
Super User

Hi @komaldhabhai ,

To achieve your goal, first create a new disconected calendar table by this DAX:

Date = 
CALENDAR(
    MIN('Table'[Date]),
    MAX('Table'[Date])
)

Make sure that you have the period table, if not, please consider to create a new disconnected table for period by this DAX:

Period = {"YTD", "Current Month"}

And rename the column for Period also:

Bibiano_Geraldo_0-1739450330746.png

Now create DAX measure to count Distinct Contacts with MultipleChannels:

Distinct_Contacts_Multiple_Channels = 
VAR selectedDate = LASTDATE('Date'[Date]) 
VAR PeriodSelection = SELECTEDVALUE('Period'[Period])

VAR startDate = 
    IF(
        PeriodSelection = "YTD", 
        DATE(YEAR(selectedDate), 1, 1), 
        DATE(YEAR(selectedDate), MONTH(selectedDate), 1)
    )

VAR endDate = 
    IF(
        PeriodSelection = "YTD", 
        selectedDate, 
        EOMONTH(selectedDate, 0)
    )


VAR Contact_Channel_Count =
    ADDCOLUMNS(
        ALL('Table'[ContactID]),
        "ChannelCount", 
        CALCULATE(
            DISTINCTCOUNT('Table'[ChannelType]),
            DATESBETWEEN('Table'[Date], startDate, endDate)
        )
    )

RETURN
CALCULATE(
    DISTINCTCOUNT('Table'[ContactID]),
    FILTER(Contact_Channel_Count, [ChannelCount] > 1),
    DATESBETWEEN('Table'[Date], startDate, endDate)
)

 

And other measure for channel distribuitions:

MultiChannel_Distribution = 
VAR SelectedDate = LASTDATE('Date'[Date])
VAR PeriodSelection = SELECTEDVALUE('Period'[Period])
VAR StartDate =
    IF(
        PeriodSelection = "YTD",
        DATE(YEAR(SelectedDate), 1, 1),
        DATE(YEAR(SelectedDate), MONTH(SelectedDate), 1)
    )
VAR EndDate =
    IF(
        PeriodSelection = "YTD",
        SelectedDate,
        EOMONTH(SelectedDate, 0)
    )
    
// Get all ContactIDs in the date range ignoring channel filters
VAR AllContacts =
    CALCULATETABLE(
        VALUES('Table'[ContactID]),
        REMOVEFILTERS('Table'[ChannelType], 'Table'[Channel]),
        DATESBETWEEN('Table'[Date], StartDate, EndDate)
    )

// From all contacts, filter those having more than one distinct ChannelType (i.e. both Digital and In Person)
VAR ValidContacts =
    FILTER(
        AllContacts,
        CALCULATE(
            DISTINCTCOUNT('Table'[ChannelType]),
            REMOVEFILTERS('Table'[ChannelType], 'Table'[Channel]),
            DATESBETWEEN('Table'[Date], StartDate, EndDate)
        ) > 1
    )

// Now count, in the current filter context (which will include a specific Channel if used in a visual),
// the distinct ContactIDs that are in the ValidContacts list.
RETURN
CALCULATE(
    DISTINCTCOUNT('Table'[ContactID]),
    TREATAS(ValidContacts, 'Table'[ContactID]),
    DATESBETWEEN('Table'[Date], StartDate, EndDate)
)

 

Now add to slicers to your report, one with period column and other with date column from date table:

Bibiano_Geraldo_1-1739450520265.png

Now add a new card and place Distinct_Contacts_Multiple_Channels measure in the field:

Bibiano_Geraldo_2-1739450580832.png

 

Now add a table visual and add the channels columns and the MultiChannel_Distribution measure:

Bibiano_Geraldo_3-1739450689563.png

this will be the final rsult if you select 01 dec 2024:

Bibiano_Geraldo_4-1739450738275.png

 

 

 

 

View solution in original post

3 REPLIES 3
Bibiano_Geraldo
Super User
Super User

Hi @komaldhabhai ,

To achieve your goal, first create a new disconected calendar table by this DAX:

Date = 
CALENDAR(
    MIN('Table'[Date]),
    MAX('Table'[Date])
)

Make sure that you have the period table, if not, please consider to create a new disconnected table for period by this DAX:

Period = {"YTD", "Current Month"}

And rename the column for Period also:

Bibiano_Geraldo_0-1739450330746.png

Now create DAX measure to count Distinct Contacts with MultipleChannels:

Distinct_Contacts_Multiple_Channels = 
VAR selectedDate = LASTDATE('Date'[Date]) 
VAR PeriodSelection = SELECTEDVALUE('Period'[Period])

VAR startDate = 
    IF(
        PeriodSelection = "YTD", 
        DATE(YEAR(selectedDate), 1, 1), 
        DATE(YEAR(selectedDate), MONTH(selectedDate), 1)
    )

VAR endDate = 
    IF(
        PeriodSelection = "YTD", 
        selectedDate, 
        EOMONTH(selectedDate, 0)
    )


VAR Contact_Channel_Count =
    ADDCOLUMNS(
        ALL('Table'[ContactID]),
        "ChannelCount", 
        CALCULATE(
            DISTINCTCOUNT('Table'[ChannelType]),
            DATESBETWEEN('Table'[Date], startDate, endDate)
        )
    )

RETURN
CALCULATE(
    DISTINCTCOUNT('Table'[ContactID]),
    FILTER(Contact_Channel_Count, [ChannelCount] > 1),
    DATESBETWEEN('Table'[Date], startDate, endDate)
)

 

And other measure for channel distribuitions:

MultiChannel_Distribution = 
VAR SelectedDate = LASTDATE('Date'[Date])
VAR PeriodSelection = SELECTEDVALUE('Period'[Period])
VAR StartDate =
    IF(
        PeriodSelection = "YTD",
        DATE(YEAR(SelectedDate), 1, 1),
        DATE(YEAR(SelectedDate), MONTH(SelectedDate), 1)
    )
VAR EndDate =
    IF(
        PeriodSelection = "YTD",
        SelectedDate,
        EOMONTH(SelectedDate, 0)
    )
    
// Get all ContactIDs in the date range ignoring channel filters
VAR AllContacts =
    CALCULATETABLE(
        VALUES('Table'[ContactID]),
        REMOVEFILTERS('Table'[ChannelType], 'Table'[Channel]),
        DATESBETWEEN('Table'[Date], StartDate, EndDate)
    )

// From all contacts, filter those having more than one distinct ChannelType (i.e. both Digital and In Person)
VAR ValidContacts =
    FILTER(
        AllContacts,
        CALCULATE(
            DISTINCTCOUNT('Table'[ChannelType]),
            REMOVEFILTERS('Table'[ChannelType], 'Table'[Channel]),
            DATESBETWEEN('Table'[Date], StartDate, EndDate)
        ) > 1
    )

// Now count, in the current filter context (which will include a specific Channel if used in a visual),
// the distinct ContactIDs that are in the ValidContacts list.
RETURN
CALCULATE(
    DISTINCTCOUNT('Table'[ContactID]),
    TREATAS(ValidContacts, 'Table'[ContactID]),
    DATESBETWEEN('Table'[Date], StartDate, EndDate)
)

 

Now add to slicers to your report, one with period column and other with date column from date table:

Bibiano_Geraldo_1-1739450520265.png

Now add a new card and place Distinct_Contacts_Multiple_Channels measure in the field:

Bibiano_Geraldo_2-1739450580832.png

 

Now add a table visual and add the channels columns and the MultiChannel_Distribution measure:

Bibiano_Geraldo_3-1739450689563.png

this will be the final rsult if you select 01 dec 2024:

Bibiano_Geraldo_4-1739450738275.png

 

 

 

 

Hey @Bibiano_Geraldo Thanks! It worked.

FarhanJeelani
Super User
Super User

Dear @komaldhabhai ,

 

Please try below dax:

-- DAX Measure to calculate the distinct count of ContactIDs having both Digital and In Person channel types
DistinctContactIDsBothChannels =
VAR SelectedEndDate = MAX('Date'[Date])
VAR SelectedPeriod = SELECTEDVALUE('Period'[Period])
VAR StartDate =
    IF(
        SelectedPeriod = "YTD", DATE(YEAR(SelectedEndDate), 1, 1),
        DATE(YEAR(SelectedEndDate), MONTH(SelectedEndDate), 1)
    )
VAR FilteredData =
    FILTER(
        'ContactData',
        'ContactData'[Date] >= StartDate && 'ContactData'[Date] <= SelectedEndDate
    )
VAR ContactsWithBothChannels =
    FILTER(
        ADDCOLUMNS(
            SUMMARIZE(FilteredData, 'ContactData'[ContactID]),
            "ChannelCount",
            CALCULATE(
                DISTINCTCOUNT('ContactData'[ChannelType]),
                FilteredData,
                'ContactData'[ContactID] = EARLIER('ContactData'[ContactID])
            )
        ),
        [ChannelCount] = 2
    )
RETURN
    CALCULATE(
        DISTINCTCOUNT('ContactData'[ContactID]),
        ContactsWithBothChannels
    )

-- Measure to show distribution of these ContactIDs among Channels
ChannelDistribution =
VAR SelectedEndDate = MAX('Date'[Date])
VAR SelectedPeriod = SELECTEDVALUE('Period'[Period])
VAR StartDate =
    IF(
        SelectedPeriod = "YTD", DATE(YEAR(SelectedEndDate), 1, 1),
        DATE(YEAR(SelectedEndDate), MONTH(SelectedEndDate), 1)
    )
VAR FilteredData =
    FILTER(
        'ContactData',
        'ContactData'[Date] >= StartDate && 'ContactData'[Date] <= SelectedEndDate
    )
VAR ContactsWithBothChannels =
    FILTER(
        ADDCOLUMNS(
            SUMMARIZE(FilteredData, 'ContactData'[ContactID]),
            "ChannelCount",
            CALCULATE(
                DISTINCTCOUNT('ContactData'[ChannelType]),
                FilteredData,
                'ContactData'[ContactID] = EARLIER('ContactData'[ContactID])
            )
        ),
        [ChannelCount] = 2
    )
RETURN
    CALCULATE(
        DISTINCTCOUNT('ContactData'[ContactID]),
        FilteredData,
        ContactsWithBothChannels
    )

 

Please mark this as solution if it helps you. Appreciate Kudos.

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors