尝试在Excel Power Query SelectRows行中使用字段名称变量时出错

时间:2017-06-28 22:10:35

标签: excel powerquery excel-2016

我有以下Excel 2016 Power Query(获取和转换查询)。它工作正常,但我试图在两个SelectRows行中使用变量作为字段名称:

let

    //Variables:
        //Server Name:
            DefnameServerName = Excel.CurrentWorkbook(){[Name="IMPORT_SERVER_NAME"]}[Content],
            ServerName = DefnameServerName{0}[Column1],
        //Database Name:
            DefnameDatabaseName = Excel.CurrentWorkbook(){[Name="IMPORT_DATABASE_NAME"]}[Content],
            DatabaseName = DefnameDatabaseName{0}[Column1],
        //Schema Name:
            DefnameSchemaName = Excel.CurrentWorkbook(){[Name="IMPORT_SCHEMA_NAME"]}[Content],
            SchemaName = DefnameSchemaName{0}[Column1],
        //Table Name:
            DefnameTableName = Excel.CurrentWorkbook(){[Name="IMPORT_TABLE_NAME"]}[Content],
            TableName = DefnameTableName{0}[Column1],
        //ID Field Name:
            DefnameFieldNameID = Excel.CurrentWorkbook(){[Name="IMPORT_FIELDNAME_ID"]}[Content],
            FieldNameID = DefnameFieldNameID{0}[Column1],
        //Customer Field Name:
            DefnameFieldNameCustomer = Excel.CurrentWorkbook(){[Name="IMPORT_FIELDNAME_CUSTOMER"]}[Content],
            FieldNameCustomer = DefnameFieldNameCustomer{0}[Column1],
        //Region Field Name:
            DefnameFieldNameRegion = Excel.CurrentWorkbook(){[Name="IMPORT_FIELDNAME_REGION"]}[Content],
            FieldNameRegion = DefnameFieldNameRegion{0}[Column1],
        //Month Field Name:
            DefnameFieldNameMonth = Excel.CurrentWorkbook(){[Name="IMPORT_FIELDNAME_MONTH"]}[Content],
            FieldNameMonth = DefnameFieldNameMonth{0}[Column1],
        //Forecast Value Field Name:
            DefnameFieldNameForecastValue = Excel.CurrentWorkbook(){[Name="IMPORT_FIELDNAME_FORECAST_VALUE"]}[Content],
            FieldNameForecastValue = DefnameFieldNameForecastValue{0}[Column1],

    //Parameters:
        //ID:
            DefnameParameterID = Excel.CurrentWorkbook(){[Name="IMPORT_PARAMETER_ID"]}[Content],
            ParameterID = DefnameParameterID{0}[Column1],
        //Customer:
            DefnameParameterCustomer = Excel.CurrentWorkbook(){[Name="IMPORT_PARAMETER_CUSTOMER"]}[Content],
            ParameterCustomer = DefnameParameterCustomer{0}[Column1],

    Source = Sql.Database(ServerName, DatabaseName),
    dbo_MidtermOmnibusForecast = Source{[Schema=SchemaName,Item=TableName]}[Data],
    RemoveOtherColumns = Table.SelectColumns(dbo_MidtermOmnibusForecast,{FieldNameID, FieldNameCustomer, FieldNameMonth, FieldNameForecastValue, FieldNameRegion}),
    FilterByUUID = Table.SelectRows(RemoveOtherColumns, each ([UUID] = ParameterID)),
    FilterByCustomer = Table.SelectRows(FilterByUUID, each ([ForecastName] = ParameterCustomer)),
    PivotMonths = Table.Pivot(Table.TransformColumnTypes(FilterByCustomer, {{FieldNameMonth, type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(FilterByCustomer, {{FieldNameMonth, type text}}, "en-US")[ForecastDate]), FieldNameMonth, FieldNameForecastValue, List.Sum),
    SortByCustomerThenRegion = Table.Sort(PivotMonths,{{FieldNameCustomer, Order.Ascending}, {FieldNameRegion, Order.Ascending}})
in
    SortByCustomerThenRegion

我试过这个:

    FilterByUUID = Table.SelectRows(RemoveOtherColumns, each ([FieldNameID] = ParameterID)),
    FilterByCustomer = Table.SelectRows(FilterByUUID, each ([FieldNameCustomer] = ParameterCustomer)),

我试过这个:

    FilterByUUID = Table.SelectRows(RemoveOtherColumns, each (FieldNameID = ParameterID)),
    FilterByCustomer = Table.SelectRows(FilterByUUID, each (FieldNameCustomer = ParameterCustomer)),

但他们都破坏了查询。如何在两个SelectRows行中使用字段名称变量?

1 个答案:

答案 0 :(得分:0)

https://social.technet.microsoft.com/Forums/en-US/13df0744-4f27-4f3a-8565-7e24b1d73c17/error-trying-to-use-variable-for-the-field-name-in-a-selectrows-line?forum=powerquery提供了答案:

您不能使用带有可变字段名称的方括号,但必须使用Record.Field,如下所示:

FilterByUUID = Table.SelectRows(RemoveOtherColumns, each (Record.Field(_, FieldNameID) = ParameterID)),

(只有在字段名称周围使用方括号时才有必要,因为它们位于此SelectRows行中。如果没有方括号,如上面代码中的SelectColumns行,您只需交换变量就像我做的那样.-- Greg)