Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Mapping values in [list] under a list

Hi All   I would like a solution to mapping value as below situation:   Mapping Table Mapping Description Category *A*B*C* Category 1 *A*D* Category 2 Asterisk "*" here is for Ex...
  • dufoq3's avatar
    2 years ago

    Hi Anonymous, check this:

     

    I've added 1 more row to sample data to determine: if order is different (for row no 4 it is B then A then C) there is no match.

     

    Result

    let
        DataTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjBwNDBwclaK1YlWArEMDJwNDMA8oAyQ6QJlO4G4SrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Description = _t]),
        MappingTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W0nLUctJy1lLSUXJOLElNzy+qVDBUitUBS7igCBspxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Mapping Description" = _t, Category = _t]),
        Ad_MappingList = Table.AddColumn(MappingTable, "MappingList", each List.Select(Text.Split([Mapping Description], "*"), (x)=> x <> ""), type list),
        MappingListsBuffer = List.Buffer(Ad_MappingList[MappingList]),
        Ad_Category = Table.AddColumn(DataTable, "Category", each 
            [ a = MappingListsBuffer,
              b = List.Transform({0..List.Count(a)-1}, (x)=>
                        List.Accumulate(
                            {0..List.Count(a{x})-1},
                            {},
                            (s,c)=> s & {Text.PositionOf([Description], a{x}{c}, Occurrence.First)})),
              c = List.Transform(b, (x)=> if List.Contains(x, -1) or x <> List.Sort(x) then null else 1),
              d = List.PositionOf(c, 1, Occurrence.First),
              e = try MappingTable[Category]{d}? otherwise null
            ][e], type text)
    in
        Ad_Category