Forum Discussion

Donny620's avatar
Donny620
Icon for Helper I rankHelper I
3 years ago
Solved

Difference in date between two columns in a measure?

Hello!

 

I have an Excel version of this that I am trying to duplicate in PBI.  I have three date columns Date 1a(stock date), Date1b(different stock date), and Date 2(sell date).  I want to use Date 1a stock date over Date 1b stock date, so only use Date 1b if Date 1a is blank.  The goal is to subtract them to see a days in inventory and then one more column which just says that if Aging is >540 then make it 540 and less than 5 make it 5.  

 

For some reason the PBI I'm using won't let me create calculated columns (there are many tables which are connected to some sort of data lake, so I think I might not have access , e.g. right clicking on a table says 'new measure', 'select columns', etc but option to create column).  I can create measures though. So my question is: is there a way to create measures which outputs an aging in the way as it is given below?  Thanks! 

 

 

Date 1a (given)Date 1b (given)Date 2 (given)Date 1 Column (if 1a is blank do 1b, otherwise always look at 1a)Aging (Date 1 Column minus Date 2)Trimmed Aging 
6/21/20235/1/20234/6/20236/21/2023-655 
Null11/30/20146/13/201911/30/20141656540 
  • Donny620 you can add a measure like this:

     

     

    Trimmed Aging = 
    VAR __Date1 = COALESCE ( SELECTEDVALUE ( Table[Date1] ), Table[Date2] )
    VAR __Date2 = SELECTEDVALUE ( Table[Date2] )
    VAR __Diff = DATEDIFF ( __Date1, __Date2, DAYS )
    RETURN
    IF ( __Diff < 5, 5, IF ( __Diff > 540, 540, __Diff ) )

     

     

     

17 Replies

  • Donny620 you can add a measure like this:

     

     

    Trimmed Aging = 
    VAR __Date1 = COALESCE ( SELECTEDVALUE ( Table[Date1] ), Table[Date2] )
    VAR __Date2 = SELECTEDVALUE ( Table[Date2] )
    VAR __Diff = DATEDIFF ( __Date1, __Date2, DAYS )
    RETURN
    IF ( __Diff < 5, 5, IF ( __Diff > 540, 540, __Diff ) )

     

     

     

    • Donny620's avatar
      Donny620
      Icon for Helper I rankHelper I

      Hi thank you!  I'm having trouble recreating that formula, when I get to 

      SELECTEDVALUE ( Table[Date1] ), Table[Date2] )

      It won't let me select the second date2 column.  I mean I can put in Date1 fine but when I try to put in Date2 it will only let me put calculations (measures?) and won't let me insert any real column.  

      Is this a limation of the SelectedValue function?  Any ideas, does this make sense?

  • Donny620 check my formula, it is not DATE1, DATE2, there is a parenthesis in between.

    • Donny620's avatar
      Donny620
      Icon for Helper I rankHelper I

      Hi parry2k thank you so much and I'm so sorry but since my real data set is based on a SQL database it won't let me reference the columns in the way you did.  I can get the formula to not give me an error if I tweak it as below but the result is not right (e.g. has values much greater than 540, etc.) My columns are stored in 'Fact Inventory'. I had to add the selectedvalue for it to let me select any column in that table (otherwise it only lets me select other measures), even though it may not be correct to do that.

       

      Trimmed Aging =

          SUMX (
              'Fact Inventory',
              VAR __Date1 = COALESCE (selectedvalue('Fact Inventory'[Date 1a]), selectedvalue('Fact Inventory'[Date 1b] ))
              VAR __Date2 =  selectedvalue('Fact Inventory'[Date 2 (date it leaves inventory)])
              VAR __Diff = DATEDIFF ( __Date1, __Date2, DAY )
              RETURN
                  IF ( __Diff < 5, 5, IF ( __Diff > 540, 540, __Diff ) )
          )
       
      I'm hoping it's possible to just tweak this? 🙂 Please let me know thank you!
  • Donny620 did you look at pbix file with the solution, I used a different approach and you should follow the same. 

    • Donny620's avatar
      Donny620
      Icon for Helper I rankHelper I

      Hi I did thank you, that's where I got the formula (I tweaked it) for the measure I copied below.  In your file I see a measure called "Trimmed Aging" (below) and I see a calculated column called "days in invnetory trimmed - fixed".  Because my data is just a bunch of SQL tables, or perhaps because of access issues, I cannot create any columns, only measures.  That's why I was hoping for a measure-only solution.  😞 Do you think this is possible?  Thanks again!

  • Donny620 the measure I gave you is this one, don't know which one you are using:

     

    Trimmed Aging = 
    
        SUMX ( 
            'Data', 
            VAR __Date1 = COALESCE ( Data[Date 1a], Data[Date 1b] )
            VAR __Date2 = Data[Date 2 (date it leaves inventory)] 
            VAR __Diff = DATEDIFF ( __Date1, __Date2, DAY )
            RETURN 
                IF ( __Diff < 5, 5, IF ( __Diff > 540, 540, __Diff ) )
        )
    
    • Donny620's avatar
      Donny620
      Icon for Helper I rankHelper I

      parry2k Agreed, what I mean is that the file I sent was based on sample data, but on my real data I am unable to use this measure as is because I can't reference columns in that measure.  If I try to type in Data[Date 1a], it won't come up, it will only show me other measures/calculations.  The only way it would let me reference it was with a function like selectedvalue.  Does that make sense? 😞 Thanks!

  • Can you share what you expression are you using based on what I gave you. You are trying me to guess things here and making things over complicated.

    • Donny620's avatar
      Donny620
      Icon for Helper I rankHelper I

      So sorry!  See below, where 'Fact Inventory' is the name of the SQL database table.  I only added selectedvalue function because otherwise it won't let me insert/find columns like 'Date 1a' (it only 'finds' calculations/measures).  Does this help?

       

      Trimmed Aging =

       

          SUMX (
              'Fact Inventory',
              VAR __Date1 = COALESCE (selectedvalue('Fact Inventory'[Date 1a]), selectedvalue('Fact Inventory'[Date 1b] ))
              VAR __Date2 =  selectedvalue('Fact Inventory'[Date 2 (date it leaves inventory)])
              VAR __Diff = DATEDIFF ( __Date1, __Date2, DAY )
              RETURN
                  IF ( __Diff < 5, 5, IF ( __Diff > 540, 540, __Diff ) )
          )
  • Donny620 it doesn't make sense at all, what is your data source? Are you using DQ/Import or Live connection?