Adjusting values based on rankings

LBala

New Member
Joined
Apr 3, 2014
Messages
22
I am having trouble building a measure to accomplish what's described below.
I am working with the following scenario:
- There are 2 types of balls: green and red.
- There are 3 types of buckets: A, B and C.

The balls are randomly dropped inside the buckets. The table below shows how many balls are inside each bucket:
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Ball_color[/TD]
[TD="align: center"]Bucket-A[/TD]
[TD="align: center"]Bucket-B[/TD]
[TD="align: center"]Bucket-C[/TD]
[TD="align: center"]Total[/TD]
[/TR]
[TR]
[TD]Green[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]4[/TD]
[/TR]
[TR]
[TD]Red[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]6[/TD]
[/TR]
</tbody>[/TABLE]






Now, the table below shows how many balls, by color, should be removed from the buckets:
[TABLE="class: grid, width: 100, align: left"]
<tbody>[TR]
[TD]Ball_color[/TD]
[TD="align: center"]Remove[/TD]
[/TR]
[TR]
[TD]Green[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD]Red[/TD]
[TD="align: center"]3[/TD]
[/TR]
</tbody>[/TABLE]






The balls should be removed from the buckets starting from the lowest ranked bucket for each ball color.
Below is the rank of each bucket by ball color:
[TABLE="class: grid, width: 300, align: left"]
<tbody>[TR]
[TD]Ball_color[/TD]
[TD="align: center"]Bucket-A[/TD]
[TD="align: center"]Bucket-B[/TD]
[TD="align: center"]Bucket-C[/TD]
[/TR]
[TR]
[TD]Green[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]1[/TD]
[/TR]
[TR]
[TD]Red[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]3[/TD]
[/TR]
</tbody>[/TABLE]






The table below shows how the final result looks like after the balls are removed starting from the lowest ranked buckets:[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Ball_color[/TD]
[TD="align: center"]Bucket-A[/TD]
[TD="align: center"]Bucket-B[/TD]
[TD="align: center"]Bucket-C[/TD]
[TD="align: center"]Total[/TD]
[/TR]
[TR]
[TD]Green[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD]Red[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]3[/TD]
[/TR]
</tbody>[/TABLE]







Explaining the case of the green ball: the Bucket-A is ranked lowest (rank = 3) among the 3 buckets and the quantity in the Bucket-A dropped from 1 to 0. Then the Bucket-B (rank = 2) had its quantity lowered from 2 to 1. Result: total of 2 green balls removed.

Question: Is it possible to build a measure to give me the results showing in the table above? Basically I need a measure to calculate the quantity of balls to be removed from each bucket according to their rankings by ball color.

------------------------------------

These are the tables I am working with:

Table: Remove
[TABLE="class: grid, width: 100, align: left"]
<tbody>[TR]
[TD]Ball_color[/TD]
[TD="align: center"]Remove[/TD]
[/TR]
[TR]
[TD]Green[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD]Red[/TD]
[TD="align: center"]3[/TD]
[/TR]
</tbody>[/TABLE]






Table: Quantity
[TABLE="class: grid, width: 300, align: left"]
<tbody>[TR]
[TD]Ball_color[/TD]
[TD]Bucket_ID[/TD]
[TD]Line_ID[/TD]
[TD="align: center"]Quantity[/TD]
[/TR]
[TR]
[TD]Green[/TD]
[TD]Bucket-A[/TD]
[TD]Green-A[/TD]
[TD="align: center"]1[/TD]
[/TR]
[TR]
[TD]Green[/TD]
[TD]Bucket-B[/TD]
[TD]Green-B[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD]Green[/TD]
[TD]Bucket-C[/TD]
[TD]Green-C[/TD]
[TD="align: center"]1[/TD]
[/TR]
[TR]
[TD]Red[/TD]
[TD]Bucket-A[/TD]
[TD]Red-A[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD]Red[/TD]
[TD]Bucket-B[/TD]
[TD]Red-B[/TD]
[TD="align: center"]3[/TD]
[/TR]
[TR]
[TD]Red[/TD]
[TD]Bucket-C[/TD]
[TD]Red-C[/TD]
[TD="align: center"]1[/TD]
[/TR]
</tbody>[/TABLE]












Table: Rank
[TABLE="class: grid, width: 300, align: left"]
<tbody>[TR]
[TD]Ball_color[/TD]
[TD]Bucket_ID[/TD]
[TD]Line_ID[/TD]
[TD="align: center"]Rank[/TD]
[/TR]
[TR]
[TD]Green[/TD]
[TD]Bucket-A[/TD]
[TD]Green-A[/TD]
[TD="align: center"]3[/TD]
[/TR]
[TR]
[TD]Green[/TD]
[TD]Bucket-B[/TD]
[TD]Green-B[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD]Green[/TD]
[TD]Bucket-C[/TD]
[TD]Green-C[/TD]
[TD="align: center"]1[/TD]
[/TR]
[TR]
[TD]Red[/TD]
[TD]Bucket-A[/TD]
[TD]Red-A[/TD]
[TD="align: center"]1[/TD]
[/TR]
[TR]
[TD]Red[/TD]
[TD]Bucket-B[/TD]
[TD]Red-B[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD]Red[/TD]
[TD]Bucket-C[/TD]
[TD]Red-C[/TD]
[TD="align: center"]3[/TD]
[/TR]
</tbody>[/TABLE]












The tables "Remove" and "Quantity" are linked by the column "Ball_color".
The tables "Quantity" and "Rank" are linked by the column "Line_ID".

Any help is appreciated. Thanks.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
It looks more like a new table than a measure, so my suggestion would be to use Power Query.

I loaded your tables into Power Query and from there:

I combined tables Quantity and Rank in Query/table QuantityRank:

Code:
let
    Source = Table.NestedJoin(Quantity,{"Line_ID"},Rank,{"Line_ID"},"Rank",JoinKind.LeftOuter),
    ExpandedRank = Table.ExpandTableColumn(Source, "Rank", {"Rank"})
in
    ExpandedRank

I created query FinalResult:

Code:
let
    Source = Table.NestedJoin(Remove,{"Ball_color"},QuantityRank,{"Ball_color"},"QR",JoinKind.LeftOuter),
    Sorted = Table.TransformColumns(Source,{{"QR", each Table.Sort(_,{{"Rank", Order.Descending}}), type table}}),
    AddedStillToRemove = Table.AddColumn(Sorted, "StillToRemove", each List.Accumulate([QR][Quantity],{[Remove]},(StillToRemove,Quantity) => StillToRemove & {List.Max({0,List.Last(StillToRemove)-Quantity})}), type {Int64.Type}),
    AddedNewTable = Table.AddColumn(AddedStillToRemove, "NewTable", each Table.FromColumns(Table.ToColumns([QR])&{List.FirstN([StillToRemove],List.Count([StillToRemove])-1)}), type table),
    RemovedColumns1 = Table.RemoveColumns(AddedNewTable,{"Remove", "QR", "StillToRemove"}),
    ExpandedNewTable = Table.ExpandTableColumn(RemovedColumns1, "NewTable", {"Column2", "Column4", "Column5", "Column6"}, {"Bucket_ID", "Quantity", "Rank", "StillToRemove"}),
    AddedNewQuantity = Table.AddColumn(ExpandedNewTable, "NewQuantity", each List.Max({0,[Quantity]-[StillToRemove]}), Int64.Type),
    RemovedColumns2 = Table.RemoveColumns(AddedNewQuantity,{"Quantity", "Rank", "StillToRemove"}),
    PivotedBucket = Table.Pivot(RemovedColumns2, List.Distinct(RemovedColumns2[Bucket_ID]), "Bucket_ID", "NewQuantity"),
    InsertedSum = Table.AddColumn(PivotedBucket, "Total", each List.Sum(List.Skip(Record.FieldValues(_))), Int64.Type)
in
    InsertedSum
 
Upvote 0
Hi Marcel, your solution works perfectly, it indeed accomplishes the end result I need, thank you very much.
Quick question: In your opinion, is it possible to have a measure doing this adjustment instead of having to create a new table?
 
Upvote 0
As far as I know, a measure is basically 1 value, that may vary according to the filter context.
Maybe you mean a calculated column instead of a measure?

Anyhow, my opinion about measures and calculated columns isn't worth too much, as I'm really not a DAX expert.
 
Upvote 0
I meant a measure but the new table solution you provided is a nice way to get the solution to the problem.
Thanks.
 
Upvote 0
LBala,

This could be handled with a measure. I would also recommend some small changes to the data model.
Which version of Excel are you using, or are you using Power BI Desktop? Just want to know whether you are using a version DAX with variables before making any suggestions.

Regards,
Owen
 
Upvote 0
Ozeroth;


I am using Excel 2016 64-bit. How would you set up the tables in order to have a measure doing the calculation?


Thanks in advance.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top