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
jbeckham812
Frequent Visitor

Distinct Count with Date Slicer

Hello All,

 

Complete newbie with Power BI/DAX here. Looking for a way to find out how many containers we had on the water at a given time headed to a certain location. I have a table called "Containers" that is layed out as follows:

 

CONTAINER #Origin Departure Actual DateReceivedDestination
YMMU60666565/26/2021 Savannah
YMLU86152215/26/2021 Savannah
TGHU60787396/16/20217/22/2021Perris

 

The count of containers would need to be based on the # of unique entries in the container # field. The amount of containers "on the water" at a given time would then be the # of unique container #'s with a departure date before the date that is chosen by using the date slicer, with a received date that is after the date chosen by using the date slicer. In the case of ongoing shipments, the received date would be blank, so therefore those would be included in the count only if their departure date was before the date chosen by the date slicer.  We would also want these counts separated based on the destination the shipment is going to.

 

So, in the example above, if the date slicer was on 6/20, it would show 2 containers on the water to savannah, and 1 to perris.

 

Please let me know if any more information is needed here, thanks in advance for the help!

1 ACCEPTED SOLUTION

Hi @jbeckham812 

 

To remove the filter which made by relation between the Calendar Table and Containrs table, use the ALLEXCEPT code in the measure.

Please try this measure:

 

The count of containers =
VAR _SelectedDate =
    SELECTEDVALUE ( 'Calendar'[Date], TODAY () ) // this VAR is to find the selected date by Slicer - if nothing selected that will return Today date
RETURN
    VAR _Count =
        CALCULATE (
            DISTINCTCOUNT ( 'Containers'[CONTAINER #] ),
            FILTER (
                ALLEXCEPT ( Containers, Containers[Destination] ),
                'Containers'[Origin Departure Actual Date] <= _SelectedDate
                    && OR (
                        ISBLANK ( 'Containers'[Received] ),
                        'Containers'[Received] > _SelectedDate
                    )
            )
        )
    RETURN
        IF ( ISBLANK ( _Count ), 0, _Count )
// this IF is for showingero when the value is Blank

 

 

Did I answer your question? Mark my post as a solution!

Appreciate your Kudos VahidDM_0-1629245337499.png !!

View solution in original post

4 REPLIES 4
VahidDM
Super User
Super User

Hi @jbeckham812 

 

Please use the below code: 

The count of containers = 
VAR _SelectedDate =
    SELECTEDVALUE ( 'Calendar'[Date], TODAY () )
    // this VAR is to find the selected date by Slicer - if nothing selected that will return Today date
RETURN
    VAR _Count =
        CALCULATE (
            DISTINCTCOUNT ( 'Containers'[CONTAINER #] ),
            FILTER (
                FILTER ( 'Containers', 'Containers'[Origin Departure Actual Date] <= _SelectedDate ),
                OR ( ISBLANK ( 'Containers'[Received] ), 'Containers'[Received] > _SelectedDate )
            )
        )
    RETURN
        IF ( ISBLANK ( _Count ), 0, _Count )
        // this IF is for showingero when the value is Blank

The Output will be as follow:

VahidDM_0-1629158072848.pngVahidDM_1-1629158096869.png

** If you don't select any date on the Slicer, it will return the Today date. For more information please read about SELECTEDVALUE.

I added a calendar table to the model with the date column to use that in the slicer, but you can use any other columns with the date value and change "Calendar'[Date]" in line 3 of the above measure to the new column.

 

Did I answer your question? Mark my post as a solution!

Appreciate your Kudos VahidDM_2-1629158344948.png !!

 

@VahidDM ,

 

Thank you for your help. This measure works perfectly for calculating current # on the water, however it's not supplying the correct # when I choose past dates. I have a calendar table currently, with a relationship to a column in my container table called "Destination Arrival Actual Date".

 

It seems that when using the date slicer with the visual I created using your measure, it outputs the count of containers which have a "Destination Arrival Actual Date" that matches up to the date I've selected in the slicer. I've used USERELATIONSHIP on other measures to work around this on other visuals since my table contains many date fields, however on this one it appears that doing that ends up in the same issue, where the date slicer then causes the visual to give a count of containers that correspond with whichever date field I'm using.

 

Is there something that I'm missing here on this? 

 

 

Hi @jbeckham812 

 

To remove the filter which made by relation between the Calendar Table and Containrs table, use the ALLEXCEPT code in the measure.

Please try this measure:

 

The count of containers =
VAR _SelectedDate =
    SELECTEDVALUE ( 'Calendar'[Date], TODAY () ) // this VAR is to find the selected date by Slicer - if nothing selected that will return Today date
RETURN
    VAR _Count =
        CALCULATE (
            DISTINCTCOUNT ( 'Containers'[CONTAINER #] ),
            FILTER (
                ALLEXCEPT ( Containers, Containers[Destination] ),
                'Containers'[Origin Departure Actual Date] <= _SelectedDate
                    && OR (
                        ISBLANK ( 'Containers'[Received] ),
                        'Containers'[Received] > _SelectedDate
                    )
            )
        )
    RETURN
        IF ( ISBLANK ( _Count ), 0, _Count )
// this IF is for showingero when the value is Blank

 

 

Did I answer your question? Mark my post as a solution!

Appreciate your Kudos VahidDM_0-1629245337499.png !!

@VahidDM Works great, thank you so much for your help!

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.