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
alan7lp
Helper III
Helper III

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.

 

2 ACCEPTED SOLUTIONS
DataInsights
Super User
Super User

Hi @alan7lp,

 

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




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

Proud to be a Super User!




View solution in original post

@alan7lp,

 

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

 

DataInsights_0-1621865955183.png

 





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

Proud to be a Super User!




View solution in original post

6 REPLIES 6
DataInsights
Super User
Super User

Hi @alan7lp,

 

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




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

Proud to be a Super User!




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:

1.png

 

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:

2.png

Is that possible, please?

 

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

 

Thanks in advance,

 

Cheers.

Hi @alan7lp,

 

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.

 

DataInsights_0-1613663029565.png

 

DataInsights_1-1613663055690.png

 





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

Proud to be a Super User!




 

Hi @DataInsights,
1st than anything, thanks for the time invested on helping! Much appreciated.

Above solution is close although I can see an error:
SUPPLIER 13662 ARTICLE 89163 is picking 9.81 price but the dates of that price is in the future. It should be picking 9.23 where TODAY is between those dates range.

 

I am sharing below an extended sample to use as data source:

SUPPLIERITEM_NOFROM_DATETO_DATEMain Price  Total QTY
136628916318/09/202030/03/2021$9.00100
136628916330/03/20211/05/2021$10.00100
16230891631/05/202031/12/2020$9.50100
162308916331/12/20201/02/2021$11.00100
162302740285/08/20192/09/2019$18.50100
162302740282/09/201926/11/2019$19.00100
128792740282/09/201926/11/2019$20.00100
162302740665/08/20192/09/2019$7.50100
162302740662/09/201926/11/2019$8.00100
136622740665/08/20192/09/2021$7.00100
136622740662/09/202131/12/9999$9.00100
162302740855/08/20192/09/2019$11.00100
162302740852/09/201926/11/2021$12.00100
128792740855/08/20192/09/2019$13.00100
128792740852/09/201931/12/9999$14.00100
136622756075/08/20192/09/2019$12.00100
136622756072/09/20196/01/2020$12.50100
136622756076/01/202024/02/2020$13.00100
1287927560724/02/20204/05/2021$13.50100
128792756074/05/202118/05/2022$14.00100
1623027560718/05/202027/07/2020$14.00100
1623027560727/07/202031/12/9999$15.00100

 

The expected result:
If TODAY's date is between [FROM_DATE] and [TO_DATE], [Main Price] * [Total QTY] 

If TODAY's date is NOT between [FROM_DATE] and [TO_DATE], capture the latest available [Main Price] and then [Main Price] * [Total QTY]

 

This should take into consideration than 1 article can be shared by many suppliers. If it's shared, I should get a result per ARTICLE but also per SUPPLIER. See image below for expected outcome:
Result.png

I hope this is clear enough 🙂

Thanks once again mate!

Cheers

@alan7lp,

 

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

 

DataInsights_0-1621865955183.png

 





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

Proud to be a Super User!




Works perfectly!

Thanks mate! 

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.