Forum Discussion

KristofferAJ's avatar
KristofferAJ
Helper III
4 years ago
Solved

Using latest date based on two specific coloumns in data

Hi!

I have a question for a formula I would like to create. In theory it's only two dates which needs to be subtracted in order to provide time between 'Dispatch' and 'last sold date'

 

1. Can I do this with a calculation? or do I need to set up a table similar to a pivot table/matrix table I can then relate to my other data?

 

Formula wish: I want to know the time between 'Dispatch date' and 'last sold date' per Account ID, but I want to use the latest date per fruit and country for each calculation.

 

For instance:

For orange, account Konrad:

Use 'Date dispatch': 7/18/2013, however use last date for orange in his country Austria: 10/9/2013

 

I know this can be done in tableau by FIX to a specific column like country, but is there a way I can do this in DAX without needing to setup additional related tables?

 

Would appreciate input and help to this question. I have attached a test data set below

Thanks

 

FruitAccount IDCountryDispathFirst sold dateLast sold date
PearklaraAustria10/23/201311/11/20132/10/2014
OrangeKonradAustria7/18/20137/24/20138/5/2013
AppleMikeAustria4/17/20134/25/20134/29/2013
OrangePiaAustria8/7/20139/5/201310/9/2013
PearEvaFrance2/8/20143/17/20143/17/2014
OrangeJohnFrance8/28/20139/20/20139/20/2013
AppleShannonFrance5/29/20137/11/2013 
PearSimonFrance8/20/20139/18/201312/11/2013
PearChrisGermany1/29/20143/3/20143/3/2014
AppleJimGermany5/3/20137/8/20137/8/2013
PearLizGermany7/2/20137/29/201310/29/2013
OrangePaulGermany8/13/20138/23/20138/23/2013
AppleBeernardSpain5/29/20136/4/20137/24/2013
PearKarinSpain 8/21/201312/20/2013
OrangeSusanSpain7/16/20137/22/20139/24/2013
      
+ 20 other+ 1000 other+ 20 other 


2. If this is not possible, how can I best set up a pivot like table in powerBI which I can then connect and use for this connection? I would like to have this table as part of the data model and based on my original table.

 

Like below

FruitCountryLast sold date (max date)



  • mahoneypat's avatar
    mahoneypat
    4 years ago

    Here you go.

     

    NewMeasure =
    VAR dispdate =
        MIN( T4[Dispath] )
    VAR lastsoldthiscountry =
        CALCULATE(
            MAX( T4[Last sold date] ),
            ALL( t4 ),
            SUMMARIZE( t4, T4[Fruit], T4[Country] )
        )
    RETURN
        IF(
            NOT ( ISBLANKlastsoldthiscountry ) && NOT ( ISBLANKdispdate ),
            INTlastsoldthiscountry - dispdate )
        )

     

    Pat

4 Replies

  • mahoneypat's avatar
    mahoneypat
    Microsoft Employee

    Please try this measure expression to get the results shown.

     

    NewMeasure =
    VAR dispdate =
        MIN( T4[Dispath] )
    VAR lastsoldthiscountry =
        CALCULATE(
            MAX( T4[Last sold date] ),
            ALL( t4 ),
            SUMMARIZE( t4, T4[Fruit], T4[Country] )
        )
    RETURN
        IF(
            NOT ( ISBLANKlastsoldthiscountry ),
            INTlastsoldthiscountry - dispdate )
        )

     

     

    Pat

    • KristofferAJ's avatar
      KristofferAJ
      Helper III

      Hi mahoneypat ,

       

      This is exactly what I was looking for, and testing the formula in my massive dataset it runs very fast and smooth! 100000 thanks for that!

       

      Last question: When no Dispatch date is available (e.g. above Pear Karin), can I have the formula to return blank since the date is missing?

       

      Additionally in some cases I will have negative values returned, can I have the formula to blank those also?

       

      Alternatively I could solve that either manually or by adding an additional step

       

      Kristoffer

      • mahoneypat's avatar
        mahoneypat
        Microsoft Employee

        Here you go.

         

        NewMeasure =
        VAR dispdate =
            MIN( T4[Dispath] )
        VAR lastsoldthiscountry =
            CALCULATE(
                MAX( T4[Last sold date] ),
                ALL( t4 ),
                SUMMARIZE( t4, T4[Fruit], T4[Country] )
            )
        RETURN
            IF(
                NOT ( ISBLANKlastsoldthiscountry ) && NOT ( ISBLANKdispdate ),
                INTlastsoldthiscountry - dispdate )
            )

         

        Pat