Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Total cost per validity date

Hi, I am trying to calculate total cost based on a price of specific validation date:

 

ITEM_NOFROM_DATETO_DATEMain Price  Total QTYTotal Cost
8916318/09/202030/03/2021 $     9.2350 $    461.5
8916330/03/202101/05/2021 $     9.81100 $    981.0
8916301/05/202131/12/9999 $     9.66150 $  1,449.0

 

I am after the following result:

 

If TODAY's date is between [FROM_DATE] and [TO_DATE], [Main Price] * [Total QTY]. Here the outcome would be the green row, $461.5 But, as soon we get to 30th of March, I would expect to get the blue row as result, $981 and so on. In this example, I have 3 different dates for the same ITEM_NO but in other cases I have 2; 4; 5 and more date ranges therefore it should be "looping" until it gets a match basically.

 

Also, for some ITEM_NO, [FROM_DATE] and [TO_DATE] are already in the past, if this is the case, I want to capture the latest available [Main Price].

 

I hope it is clear otherwhise please, let me know and I will try to explain it better.

 

Thanks a lot in advance,

Cheers.

 

  • Hi Anonymous,

     

    Try this measure. You can play around with different dates by setting the vToday variable.

     

    Total Cost = 
    VAR vToday =
        TODAY()
        --DATE(2021, 3, 31)
    VAR vCurrentItem =
        MAX ( Items[ITEM_NO] )
    VAR vRowInDateRange =
        FILTER ( Items, vToday >= Items[FROM_DATE] && vToday <= Items[TO_DATE] )
    VAR vResultInDateRange =
        SUMX ( vRowInDateRange, Items[Main Price] * Items[Total QTY] )
    VAR vItemMaxDate =
        CALCULATE ( MAX ( Items[TO_DATE] ) )
    VAR vRowWithMaxDate =
        FILTER ( Items, Items[TO_DATE] = vItemMaxDate )
    VAR vResultWithMaxDate =
        SUMX ( vRowWithMaxDate, Items[Main Price] * Items[Total QTY] )
    VAR vResult =
        IF ( ISBLANK ( vResultInDateRange ), vResultWithMaxDate, vResultInDateRange )
    RETURN
        vResult
  • Anonymous,

     

    Try this measure. There are a few differences compared to your example result, like SUPPLIER 16230 / ITEM_NO 274028 (your example shows FALSE for each row, but shouldn't it use the latest price since today isn't between any FROM_DATE / TO_DATE?).

     

    Total Cost = 
    VAR vToday =
        --TODAY()
        DATE ( 2021, 2, 18 )
    VAR vMainPrice =
        MAX ( Items[Main Price] )
    VAR vTotalQty =
        SUM ( Items[Total QTY] )
    VAR vLatestToDate =
        CALCULATE (
            MAX ( Items[TO_DATE] ),
            ALLEXCEPT ( Items, Items[SUPPLIER], Items[ITEM_NO] )
        )
    VAR vLatestPrice =
        CALCULATE (
            MAX ( Items[Main Price] ),
            ALLEXCEPT ( Items, Items[SUPPLIER], Items[ITEM_NO] ),
            Items[TO_DATE] = vLatestToDate
        )
    VAR vNumRowsTodayInRange =
        CALCULATE (
            COUNTROWS ( Items ),
            ALLEXCEPT ( Items, Items[SUPPLIER], Items[ITEM_NO] ),
            vToday >= Items[FROM_DATE]
                && vToday <= Items[TO_DATE]
        )
    VAR vResult =
        SWITCH (
            TRUE (),
            --if vToday is between FROM_DATE / TO_DATE, use current price
            vToday >= MAX ( Items[FROM_DATE] )
                && vToday <= MAX ( Items[TO_DATE] ), vMainPrice * vTotalQty,
            --if vToday is not between any FROM_DATE / TO_DATE, use latest price
            vNumRowsTodayInRange = 0
                && MAX ( Items[TO_DATE] ) = vLatestToDate, vLatestPrice * vTotalQty
        )
    RETURN
        vResult

     

     

6 Replies

  • Hi Anonymous,

     

    Try this measure. You can play around with different dates by setting the vToday variable.

     

    Total Cost = 
    VAR vToday =
        TODAY()
        --DATE(2021, 3, 31)
    VAR vCurrentItem =
        MAX ( Items[ITEM_NO] )
    VAR vRowInDateRange =
        FILTER ( Items, vToday >= Items[FROM_DATE] && vToday <= Items[TO_DATE] )
    VAR vResultInDateRange =
        SUMX ( vRowInDateRange, Items[Main Price] * Items[Total QTY] )
    VAR vItemMaxDate =
        CALCULATE ( MAX ( Items[TO_DATE] ) )
    VAR vRowWithMaxDate =
        FILTER ( Items, Items[TO_DATE] = vItemMaxDate )
    VAR vResultWithMaxDate =
        SUMX ( vRowWithMaxDate, Items[Main Price] * Items[Total QTY] )
    VAR vResult =
        IF ( ISBLANK ( vResultInDateRange ), vResultWithMaxDate, vResultInDateRange )
    RETURN
        vResult
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi mate,

       

      Works wonders, amazing! Thanks for your help!

       

      EDIT:

      Hi DataInsights ,

       

      I have now discovered I have one more step to go and here my apologies as I didn't mention it before.

       

      Currently the measure is taking into consideration ITEM_NO however I have another colum of SUPPLIER. In this case the same ITEM_NO can belong to 1 or more suppliers and have it's own price, see table below:

      Current:

       

      With your solution, I get the proper result but only if the ITEM_NO belongs to only one SUPPLIER. In the example above a correct answer should show the price based on the current logic of your solution but also considering SUPPLIER number 

       

      Wished result:

      Is that possible, please?

       

      If not clear explanation let me know as well so I try to make it clearer.

       

      Thanks in advance,

       

      Cheers.

      • DataInsights's avatar
        DataInsights
        Super User

        Hi Anonymous,

         

        Here's the sample data I created, along with the result. Is this correct? If not, would you provide more accurate sample data (need multiple SUPPLIER and ITEM_NO), along with the expected result.