Power Query multi-condition sorting of Power BI

Original address: Power Query multi-condition sorting of Power BI
Homepage of the blogger: Mori_Katie

Business scene

Three sorting requirements:
①Sort according to sales and employee sales; ——Priority: Sales
②Sequence of employees in departments according to sales; ——Priority: Department, sales
③According to sales, employees in Sorting in department and level;-priority: department, level, sales
Insert picture description here

Practical operation:
①Sorting sales
First sort the sales in descending order—add an index.

= Table.Sort(删除的列,{
    
    {
    
    "销售额", Order.Descending}})
= Table.AddIndexColumn(排序的行, "销售额排名", 1, 1, Int64.Type)

②Department-Sales Sort:

Group in descending order: Select the department, group all rows, and sort them in descending order of sales.

= Table.Group(已添加索引, {
    
    "部门"}, {
    
    {
    
    "组合", each Table.Sort(_,{
    
    {
    
    "销售额", Order.Descending}}), type table [员工=nullable text, 部门=nullable text, 级别=nullable text, 销售额=nullable number, 销售额排名=number]}})

Add a custom column-department sales ranking, and add an index.

= Table.AddColumn(分组的行, "部门销售额排名", each Table.AddIndexColumn([组合], "部门销售额排名", 1, 1))

Delete other columns, only keep "Department Sales Ranking", expand the table

= Table.SelectColumns(已添加自定义,{
    
    "部门销售额排名"})
= Table.ExpandTableColumn(删除的其他列, "部门销售额排名", {
    
    "员工", "部门", "级别", "销售额", "销售额排名", "部门销售额排名"}, {
    
    "员工", "部门", "级别", "销售额", "销售额排名", "部门销售额排名"})

The following figure after expansion: Insert picture description here
③Department-level-sales sorting:
same as the operation above, grouping is a combination of department and level

Grouping in descending order: Select department and level, group all rows, and sort them in descending order of sales.

= Table.Group(#"展开的“部门销售额排名”", {"部门", "级别"}, {
    
    {"组合", each _, type table [员工=text, 部门=text, 级别=text, 销售额=number, 销售额排名=number, 部门销售额排名=number]}})

Add a custom column-department-level sales ranking, and add an index.

= Table.AddColumn(分组的行1, "部门级别销售额排名", each Table.AddIndexColumn([组合], "部门级别销售额排名", 1, 1))

Delete other columns, only keep "Department Sales Ranking", expand the table

= Table.SelectColumns(已添加自定义1,{
    
    "部门级别销售额排名"})
= Table.ExpandTableColumn(删除的其他列1, "部门级别销售额排名", {
    
    "员工", "部门", "级别", "销售额", "销售额排名", "部门销售额排名", "部门级别销售额排名"}, {
    
    "员工", "部门", "级别", "销售额", "销售额排名", "部门销售额排名", "部门级别销售额排名"})

Insert picture description here
Loaded into the table, the final ranking is as follows:
Insert picture description here
Summary:
When the amount of data is large, this sorting method can be quickly generated. If you enter the formula sorting directly in the table, the table will get stuck when the amount of data is large.

Guess you like

Origin blog.csdn.net/qq_35866846/article/details/110183657