powerbidaxpowerquerypowerbi-desktopdaxstudio

How do I tabulate the last 2 production defect info. based on date of manufacturing per location, per product and per defect?


There are two production units, one in the US and one in Australia. The US plant produces Food and Drinks while the Australian plant produces Food and Stationeries. I have a table that consists of the product ID, date of manufacturing of the products, defects in the products and the cost of defects in USD for each of these defects. The table is as shown below.

Product ID Location Product Date of manufacturing Defect Cost of Defect in USD
USF01 US Food 04/13/2024 Expired 29
USF52 US Food 04/20/2024 Expired 26
USF08 US Food 04/12/2024 Expired 94
USF22 US Food 04/14/2024 Expired 73
USF11 US Food 04/10/2024 Packing error 25
USF08 US Food 04/12/2024 Packing error 82
USF99 US Food 04/19/2024 Packing error 82
USF52 US Food 04/20/2024 Packing error 32
USD07 US Drinks 04/12/2024 Expired 7
USD53 US Drinks 04/14/2024 Expired 81
USD38 US Drinks 04/20/2024 Expired 16
USD76 US Drinks 04/13/2024 Expired 55
USD76 US Drinks 04/13/2024 Packing error 79
USD07 US Drinks 04/12/2024 Packing error 55
USD99 US Drinks 04/15/2024 Packing error 14
USD29 US Drinks 04/17/2024 Packing error 7
AUS51 Australia Stationery 04/19/2024 Expired 33
AUS12 Australia Stationery 04/13/2024 Expired 56
AUS52 Australia Stationery 04/08/2024 Expired 15
AUS72 Australia Stationery 04/20/2024 Expired 15
AUS30 Australia Stationery 04/09/2024 Packing error 61
AUS72 Australia Stationery 04/20/2024 Packing error 78
AUS52 Australia Stationery 04/08/2024 Packing error 22
AUS58 Australia Stationery 04/12/2024 Packing error 84
AUF58 Australia Food 04/19/2024 Expired 44
AUF79 Australia Food 04/18/2024 Expired 10
AUF45 Australia Food 04/08/2024 Expired 31
AUF88 Australia Food 04/12/2024 Expired 71
AUF50 Australia Food 04/08/2024 Packing error 63
AUF88 Australia Food 04/12/2024 Packing error 27
AUF32 Australia Food 04/21/2024 Packing error 69
AUF66 Australia Food 04/19/2024 Packing error 26

I am looking to create a new table which will show the latest 2 rows per location per product and per defect based on the date of manufacturing. The output table should like something like this.

Product ID Location Product Date of manufacturing Defect Cost of Defect in USD
USF52 US Food 04/20/2024 Expired 26
USF22 US Food 04/14/2024 Expired 73
USF52 US Food 04/20/2024 Packing error 32
USF99 US Food 04/19/2024 Packing error 82
USD38 US Drinks 04/20/2024 Expired 16
USD53 US Drinks 04/14/2024 Expired 81
USD29 US Drinks 04/17/2024 Packing error 7
USD99 US Drinks 04/15/2024 Packing error 14
AUF58 Australia Food 04/19/2024 Expired 44
AUF79 Australia Food 04/18/2024 Expired 10
AUF32 Australia Food 04/21/2024 Packing error 69
AUF66 Australia Food 04/19/2024 Packing error 26
AUS72 Australia Stationery 04/20/2024 Expired 15
AUS51 Australia Stationery 04/19/2024 Expired 33
AUS72 Australia Stationery 04/20/2024 Packing error 78
AUS58 Australia Stationery 04/12/2024 Packing error 84

Also, I'm trying to obtain a visual where I can see the consolidated defect costs for the latest 2 rows per location per product and per defect based on the date of manufacturing. The output will be as such.

Location Product Defect Cost of Defect in USD
US Food Expired 99
US Drinks Expired 97
US Food Packing Error 114
US Drinks Packing Error 21
Australia Food Expired 54
Australia Stationery Expired 48
Australia Food Packing Error 95
Australia Stationery Packing Error 162

I tried using the topn formula but I can't seem to get the right answer at all.


Solution

  • I am diving your question in 2 part :

    1st part : create a new table which will show the latest 2 rows per location per product and per defect based on the date of manufacturing

    You need to calculate the rank in your main table using a calculated column :

    Rank = 
    RANKX(
      FILTER(
        ALL('Manufacturers Table'),
        'Manufacturers Table'[Location] = EARLIER('Manufacturers Table'[Location])
        && 'Manufacturers Table'[Product] = EARLIER('Manufacturers Table'[Product])
        && 'Manufacturers Table'[Defect] = EARLIER('Manufacturers Table'[Defect])
      ),
      'Manufacturers Table'[Date of manufacturing],
      ,
      DESC,
      Dense
    )
    

    Then create a calculated table where you mention the rank as condition to filter :

    LatestDefects = 
    CALCULATETABLE(
      'Manufacturers Table',
      FILTER(
        'Manufacturers Table',
        'Manufacturers Table'[Rank] <= 2
      )
    )
    

    enter image description here

    2nd part : Obtain a visual where I can see the consolidated defect costs for the latest 2 rows per location per product and per defect based on the date of manufacturing In the calculated table, create the measure :

    Sum of Top 2 Defect Costs = 
    CALCULATE(
      SUM('YourRankedTableName'[Cost of Defect in USD]),
      'YourRankedTableName'[Rank] <= 2
    )
    

    enter image description here