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
diegoamferreira
Frequent Visitor

Track Cost Changes

I'm trying to track the cost of products using a table that shows me only the price update.

 

I have the fact table that shows me the sales of products and the cost table with the changes in cost per product...

So each time a price changes, a new record will go into the table with corresponding SKU, and date of the change.

 

I need a measure to bring the cost on the day of sale.

 

I tried using that expression, but I had a lot of performance problems as you can see

 

Product Cost = CALCULATE (
                    SUM(d_Cost[cost]),
                    FILTER (d_Cost,
                        d_Cost[DATE]
                            = CALCULATE (
                                LASTNONBLANK ( d_Cost[DATE], 1 ),
                                FILTER (
                                    d_Cost,
                                    d_Cost[DATE] <= LASTDATE(f_Sales[Date])))))

 

 

 
 

Capturar.PNG

 

Table fSales

Spoiler

DateTotalSKU
18/05/2020R$ 236,991224
08/04/2020R$ 206,111787
08/05/2020R$ 197,91787
20/05/2020R$ 225,81787
19/05/2020R$ 394,992061
21/05/2020R$ 394,992061
20/04/2020R$ 71,762768
04/05/2020R$ 379,92786
15/05/2020R$ 324,992786
18/05/2020R$ 349,992786
18/05/2020R$ 324,992786
18/05/2020R$ 349,992786
19/05/2020R$ 324,992786
19/05/2020R$ 324,992786
20/05/2020R$ 324,992786
22/05/2020R$ 332,892786
07/04/2020R$ 450,152792
02/05/2020R$ 440,252792
14/05/2020R$ 440,252792
19/05/2020R$ 132,892986
21/05/2020R$ 120,792986
22/05/2020R$ 121,992986
24/05/2020R$ 128,892986
17/04/2020R$ 157,853009
07/05/2020R$ 150,953009
16/05/2020R$ 150,953009
16/05/2020R$ 326,793124
16/05/2020R$ 114,793264
17/05/2020R$ 116,83264
18/05/2020R$ 120,793264
18/05/2020R$ 127,793264
19/05/2020R$ 114,793264
20/05/2020R$ 126,793264
21/05/2020R$ 116,793264
21/05/2020R$ 104,83264
23/05/2020R$ 104,83264
22/04/2020R$ 579,43286
16/03/2020R$ 280,143426
13/04/2020R$ 280,153426
07/05/2020R$ 269,993426
07/05/2020R$ 269,993426

 

 

Table dCost

Spoiler

 

SKUPROVIDERDATEDESCRIPTION COST 
6407BRAND 301/01/2020PRODUCT 1101,05
6465BRAND 201/01/2020PRODUCT 249,96
3426BRAND 101/01/2020PRODUCT 386,4
2786BRAND 301/01/2020PRODUCT 4171,5
5757BRAND 301/01/2020PRODUCT 537,2
6085BRAND 101/01/2020PRODUCT 688,65
1787BRAND 101/01/2020PRODUCT 798,95
6501BRAND 101/01/2020PRODUCT 877,81
2986BRAND 301/01/2020PRODUCT 943,92
3858BRAND 301/01/2020PRODUCT 1048
6195BRAND 201/01/2020PRODUCT 1122,54
5991BRAND 101/01/2020PRODUCT 1234,8
5619BRAND 301/01/2020PRODUCT 13173,86
2061BRAND 101/01/2020PRODUCT 14118,5
3264BRAND 301/01/2020PRODUCT 1544,06
6431BRAND 201/01/2020PRODUCT 16429,84
5469BRAND 301/01/2020PRODUCT 17128,72
6255BRAND 101/01/2020PRODUCT 1819,07
5175BRAND 301/01/2020PRODUCT 19123,8
6099BRAND 101/01/2020PRODUCT 20219,49
3234BRAND 301/01/2020PRODUCT 21224,36
5435BRAND 201/01/2020PRODUCT 22149,8
5731BRAND 201/01/2020PRODUCT 23398,7
5363BRAND 201/01/2020PRODUCT 2473,06
5707BRAND 201/01/2020PRODUCT 2543,2
4443BRAND 101/01/2020PRODUCT 2652,8
3286BRAND 301/01/2020PRODUCT 27266,53
6017BRAND 101/01/2020PRODUCT 28117,66
3132BRAND 101/01/2020PRODUCT 29146
2792BRAND 101/01/2020PRODUCT 30105,66
6065BRAND 201/01/2020PRODUCT 31101,75
6261BRAND 201/01/2020PRODUCT 3236,29
3009BRAND 201/01/2020PRODUCT 3370,95
3582BRAND 301/01/2020PRODUCT 3411,07
1224BRAND 201/01/2020PRODUCT 35118,5
6489BRAND 201/01/2020PRODUCT 36216,72
4487BRAND 201/01/2020PRODUCT 3758,46
5981BRAND 101/01/2020PRODUCT 3895,97
6131BRAND 101/01/2020PRODUCT 3910,66
6521BRAND 301/01/2020PRODUCT 40144,41
6001BRAND 201/01/2020PRODUCT 41186,17
3262BRAND 201/01/2020PRODUCT 4224,71
5729BRAND 301/01/2020PRODUCT 43321,86
6185BRAND 301/01/2020PRODUCT 4424,18
2768BRAND 301/01/2020PRODUCT 4517,94
6483BRAND 201/01/2020PRODUCT 4640,89
3124BRAND 101/01/2020PRODUCT 47118,36
3568BRAND 301/01/2020PRODUCT 4813,62
6473BRAND 101/01/2020PRODUCT 4998,67
6067BRAND 301/01/2020PRODUCT 50131,06
4323BRAND 101/01/2020PRODUCT 5236,99
6457BRAND 101/01/2020PRODUCT 5358,38
3380BRAND 201/01/2020PRODUCT 548,12
3426BRAND 120/04/2020PRODUCT 3100
3426BRAND 120/05/2020PRODUCT 390
6195BRAND 217/02/2020PRODUCT 1125
6195BRAND 221/02/2020PRODUCT 117

 the 'current' price is the 'last price' recorded

 

Any help would be greatly appreciated.

 

Many thanks.

1 ACCEPTED SOLUTION
v-alq-msft
Community Support
Community Support

Hi, @diegoamferreira 

 

You may try the following measure.

ResultCost = 
var _maxdate = 
CALCULATE(
    MAX(dCost[DATE]),
    FILTER(
        ALL(dCost),
        dCost[SKU] = SELECTEDVALUE(fSales[SKU])
    )
)
return
MAXX(
    FILTER(
        ALL(dCost),
        dCost[SKU] = SELECTEDVALUE(fSales[SKU])&&
        dCost[DATE] = _maxdate
    ),
    dCost[ COST ]
)

 

Result:

g1.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-alq-msft
Community Support
Community Support

Hi, @diegoamferreira 

 

If you take the answer of someone, please mark it as the solution to help the other members who have same problems find it more quickly. If not, let me know and I'll try to help you further. Thanks.

 

Best Regards

Allan

v-alq-msft
Community Support
Community Support

Hi, @diegoamferreira 

 

You may try the following measure.

ResultCost = 
var _maxdate = 
CALCULATE(
    MAX(dCost[DATE]),
    FILTER(
        ALL(dCost),
        dCost[SKU] = SELECTEDVALUE(fSales[SKU])
    )
)
return
MAXX(
    FILTER(
        ALL(dCost),
        dCost[SKU] = SELECTEDVALUE(fSales[SKU])&&
        dCost[DATE] = _maxdate
    ),
    dCost[ COST ]
)

 

Result:

g1.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you!

AlB
Super User
Super User

Hi @diegoamferreira 

Hi, can you try to explain based on  an example

1.what the measure does,

2. what the expected result is for that example and

3. the steps in your measure code?

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

 

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.