First I have to make some data, since no data in copyable form was given in the question.
keys = {"year", "id", "qty"};
vals =
{{2012, "abc", 123}, {2012, "def", 456}, {2013, "abc", 321},
{2013, "ghi", 42}, {2014, "abc", 312}};
data = AssociationThread[keys, #] & /@ vals
{<|"year" -> 2012, "id" -> "abc", "qty" -> 123|>,
<|"year" -> 2012, "id" -> "def", "qty" -> 456|>,
<|"year" -> 2013, "id" -> "abc", "qty" -> 321|>,
<|"year" -> 2013, "id" -> "ghi", "qty" -> 42|>,
<|"year" -> 2014, "id" -> "abc", "qty" -> 312|>}
Given the data, a composition of MapThread
, Join
, Map
, KeyDrop
, KeyTake
and Values
will do the job. Perhaps it can be expressed more succinctly, but I've been much good at code golf.
MapThread[
Join,
{<|"newKey" -> #|> & /@ (KeyDrop[data, "qty"] // Values), KeyTake[data, "qty"]}]
{<|"newKey" -> {2012, "abc"}, "qty" -> 123|>,
<|"newKey" -> {2012, "def"}, "qty" -> 456|>,
<|"newKey" -> {2013, "abc"}, "qty" -> 321|>,
<|"newKey" -> {2013, "ghi"}, "qty" -> 42|>,
<|"newKey" -> {2014, "abc"}, "qty" -> 312|>}
{2013, TYML143} -> data
is a compositeKey
, whereas the list is theValue
here. $\endgroup$ – alancalvitti Nov 13 '14 at 22:17