excelpivot-tablepowerquerypowerpivotm

Power Query: after combining 2 tables got something strange


I have a column in which each value is repeated twice. And each value has its own share (input data). I need to calculate the average share among the first occurrence of values in the first column and the average for the second occurrence, for each subgroup (the subgroups are distinguished by the first part of the name: like input car 1, car 1, car 2, car 2, bike 1, bike 1, bike 2, bike 2, etc (word + number). An example is shown in the figure.enter image description hereTo do this, I created an occurrence index. However, when I combine data from the new table with indexes and the old table with weeks and shares, I get something strange: each value from the first column is now repeated not twice, but four times. How can I fix this? It is also worth noting that I need the average not for the entire column, but for each subgroup of the column.

I created an occurrence index as suggested here: link Right click, Group By, keep name, use operation All Rows Change the code in the window from

= Table.Group(#"Changed Type", {"Group ID"}, {{"Count", each , type table ....

to = Table.Group(#"Changed Type", {"Group ID"}, {{"Count", each Table.AddIndexColumn(_, "Index", 0, 1, Int64.Type), type table}})


Solution

  • you can try this in PQ to create the index column and new name column

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Share", type number}}),
        Custom1 = Table.Group(#"Changed Type", {"Name"}, {{"Count", each Table.AddIndexColumn(_, "Index", 1, 1, Int64.Type), type table}}),
        #"Expanded Count" = Table.ExpandTableColumn(Custom1, "Count", {"Share", "Index"}, {"Share", "Index"}),
        #"Added Custom" = Table.AddColumn(#"Expanded Count", "Name2", each Text.Remove([Name],{"0".."9"}))
    in
        #"Added Custom"
    

    enter image description here

    then you can create a calculated column in PP

    =CALCULATE(AVERAGE([Share]),ALLEXCEPT(Table1_1,Table1_1[Name2],Table1_1[Index]))
    

    enter image description here


    I tried to add a column in the sample data

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"share", type number}}),
        Custom1 = Table.Group(#"Changed Type", {"Name"}, {{"Count", each Table.AddIndexColumn(_, "Index", 1, 1, Int64.Type), type table}}),
        #"Expanded Count" = Table.ExpandTableColumn(Custom1, "Count", {"column", "share", "Index"}, {"column", "share", "Index"})
    in
        #"Expanded Count"
    

    enter image description here