Forum Discussion

krishma's avatar
krishma
Regular Visitor
2 years ago
Solved

Power BI Desktop query

There are two tables which are mapped on the basis of name:
table 1                           

 

nametype
rama
shaymb
kirana
angleab
davidc
anghaa
arpithab
ramb
kiranc
davida

 

Table 2

namemoney
ram200
shaym300
kiran500
anglea600
david700
angha800
arpitha100

 

When i am doing left join on the bass of name, it gives me

nametypemoney
rama200
shaymb300
kirana500
angleab600
davidc700
anghaa800
arpithab200
ramb200
kiranc500
davida700

 

but i want output like this: total money for that name should be divided equally for each type for ex: Ram has two types a and b and money is 200 so in the final table it should be like
Ram a 100
ram b 100

 

below output is what i am looking for

nametypemoney I want to come
anghaa800
angleab600
arpithab200
davidc350
davida350
kirana250
kiranc250
rama100
ramb100
shaymb300

 

can someplease please help me

  • You can do this in Power Query. 

    Group the the table with the types by name choosing all rows to get a nested table per name.

    Merge by Name with the money query.

    Expand the merge to get the money column

    Add a column to get the row count of the nested tables. 

     

    Table.AddColumn(PREVIOUSSTEP, "nestedRowCount", each Table.RowCount([_nestedTable]), Int64.Type)​

     

    Replace the values in the money column with the money value divided by the nested row count.

     

    Table.ReplaceValue(PREVIOUSSTEP,each [money],each [money]/[nestedRowCount],Replacer.ReplaceValue,{"money"})

     

    ​Remove the nestedRowCount column

    expand the nested table type column only.

    You should get something like...

  • Table 1

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKkrMVdJRSlSK1YlWKs5IrATxksC87MyixDy4XGJeek5qIlwyJbEsMwXIS4ZJZiQilBYVZJZkINRCrEA1NBnFFKC+WAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [name = _t, #"type" = _t]),
        #"Added Custom" = Table.AddColumn(Source, "money I want to come", (k)=> Table.SelectRows(#"Table 2",each [name]=k[name]){0}[money]/List.Count(List.Select(Source[name],each _=k[name])))
    in
        #"Added Custom"

     

     

5 Replies

  • You can do this in Power Query. 

    Group the the table with the types by name choosing all rows to get a nested table per name.

    Merge by Name with the money query.

    Expand the merge to get the money column

    Add a column to get the row count of the nested tables. 

     

    Table.AddColumn(PREVIOUSSTEP, "nestedRowCount", each Table.RowCount([_nestedTable]), Int64.Type)​

     

    Replace the values in the money column with the money value divided by the nested row count.

     

    Table.ReplaceValue(PREVIOUSSTEP,each [money],each [money]/[nestedRowCount],Replacer.ReplaceValue,{"money"})

     

    ​Remove the nestedRowCount column

    expand the nested table type column only.

    You should get something like...

    • lbendlin's avatar
      lbendlin
      Super User

      Table 1

       

       

      let
          Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKkrMVdJRSlSK1YlWKs5IrATxksC87MyixDy4XGJeek5qIlwyJbEsMwXIS4ZJZiQilBYVZJZkINRCrEA1NBnFFKC+WAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [name = _t, #"type" = _t]),
          #"Added Custom" = Table.AddColumn(Source, "money I want to come", (k)=> Table.SelectRows(#"Table 2",each [name]=k[name]){0}[money]/List.Count(List.Select(Source[name],each _=k[name])))
      in
          #"Added Custom"