0
$\begingroup$

Example data as image
Desired output as image

I have data for planned and actual produced quantities for a number of items (articles) and batches. Please see example data in image and attached table. I wish to calculate:

Note: For a specific item type, the planned quantity may change between batches (depending on when in time the batch was produced, data not included in example), i.e. for the last 3 rows for the item Orange. Different planned quantities within a single item type is to be considered different items.

  1. Count number of occurrences/records per item type and planned quantity. Exclude items with <5 occurrences.
  2. Overall average yield per article and planned quantity.
  3. Max yield per article and planned quantity.
  4. Average yield for the bottom 80% of batches as ranked by yield.
  5. Average yield for the top 20% of batches as ranked by yield.

Number 1, 2 and 3 I can do myself in Power Query. Number 1 is done in a separate query "Count&Filter_Occurances" which is then merged with the main query.
Number 3 and 4 requires steps that I do not know how to produce, but I would assume what is required is something along the lines of:

  1. Sort/rank all records within an article type and planned quantity based on their yield.
  2. Group top 20% and bottom 80% of records per each article and planned quantity.
  3. Calculate average yield in both groups.

Not sure if doing this in a separate query and merging, or inserting the grouping code directly into the main query is the best approach.

The number of records per item type and quantity can vary greatly, hence I need the top and bottom percentage of records instead of just the top and bottom number of records.

Any help with this would be very highly appreciated! Please see my current M code below.

Main query

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Batch No", Int64.Type}, {"Item Name", type text}, {"Item ArtNo", Int64.Type}, {"Plan Qty", type number}, {"Actual Qty", type number}, {"Yield", type number}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Item ArtNo", "Plan Qty"}, #"Count&Filter_Occurances", {"Batch Item No", "Plan Qty"}, "Count_Occurances", JoinKind.Inner),
    #"Expanded Count_Occurances" = Table.ExpandTableColumn(#"Merged Queries", "Count_Occurances", {"Count"}, {"Count"}),
    #"Grouped Rows" = Table.Group(#"Expanded Count_Occurances", {"Batch Item No", "Plan Qty"}, 
        {
            {"Item Name", each Table.First(_, "Item Name")[Item Name]},
            {"Overall Avg Yield%", each List.Average([#"%"]), type nullable number},
            {"Max Yield%", each List.Max([#"%"]), type nullable number},
            /* Change the below to perform the List.Average operation on a subset of the data records.
            {"Bottom80% Yield%", each List.Average([#"%"]), type nullable number},
            {"Top20% Yield%", each List.Average([#"%"]), type nullable number},
            */
            {"Count of occurances", each Table.First(_, "Count")[Count]}
        })
in
    #"Grouped Rows"

Count & Filter query

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Batch No", Int64.Type}, {"Item Name", type text}, {"Item ArtNo", Int64.Type}, {"Plan Qty", type number}, {"Actual Qty", type number}, {"Yield", type number}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Item ArtNo", "Plan Qty"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
    #"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each [Count] >= 5)
in
    #"Filtered Rows"

Sample data

Item Name Item ArtNo Batch No Plan Qty Actual Qty Yield
Banana 1 1002 10 9.7 0.97
Banana 1 1003 10 10.3 1.03
Banana 1 1004 10 10.1 1.01
Banana 1 1005 10 8.9 0.89
Apple 2 2001 20 24.1 1.21
Apple 2 2002 20 25 1.25
Apple 2 2003 20 17 0.85
Apple 2 2004 20 19.3 0.97
Orange 3 3001 50 50.5 1.01
Orange 3 3002 50 52.2 1.04
Orange 3 3003 50 51 1.02
Orange 3 3004 50 51 1.02
Orange 3 3005 50 51.8 1.04
Orange 3 3006 50 49.9 1.00
Orange 3 3007 70 72 1.03
Orange 3 3008 70 68.9 0.98
Orange 3 3009 70 53.2 0.76
Kiwi 4 4001 80 32 0.40
Kiwi 4 4002 80 53 0.66
Kiwi 4 4003 80 77 0.96
Kiwi 4 4004 80 35 0.44
Kiwi 4 4005 80 48 0.60
Kiwi 4 4006 80 34 0.43
Kiwi 4 4007 80 110 1.38

Edit: Added sample data as table.

$\endgroup$
1
  • $\begingroup$ I don't do ranking much, but when I do, I start with DAX not Power Query. See daxpatterns.com/ranking $\endgroup$
    – TheRizza
    Mar 10 at 16:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.