Forum Discussion

YeonseokKim's avatar
YeonseokKim
Frequent Visitor
4 years ago

DISTINCTCOUNT function with many conditions

I want to calcualte DISTINCTCOUNT( 'shipping data'[Loaing #]) with many conditions. 

 

I can get the # of trips of each route easily. But, I want to get the number of backhaul opportunities of the route.

For example, At 7/1, The number of trips of A->B route is 3.

And the Backhaul opportunutes of the route(B->A) is 5. Because  trips of B->A route is total 5 at 7/1.

In addition, Final opportunity number will be just 3 because the opportunity is less than the number of trips of the route at the same day. 

 

7/1 = A-> B route

Actual trip = 3

Backhaul opportunity = 3

 

 

I have numerous daily shipping data and I want to get exact number of backhaul opportunities. 

Please refer to the table below and give me advice how should I use DAX function for it

 

 

Table='shipping data'

DateRouteLoading #Item
2022-07-01A->B46912042PET
2022-07-01A->B46912042Keg
2022-07-01A->B46912043PET
2022-07-01A->B46912044bottle
2022-07-01B->A46912045Keg
2022-07-01B->A46912045PET
2022-07-01B->A46912045bottle
2022-07-01B->A46912046bottle
2022-07-01B->A46912047bottle
2022-07-01B->A46912048bottle
2022-07-01B->A46912049bottle
2022-07-01A->C46912050bottle
2022-07-01A->C46912051bottle
2022-07-02A->B46912052PET
2022-07-02A->B46912053Keg
2022-07-02A->B46912054PET
2022-07-02B->A46912055bottle
2022-07-02A->C46912056Keg
2022-07-02A->B46912057PET
2022-07-02A->B46912058bottle
2022-07-02A->B46912059bottle
2022-07-02B->A46912060bottle
2022-07-02A->C46912061Keg
2022-07-02A->B46912062PET
2022-07-02A->B46912063bottle
2022-07-02A->B46912064bottle
2022-07-02B->A46912065PET
2022-07-02A->C46912066bottle
2022-07-02A->B46912067bottle

 

3 Replies

  • If you create 2 new columns, Source and Destination, by extracting the text before / after delimiter in Power Query, then you could create a measure like

    Backhaul opportunity = 
    var source = SELECTEDVALUE('Table'[Source])
    var dest = SELECTEDVALUE('Table'[Dest])
    var actualTrips = COUNTROWS( 'Table' )
    var returnTrips = CALCULATE( COUNTROWS( 'Table' ),
        REMOVEFILTERS('Table'[Route]),
        TREATAS( { ( source, dest ) },'Table'[Dest],'Table'[Source] )
    )
    var opportunity = MIN( actualTrips, returnTrips )
    return opportunity 
  • YeonseokKim's avatar
    YeonseokKim
    Frequent Visitor

    Thanks for your solution!

    I appreciate it. 

     

    But, I have more questions...

    I want to visualize the opportunity & Actual trips in the same graph.

     

    The graph format will be like below

     

    In addition, I want to show the opportunity & real trips by each route, the graph like below.

    The point to be emphasized here is that all opportinites should be summed up by adding each opportunity per day

    (EX. 7/4

    A->B = 7

    B->A = 4

    Then the opportunity of A->B = 4

     

    7/5

    A->B = 6

    B->A  = 7

    Then the opportunity of A->B = 6

    Finally, Total opportunity of 7/4&7/5 = 4+6 = 10 (Not 11)

     

     

    It would be very grateful if you could give me advice. 

    • johnt75's avatar
      johnt75
      Icon for Super User rankSuper User

      Try the below

      Backhaul opportunity = 
      var source = SELECTEDVALUE('Table'[Source])
      var dest = SELECTEDVALUE('Table'[Dest])
      var actualTrips = SUMX( VALUES('Date'[Date]), CALCULATE(COUNTROWS( 'Table' ))
      var returnTrips = SUMX( VALUES('Date'[Date]), CALCULATE( COUNTROWS( 'Table' ),
          REMOVEFILTERS('Table'[Route]),
          TREATAS( { ( source, dest ) },'Table'[Dest],'Table'[Source] )
      ) )
      var opportunity = MIN( actualTrips, returnTrips )
      return opportunity