Move up values when null Power Query

浪子不回头ぞ 提交于 2020-12-15 08:11:29

问题


At this moment I have a big table in Excel that I would like to use with dynamic dropdown (cascade options). Depending on the selection you do on the first dropdown, then in the next cell, you should have a filtered drop down menu. Also, any option is unique for the main category selected.

The first thing I did was to take the columns I need and pivot them so it looks something similar to this (C are the column names and V are the values. Since any option is unique for any category, I have problems with null values since it is a large number of rows):

C1   | C2   | C3
V1   | null | null
V2   | null | null
null | V3   | null
null | null | V4
null | null | V5

This format is not working properly for dynamic dropdowns since it is showing all null fields at first. My question is if there is any way to remove null values with power query so I can have all values begging in the first row, such as:

C1   | C2   | C3
V1   | V3   | V4
V2   | null | V5
null | null | null
null | null | null
null | null | null

I have tried with the "fill down" or "fill up" option and then removing duplicates but is not working since it is repeating the same element so many times that is not useful for a final dropdown menu.

Not sure if there is a way to accomplish it so any help or suggestion is really appreciated.

Thanks in advance!


回答1:


Since the columns are independent, you can turn each one into a list, remove the nulls, then combine them back into a table.

Table.FromColumns(
    {
        List.RemoveNulls(Pivot[C1]),
        List.RemoveNulls(Pivot[C2]),
        List.RemoveNulls(Pivot[C3])
    },
    {"C1","C2","C3"}
)

Result:

This can be made more dynamic if the number of columns isn't always three but the same idea should apply.


Edit:

It's actually simpler than I initially anticipated to make this dynamic, independent of the number of columns and their names:

Table.FromColumns(
    List.Transform(Table.ToColumns(Pivot), List.RemoveNulls), 
    Table.ColumnNames(Pivot)
)


来源:https://stackoverflow.com/questions/64802547/move-up-values-when-null-power-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!